Skip to content

Commit 2e80d66

Browse files
committed
Refine display
1 parent ccba03d commit 2e80d66

File tree

4 files changed

+85
-39
lines changed

4 files changed

+85
-39
lines changed

gesture.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
BENT_RATIO_THRESH = [0.76, 0.88, 0.85, 0.65]
2424

25-
CAM_W = 640
26-
CAM_H = 480
25+
CAM_W = 1280
26+
CAM_H = 720
2727
TEXT_COLOR = (102,51,0)
2828

2929

@@ -134,7 +134,7 @@ def main(mode='single', target_gesture='all'):
134134
fps = 1 / (ctime - ptime)
135135
ptime = ctime
136136

137-
cv2.putText(img, f'FPS: {int(fps)}', (50,38), 0, 0.8,
137+
cv2.putText(img, f'FPS: {int(fps)}', (50,50), 0, 0.8,
138138
TEXT_COLOR, 2, lineType=cv2.LINE_AA)
139139

140140
cv2.imshow(window_name, img)

hand.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import numpy as np
1313

1414
from utils.utils import check_hand_direction, find_boundary_lm
15-
from utils.utils import calculate_angle
15+
from utils.utils import calculate_angle, display_hand_info
1616

1717

18-
CAM_W = 640
19-
CAM_H = 480
18+
CAM_W = 1280
19+
CAM_H = 720
2020
TEXT_COLOR = (102, 51, 0)
21+
LM_COLOR = (102,255,255)
22+
LINE_COLOR = (51,51,51)
2123

2224

2325
# A hand detector based on mediapipe, it can detect hands and return several features of hands:
@@ -89,12 +91,12 @@ def detect_hands(self, img):
8991

9092
def draw_landmarks(self, img):
9193
w = img.shape[1]
92-
t = int(w / 300)
94+
t = int(w / 500)
9395
if self.results.multi_hand_landmarks:
9496
for landmarks in self.results.multi_hand_landmarks:
9597
self.mp_drawing.draw_landmarks(img, landmarks, self.mp_hands.HAND_CONNECTIONS,
96-
self.mp_drawing.DrawingSpec(color=(51,255,51), thickness=3*t, circle_radius=t),
97-
self.mp_drawing.DrawingSpec(color=(255,255,255), thickness=t, circle_radius=t))
98+
self.mp_drawing.DrawingSpec(color=LM_COLOR, thickness=3*t, circle_radius=t),
99+
self.mp_drawing.DrawingSpec(color=LINE_COLOR, thickness=t, circle_radius=t))
98100

99101

100102
def main(max_hands=2):
@@ -110,16 +112,16 @@ def main(max_hands=2):
110112
img = cv2.flip(img, 1)
111113
detector.detect_hands(img)
112114
detector.draw_landmarks(img)
115+
if detector.decoded_hands:
116+
for hand in detector.decoded_hands:
117+
display_hand_info(img, hand)
113118

114119
ctime = time.time()
115120
fps = 1 / (ctime - ptime)
116121
ptime = ctime
117122

118-
cv2.putText(img, f'FPS: {int(fps)}', (30,40), 0, 0.8,
123+
cv2.putText(img, f'FPS: {int(fps)}', (50,50), 0, 0.8,
119124
TEXT_COLOR , 2, lineType=cv2.LINE_AA)
120-
if detector.decoded_hands:
121-
cv2.putText(img, f'Number of hands detected: {len(detector.decoded_hands)}',
122-
(30,70), 0, 0.8, TEXT_COLOR , 2)
123125

124126
cv2.imshow('Hand detection', img)
125127
key = cv2.waitKey(1)

utils/utils.py

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
TEXT_COLOR = (102,51,0)
6-
BAR_COLOR = (0,255,0)
6+
BAR_COLOR = (51,255,51)
77
LINE_COLOR = (255,255,255)
88
LM_COLOR = (255,51,255)
99
BOX_COLOR = (153,0,153)
@@ -23,7 +23,7 @@
2323

2424

2525
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. """
2727
xs = landmarks[:,0]
2828
ys = landmarks[:,1]
2929
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
183183
return detected_gesture
184184

185185

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+
186193
def draw_fingertips(landmarks, finger_states, img):
187194
""" Draw fingertips by finger states. """
188195
w = img.shape[1]
189-
r = int(w / 60)
196+
r = int(w / 100)
190197
for i in range(5):
191198
fingertip = landmarks[4*(i+1)]
192199
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):
197204
def draw_bounding_box(landmarks, detected_gesture, img):
198205
""" Draw a bounding box of detected hand with gesture label. """
199206
w = img.shape[1]
200-
tor = int(w / 30)
207+
tor = int(w / 40)
201208

202209
xs = landmarks[:,0]
203210
ys = landmarks[:,1]
204211
x_max, x_min = np.max(xs), np.min(xs)
205212
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+
206216
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)
210218
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+
212249

213250

214251
#########################################################################
215252
# below functions are specifically for volume control, need check later #
216253
#########################################################################
217254

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):
219256
""" 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),
221269
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),
223271
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,
225273
TEXT_COLOR, 2, lineType=cv2.LINE_AA)
226274

227275

vol_controller.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
from utils.utils import update_trajectory, check_trajectory
1717

1818

19-
CAM_W = 640 # camera width
20-
CAM_H = 480 # camera height
21-
TEXT_COLOR = (102,51,0) # text color
22-
VOL_RANGE = [0, 100] # system volume range
23-
BAR_X_RANGE = [350, 550] # bar x position range
19+
CAM_W = 1280
20+
CAM_H = 720
21+
TEXT_COLOR = (102,51,0)
22+
ACTI_COLOR = (0,255,0)
23+
VOL_RANGE = [0, 100]
24+
BAR_X_RANGE = [50, CAM_W//5]
2425

2526

2627
def vol_control(control='continuous', step=10, traj_size=10):
@@ -106,15 +107,10 @@ def vol_control(control='continuous', step=10, traj_size=10):
106107
fps = 1 / (ctime - ptime)
107108
ptime = ctime
108109

109-
draw_vol_bar(img, vol_bar, vol, BAR_X_RANGE)
110-
cv2.putText(img, f'FPS: {int(fps)}', (30,40), 0, 0.8,
111-
TEXT_COLOR, 2, lineType=cv2.LINE_AA)
112-
if activated:
113-
cv2.putText(img, f'Volume controller activated!', (100,80), 0, 0.8,
114-
(0,255,0), 2, lineType=cv2.LINE_AA)
115-
else:
116-
cv2.putText(img, f'Volume controller de-activated!', (100,80), 0, 0.8,
117-
(0,0,255), 2, lineType=cv2.LINE_AA)
110+
pt1 = (30,20)
111+
pt2 = (BAR_X_RANGE[1]+100,150)
112+
draw_vol_bar(img, pt1, pt2, vol_bar, vol, fps, BAR_X_RANGE, activated)
113+
118114
cv2.imshow(window_name, img)
119115
key = cv2.waitKey(1)
120116
if key == ord('q'):

0 commit comments

Comments
 (0)