forked from CalciferZh/minimal-hand
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app1.py
202 lines (141 loc) · 5.41 KB
/
app1.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import multiprocessing as mp
import os
import time
import cv2
import numpy as np
import pyvirtualcam
import zmq
from scipy import ndimage
from zmq.eventloop import ioloop, zmqstream
from utils import imresize
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf # noqa: E402
import logging # noqa: E402
tf.get_logger().setLevel(logging.ERROR)
from wrappers import ModelPipeline # noqa: E402
class WebcamCapture(cv2.VideoCapture):
def __init__(self):
super().__init__(index=0)
def read(self, image=None):
retval, image = super().read(image)
assert retval, 'No image'
return image
def read_rgb(self):
return cv2.cvtColor(self.read(), cv2.COLOR_BGR2RGB)
def to_vec(pos):
return dict(zip(['x', 'y', 'z'], pos))
def filter_nan_inf(x):
return x[np.isfinite(x)]
class FPS:
def __init__(self, prefix):
self.prefix = prefix
self.tic = None
self.fps = None
self.upd_rate = 0.001
def __call__(self):
if self.tic is None:
self.tic = time.time()
return
toc = time.time()
dt = toc - self.tic
self.tic = toc
if dt > 0:
if self.fps is None:
self.fps = 1 / dt
else:
self.fps = self.fps + self.upd_rate * (1 / dt - self.fps)
print(self.prefix, self.fps)
def display(msg, info_frame_, cam, recv_fps):
proc_frame = np.frombuffer(msg[0], dtype=info_frame_.dtype)
proc_frame.shape = info_frame_.shape
proc_frame = np.flip(proc_frame, axis=0)
cam.send(proc_frame)
proc_frame = cv2.cvtColor(proc_frame, cv2.COLOR_RGB2BGR)
cv2.imshow('Webcam', proc_frame)
cv2.waitKey(1)
# recv_fps()
def pull(info_frame_, base_fps_):
cam = pyvirtualcam.Camera(width=info_frame_.shape[1], height=info_frame_.shape[0], fps=base_fps_, delay=0)
context1 = zmq.Context()
socket1 = context1.socket(zmq.PULL)
socket1.bind('tcp://*:5556')
loop = ioloop.IOLoop.instance()
stream = zmqstream.ZMQStream(socket1, loop)
recv_fps = FPS('Recv:')
stream.on_recv(lambda msg: display(msg, info_frame_, cam, recv_fps))
loop.start()
def unit_vector(vector):
return vector / np.linalg.norm(vector)
def calc_origin(hmap):
origin = ndimage.measurements.center_of_mass(hmap)
origin_inv = origin[::-1]
return origin_inv[0] / 32, 1 - (origin_inv[1] / 32)
indexes_y = np.arange(5, 21).reshape((4, 4))
# indexes_y = np.arange(1, 21).reshape((5, 4))
indexes_x = indexes_y.T
def calc_hand_data(iv):
origin = to_vec(calc_origin(iv.hmap[0][..., 9]))
# origin = to_vec([iv.uv[9][1] / 32, 1 - (iv.uv[9][0]) / 32])
# UV instead of XY
# joints = list([to_vec([*(uv[::-1]/32), xyz[2]]) for xyz, uv in zip(iv.xyz, iv.uv)])
# joints = list([to_vec([*(uv[::-1]/32), xyz[2]]) for xyz, uv in zip(iv.xyz, xy)])
joints = list([to_vec(xyz) for xyz in iv.xyz.tolist()]) # tolist, because 0mq can't work with np.float
def calc_dists(indexes):
return np.array([
np.linalg.norm(iv.xyz[i[1:], :2] - iv.xyz[i[:-1], :2]) * 32 /
np.linalg.norm(iv.uv[i[1:]] - iv.uv[i[:-1]])
for i in indexes
])
dists_x = filter_nan_inf(calc_dists(indexes_x))
dists_y = filter_nan_inf(calc_dists(indexes_y))
if dists_x.size == 0 or dists_y.size == 0:
return None
dist_x = np.mean(dists_x)
dist_y = np.mean(dists_y)
# palm_size = sum(np.linalg.norm(iv.xyz[0] - iv.xyz[i]) for i in [1, 5, 9, 13, 17])
u09 = unit_vector(iv.xyz[9, :2] - iv.xyz[0, :2])
# horiz = abs(2 * np.arcsin(u09[0]) / np.pi)
vert = abs(2 * np.arcsin(u09[1]) / np.pi)
return origin, joints, dist_x, dist_y, vert
def crop(frame):
height, width, _ = frame.shape
size = min(int(width / 2), height)
margin_h = int((width - 2 * size) / 2)
margin_v = int((height - size) / 2)
return frame[margin_v:size + margin_v, margin_h:width - margin_h]
def main():
model = ModelPipeline()
webcam = WebcamCapture()
base_fps = webcam.get(cv2.CAP_PROP_FPS)
print('Base FPS:', base_fps)
info_frame = crop(webcam.read())
mp.Process(target=pull, args=(info_frame, base_fps)).start()
context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.bind('tcp://*:5555')
height, width, _ = info_frame.shape
hand_data_keys = ['origin', 'joints', 'distX', 'distY', 'vert']
fps_send = FPS('Send:')
while True:
frame_large = webcam.read_rgb()
frame_large = crop(frame_large)
frame_large_l = frame_large[:, :width // 2]
frame_large_r = frame_large[:, width // 2:]
frame_l = imresize(frame_large_l, (128, 128))
frame_r = imresize(frame_large_r, (128, 128))
# iv - intermediate values
ivl, _ = model.process(np.flip(frame_l, axis=1))
ivr, _ = model.process(frame_r)
hand_data_l = calc_hand_data(ivl)
hand_data_r = calc_hand_data(ivr)
if hand_data_l is not None and hand_data_r is not None:
socket.send_json({
'dataL': dict(zip(hand_data_keys, hand_data_l)),
'dataR': dict(zip(hand_data_keys, hand_data_r)),
'frameWidth': frame_large.shape[1],
'frameHeight': frame_large.shape[0],
}, zmq.SNDMORE)
socket.send(np.flip(frame_large, axis=0).tobytes())
fps_send()
if __name__ == '__main__':
main()