forked from mar5chi/hand_gesture_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandTrackerRenderer.py
196 lines (178 loc) · 9.2 KB
/
HandTrackerRenderer.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
import cv2
import numpy as np
LINES_HAND = [[0,1],[1,2],[2,3],[3,4],
[0,5],[5,6],[6,7],[7,8],
[5,9],[9,10],[10,11],[11,12],
[9,13],[13,14],[14,15],[15,16],
[13,17],[17,18],[18,19],[19,20],[0,17]]
class HandTrackerRenderer:
def __init__(self,
tracker,
output=None):
self.tracker = tracker
# Rendering flags
if self.tracker.use_lm:
self.show_pd_box = False
self.show_pd_kps = False
self.show_rot_rect = False
self.show_handedness = False
self.show_landmarks = True
self.show_scores = False
self.show_gesture = self.tracker.use_gesture
else:
self.show_pd_box = True
self.show_pd_kps = False
self.show_rot_rect = False
self.show_scores = False
self.show_fps = False
self.show_inferences_status = False
if output is None:
self.output = None
else:
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
self.output = cv2.VideoWriter(output,fourcc,self.tracker.video_fps,(self.tracker.img_w, self.tracker.img_h))
def norm2abs(self, x_y):
x = int(x_y[0] * self.tracker.frame_size - self.tracker.pad_w)
y = int(x_y[1] * self.tracker.frame_size - self.tracker.pad_h)
return (x, y)
def draw_selection(self, todisplay, selection):
font = cv2.FONT_HERSHEY_PLAIN
font_thickness = 1
dist_x = 20
dist_y = 30
(w, h), _ = cv2.getTextSize(todisplay, font, 1, font_thickness)
cv2.rectangle(self.frame, (dist_x, dist_y-h-5), (dist_x + w, dist_y+5), (240, 240, 240), -1)
# cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
cv2.putText(self.frame, todisplay, (dist_x, dist_y), font, 1, (15,15,15), font_thickness)
dist_y += (h + 13)
# Draw user selections:
for k, v in selection.items():
#text = f'{k}: {v}'
if k == 'item' and 'label' in selection: # show label, not name, if it's in selection
continue
text = v
(w, h), _ = cv2.getTextSize(text, font, 1, 2)
cv2.rectangle(self.frame, (dist_x + 30, dist_y-h-5), (50 + w, dist_y+5), (240, 240, 240), -1)
# cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
cv2.putText(self.frame, text, (dist_x + 30, dist_y), font, 1, (15,15,15), font_thickness)
dist_y += (h + 13)
def draw_hand(self, hand):
if self.tracker.use_lm:
# (info_ref_x, info_ref_y): coords in the image of a reference point
# relatively to which hands information (score, handedness, xyz,...) are drawn
info_ref_x = hand.landmarks[0,0]
info_ref_y = np.max(hand.landmarks[:,1])
# thick_coef is used to adapt the size of the draw landmarks features according to the size of the hand.
thick_coef = hand.rect_w_a / 400
if hand.lm_score > self.tracker.lm_score_thresh:
if self.show_rot_rect:
cv2.polylines(self.frame, [np.array(hand.rect_points)], True, (0,255,255), 2, cv2.LINE_AA)
if self.show_landmarks:
lines = [np.array([hand.landmarks[point] for point in line]).astype(np.int) for line in LINES_HAND]
cv2.polylines(self.frame, lines, False, (255, 255, 255), int(1+thick_coef*1), cv2.LINE_AA)
radius = int(1+thick_coef*5)
if self.tracker.use_gesture:
# color depending on finger state (1=open, 0=close, -1=unknown)
color = { 1: (205, 205, 205), 0: (50, 50, 50), -1:(50, 50, 50)}
cv2.circle(self.frame, (hand.landmarks[0][0], hand.landmarks[0][1]), radius, color[-1], -1)
for i in range(1,5):
cv2.circle(self.frame, (hand.landmarks[i][0], hand.landmarks[i][1]), radius, color[hand.thumb_state], -1)
for i in range(5,9):
cv2.circle(self.frame, (hand.landmarks[i][0], hand.landmarks[i][1]), radius, color[hand.index_state], -1)
for i in range(9,13):
cv2.circle(self.frame, (hand.landmarks[i][0], hand.landmarks[i][1]), radius, color[hand.middle_state], -1)
for i in range(13,17):
cv2.circle(self.frame, (hand.landmarks[i][0], hand.landmarks[i][1]), radius, color[hand.ring_state], -1)
for i in range(17,21):
cv2.circle(self.frame, (hand.landmarks[i][0], hand.landmarks[i][1]), radius, color[hand.little_state], -1)
else:
for x,y in hand.landmarks[:,:2]:
cv2.circle(self.frame, (int(x), int(y)), radius, (0,128,255), -1)
if self.show_handedness:
cv2.putText(self.frame, f"{hand.label.upper()} {hand.handedness:.2f}",
(info_ref_x-90, info_ref_y+40),
cv2.FONT_HERSHEY_PLAIN, 2, (0,255,0) if hand.handedness > 0.5 else (0,0,255), 2)
if self.show_scores:
cv2.putText(self.frame, f"Landmark score: {hand.lm_score:.2f}",
(info_ref_x-90, info_ref_y+110),
cv2.FONT_HERSHEY_PLAIN, 2, (255,255,0), 2)
if self.tracker.use_gesture and self.show_gesture:
cv2.putText(self.frame, hand.gesture, (info_ref_x-20, info_ref_y-50),
cv2.FONT_HERSHEY_PLAIN, 3, (255,255,255), 3)
if hand.pd_box is not None:
box = hand.pd_box
box_tl = self.norm2abs((box[0], box[1]))
box_br = self.norm2abs((box[0]+box[2], box[1]+box[3]))
if self.show_pd_box:
cv2.rectangle(self.frame, box_tl, box_br, (0,255,0), 2)
if self.show_pd_kps:
for i,kp in enumerate(hand.pd_kps):
x_y = self.norm2abs(kp)
cv2.circle(self.frame, x_y, 6, (0,0,255), -1)
cv2.putText(self.frame, str(i), (x_y[0], x_y[1]+12), cv2.FONT_HERSHEY_PLAIN, 1.5, (0,255,0), 2)
if self.show_scores:
if self.tracker.use_lm:
x, y = info_ref_x - 90, info_ref_y + 80
else:
x, y = box_tl[0], box_br[1]+60
cv2.putText(self.frame, f"Palm score: {hand.pd_score:.2f}",
(x, y),
cv2.FONT_HERSHEY_PLAIN, 2, (255,255,0), 2)
def draw_bag(self, bag):
if self.show_inferences_status:
# Draw inferences status
h = self.frame.shape[0]
u = h // 10
status=""
if bag.get("bpf_inference", 0):
cv2.rectangle(self.frame, (u, 8*u), (2*u, 9*u), (255,144,30), -1)
if bag.get("pd_inference", 0):
cv2.rectangle(self.frame, (2*u, 8*u), (3*u, 9*u), (0,255,0), -1)
nb_lm_inferences = bag.get("lm_inference", 0)
if nb_lm_inferences:
cv2.rectangle(self.frame, (3*u, 8*u), ((3+nb_lm_inferences)*u, 9*u), (0,0,255), -1)
def draw(self, frame, hands, todisplay, selection, bag={}):
self.frame = frame
if bag:
self.draw_bag(bag)
for hand in hands:
self.draw_hand(hand)
self.draw_selection(todisplay, selection)
return self.frame
def exit(self):
if self.output:
self.output.release()
def waitKey(self, delay=1):
if self.show_fps:
# Put it in the right upper corner, self.tracker.frame_size is the width of the frame
self.tracker.fps.draw(self.frame, orig=(self.tracker.frame_size - 200, 65), size=1, color=(240,180,100))
cv2.imshow("Hand Gesture Control", self.frame)
if self.output:
self.output.write(self.frame)
key = cv2.waitKey(delay)
if key == 32:
# Pause on space bar
key = cv2.waitKey(0)
if key == ord('s'):
print("Snapshot saved in snapshot.jpg")
cv2.imwrite("snapshot.jpg", self.frame)
elif key == ord('1'):
self.show_pd_box = not self.show_pd_box
elif key == ord('2'):
self.show_pd_kps = not self.show_pd_kps
elif key == ord('3'):
self.show_rot_rect = not self.show_rot_rect
elif key == ord('4') and self.tracker.use_lm:
self.show_landmarks = not self.show_landmarks
elif key == ord('5') and self.tracker.use_lm:
self.show_handedness = not self.show_handedness
elif key == ord('6'):
self.show_scores = not self.show_scores
elif key == ord('7') and self.tracker.use_lm:
if self.tracker.use_gesture:
self.show_gesture = not self.show_gesture
elif key == ord('f'):
self.show_fps = not self.show_fps
elif key == ord('s'):
self.show_inferences_status = not self.show_inferences_status
return key