3
3
4
4
5
5
TEXT_COLOR = (102 ,51 ,0 )
6
- BAR_COLOR = (0 ,255 ,0 )
6
+ BAR_COLOR = (51 ,255 ,51 )
7
7
LINE_COLOR = (255 ,255 ,255 )
8
8
LM_COLOR = (255 ,51 ,255 )
9
9
BOX_COLOR = (153 ,0 ,153 )
23
23
24
24
25
25
def find_boundary_lm (landmarks ):
26
- """ Get the landmarks/joints with maximum x, minimum x, maximum y, and minimum y values. """
26
+ """ Get the landmarks with maximum x, minimum x, maximum y, and minimum y values. """
27
27
xs = landmarks [:,0 ]
28
28
ys = landmarks [:,1 ]
29
29
lm_x_max , lm_x_min = np .argmax (xs ), np .argmin (xs )
@@ -183,10 +183,17 @@ def map_gesture(gestures, finger_states, landmarks, wrist_angle, direction, boun
183
183
return detected_gesture
184
184
185
185
186
+ def draw_transparent_box (img , pt1 , pt2 , alpha = 0.5 , beta = 0.5 ):
187
+ sub_img = img [pt1 [1 ]:pt2 [1 ], pt1 [0 ]:pt2 [0 ]]
188
+ white_rect = np .ones (sub_img .shape , dtype = np .uint8 ) * 255
189
+ res = cv2 .addWeighted (sub_img , alpha , white_rect , beta , 1.0 )
190
+ img [pt1 [1 ]:pt2 [1 ], pt1 [0 ]:pt2 [0 ]] = res
191
+
192
+
186
193
def draw_fingertips (landmarks , finger_states , img ):
187
194
""" Draw fingertips by finger states. """
188
195
w = img .shape [1 ]
189
- r = int (w / 60 )
196
+ r = int (w / 100 )
190
197
for i in range (5 ):
191
198
fingertip = landmarks [4 * (i + 1 )]
192
199
color = THUMB_STATES [finger_states [i ]][1 ] if i == 0 else NON_THUMB_STATES [finger_states [i ]][1 ]
@@ -197,31 +204,72 @@ def draw_fingertips(landmarks, finger_states, img):
197
204
def draw_bounding_box (landmarks , detected_gesture , img ):
198
205
""" Draw a bounding box of detected hand with gesture label. """
199
206
w = img .shape [1 ]
200
- tor = int (w / 30 )
207
+ tor = int (w / 40 )
201
208
202
209
xs = landmarks [:,0 ]
203
210
ys = landmarks [:,1 ]
204
211
x_max , x_min = np .max (xs ), np .min (xs )
205
212
y_max , y_min = np .max (ys ), np .min (ys )
213
+
214
+ draw_transparent_box (img , (x_min - tor ,y_min - tor - 40 ), (x_max + tor ,y_min - tor ))
215
+
206
216
cv2 .rectangle (img , (x_min - tor ,y_min - tor ), (x_max + tor ,y_max + tor ),
207
- BOX_COLOR , 2 , lineType = cv2 .LINE_AA )
208
- cv2 .rectangle (img , (x_min - tor ,y_min - tor - 40 ), (x_max + tor ,y_min - tor ),
209
- BOX_COLOR , - 1 , lineType = cv2 .LINE_AA )
217
+ LINE_COLOR , 1 , lineType = cv2 .LINE_AA )
210
218
cv2 .putText (img , f'{ detected_gesture } ' , (x_min - tor + 5 ,y_min - tor - 10 ), 0 , 1 ,
211
- LINE_COLOR , 3 , lineType = cv2 .LINE_AA )
219
+ TEXT_COLOR , 3 , lineType = cv2 .LINE_AA )
220
+
221
+
222
+ def display_hand_info (img , hand ):
223
+ """ Display hand information. """
224
+ w = img .shape [1 ]
225
+ tor = int (w / 40 )
226
+
227
+ landmarks = hand ['landmarks' ]
228
+ label = hand ['label' ]
229
+ wrist_angle = hand ['wrist_angle' ]
230
+ direction = hand ['direction' ]
231
+ facing = hand ['facing' ]
232
+
233
+ xs = landmarks [:,0 ]
234
+ ys = landmarks [:,1 ]
235
+ x_max , x_min = np .max (xs ), np .min (xs )
236
+ y_max , y_min = np .max (ys ), np .min (ys )
237
+
238
+ cv2 .rectangle (img , (x_min - tor ,y_min - tor ), (x_max + tor ,y_max + tor ),
239
+ LINE_COLOR , 1 , lineType = cv2 .LINE_AA )
240
+ cv2 .putText (img , f'LABEL: { label } hand' , (x_min - tor ,y_min - 4 * tor - 10 ), 0 , 0.6 ,
241
+ LINE_COLOR , 2 , lineType = cv2 .LINE_AA )
242
+ cv2 .putText (img , f'DIRECTION: { direction } ' , (x_min - tor ,y_min - 3 * tor - 10 ), 0 , 0.6 ,
243
+ LINE_COLOR , 2 , lineType = cv2 .LINE_AA )
244
+ cv2 .putText (img , f'FACING: { facing } ' , (x_min - tor ,y_min - 2 * tor - 10 ), 0 , 0.6 ,
245
+ LINE_COLOR , 2 , lineType = cv2 .LINE_AA )
246
+ cv2 .putText (img , f'WRIST ANGLE: { round (wrist_angle ,1 )} ' , (x_min - tor ,y_min - tor - 10 ),
247
+ 0 , 0.6 , LINE_COLOR , 2 , lineType = cv2 .LINE_AA )
248
+
212
249
213
250
214
251
#########################################################################
215
252
# below functions are specifically for volume control, need check later #
216
253
#########################################################################
217
254
218
- def draw_vol_bar (img , vol_bar , vol , bar_x_range ):
255
+ def draw_vol_bar (img , pt1 , pt2 , vol_bar , vol , fps , bar_x_range , activated ):
219
256
""" Draw a volume bar. """
220
- cv2 .rectangle (img , (bar_x_range [0 ],20 ), (bar_x_range [1 ],40 ),
257
+ draw_transparent_box (img , pt1 , pt2 )
258
+
259
+ cv2 .putText (img , f'FPS: { int (fps )} ' , (50 ,50 ), 0 , 0.8 ,
260
+ TEXT_COLOR , 2 , lineType = cv2 .LINE_AA )
261
+ if activated :
262
+ cv2 .putText (img , f'Activated!' , (50 ,90 ), 0 , 0.8 ,
263
+ BAR_COLOR , 2 , lineType = cv2 .LINE_AA )
264
+ else :
265
+ cv2 .putText (img , f'Deactivated!' , (50 ,90 ), 0 , 0.8 ,
266
+ TEXT_COLOR , 2 , lineType = cv2 .LINE_AA )
267
+
268
+ cv2 .rectangle (img , (bar_x_range [0 ],110 ), (bar_x_range [1 ],130 ),
221
269
BAR_COLOR , 1 , lineType = cv2 .LINE_AA )
222
- cv2 .rectangle (img , (bar_x_range [0 ],20 ), (int (vol_bar ),40 ),
270
+ cv2 .rectangle (img , (bar_x_range [0 ],110 ), (int (vol_bar ),130 ),
223
271
BAR_COLOR , - 1 , lineType = cv2 .LINE_AA )
224
- cv2 .putText (img , f'{ int (vol )} ' , (bar_x_range [1 ]+ 10 , 38 ), 0 , 0.8 ,
272
+ cv2 .putText (img , f'{ int (vol )} ' , (bar_x_range [1 ]+ 20 , 128 ), 0 , 0.8 ,
225
273
TEXT_COLOR , 2 , lineType = cv2 .LINE_AA )
226
274
227
275
0 commit comments