-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
176 lines (127 loc) · 5.08 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from socket import *
from threading import Thread
import sys
import cv2
import os
from sys import platform
import argparse
import time
import re
import numpy
def make_rect(height, width):
if(height > width):
#right hand
handRectangles = [
[
op.Rectangle(0., 0., 0., 0.),
op.Rectangle(0., (height - width)/2, width, width)
]
]
else:
#right hand
handRectangles = [
[
op.Rectangle(0., 0., 0., 0.),
op.Rectangle((width - height)/2, 0., height, height)
]
]
return handRectangles
def run_openpose(addr, data):
#openpose dir
dir_path="C:/Users/test/Desktop/openpose/build/examples/tutorial_api_python"
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
params["model_folder"] = dir_path+"/../../../models/"
params["hand"] = True
params["hand_detector"] = 2
params["body"] = 0
# Starting OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# Construct it from system arguments
# op.init_argv(args[1])
# oppython = op.OpenposePython()
frame = data
height, width, channels = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter(str(addr) + ".avi", fourcc, 30.0, (width, height))
out_file = open(str(addr) + ".txt", "a")
#cv2.imshow("VideoFrame", frame)
# Read image and face rectangle locations
#imageToProcess = cv2.imread(args[0].image_path)
# Create new datum
datum = op.Datum()
datum.cvInputData = frame
datum.handRectangles = make_rect(height, width)
# Process and display image
opWrapper.emplaceAndPop([datum])
out_file.write(str(datum.handKeypoints[1]))
#cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) #show image
video.write(datum.cvOutputData)
out_file.close()
return datum.cvOutputData, datum.handKeypoints[1]
def recvall(sock, count):
buf = b''
while count:
newbuf = sock.recv(count)
buf += newbuf
count -= len(newbuf)
return buf
def data_receive(clientSock, addr):
frame_size = clientSock.recv(30).decode()
frame_size = int(frame_size, 2)
print(frame_size)
frame = 0
while True:
length = recvall(clientSock, 16)
stringData = recvall(clientSock, int(length))
data = numpy.fromstring(stringData, dtype = 'uint8')
imageToProcess = cv2.imdecode(data, cv2.IMREAD_COLOR)
print(int(length))
print(str(frame) + "/" + str(frame_size))
img_result, skeleton_result = run_openpose(addr, imageToProcess)
print(img_result)
#cv2.imshow('result', img_result)
#cv2.waitKey(0)
'''
result_data = numpy.array(img_result)
stringData = result_data.tostring()
clientSock.sendall((str(len(stringData))).encode().ljust(16) + stringData)
'''
if(frame == frame_size):
print('read end')
break
frame += 1
clientSock.send('fend'.encode())
print('send')
def run_server(host='127.0.0.1', port=8080):
with socket(AF_INET, SOCK_STREAM) as serverSock:
serverSock.bind(('', 8080))
while True:
serverSock.listen(5)
clientSock, addr = serverSock.accept()
# 새 연결이 생성되면 스레드를 만들어서 통신
t = Thread(target=data_receive, args=(clientSock, addr))
t.start()
serverSock.close()
if __name__ == '__main__':
# Import Openpose (Windows/Ubuntu/OSX)
dir_path="C:/Users/test/Desktop/openpose/build/examples/tutorial_api_python"
try:
# Windows Import
if platform == "win32":
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append(dir_path + '/../../python/openpose/Release');
os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;'
import pyopenpose as op
else:
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append('../../python');
# If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
# sys.path.append('/usr/local/python')
from openpose import pyopenpose as op
except ImportError as e:
print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
raise e
run_server()