19
19
CAM_W = 640 # camera width
20
20
CAM_H = 480 # camera height
21
21
TEXT_COLOR = (102 ,51 ,0 ) # text color
22
- LINE_COLOR_HIGH = (0 ,0 ,255 ) # landmark color high
23
- LINE_COLOR_LOW = (0 ,255 ,0 ) # landmark color low
24
22
VOL_RANGE = [0 , 100 ] # system volume range
25
23
BAR_X_RANGE = [350 , 550 ] # bar x position range
26
- LEN_RANGE = [20 , 150 ] # range of thumb and index fingertips
27
- STEP_THRESHOLD = [30 , 130 ] # threshold of step control
28
24
29
25
30
26
def vol_control (control = 'continuous' , step = 10 , traj_size = 10 ):
@@ -40,61 +36,64 @@ def vol_control(control='continuous', step=10, traj_size=10):
40
36
ptime = 0
41
37
ctime = 0
42
38
window_name = 'Volume controller'
39
+
43
40
trajectory = list ()
44
- joint1 , joint2 = 4 , 8
41
+ target_gestures = ['Pinch' , 'C shape' ]
42
+ wrist = 0
43
+ thumb_tip , index_tip = 4 , 8
45
44
activated = False
45
+ len_range = None
46
46
47
47
while True :
48
48
_ , img = cap .read ()
49
49
img = cv2 .flip (img , 1 )
50
- detected_gesture = ges_detector .detect_gesture (img , 'single' )
51
-
52
- if detected_gesture == 'C shape' or detected_gesture == 'Pinch' :
53
- ges_detector .draw_gesture_box (img )
54
-
55
- if detected_gesture == 'Pinch' :
56
- activated = True
57
- if activated and detected_gesture == 'C shape' :
58
- activated = False
50
+ gesture = ges_detector .detect_gesture (img , 'single' )
51
+ hands = ges_detector .hand_detector .decoded_hands
52
+
53
+ if gesture :
54
+ hand = hands [- 1 ]
55
+ landmarks = hand ['landmarks' ]
56
+ if gesture in target_gestures :
57
+ ges_detector .draw_gesture_box (img )
58
+ if gesture == target_gestures [0 ]:
59
+ if not activated :
60
+ base_len = two_landmark_distance (landmarks [wrist ],
61
+ landmarks [thumb_tip ])
62
+ len_range = [0.1 * base_len , 0.6 * base_len ]
63
+ step_threshold = [0.2 * base_len , 0.9 * base_len ]
64
+ activated = True
65
+ if activated and gesture == target_gestures [1 ]:
66
+ activated = False
59
67
60
68
if activated :
61
- hands = ges_detector .hand_detector .decoded_hands
62
69
if hands :
63
- # control
64
70
hand = hands [- 1 ]
65
71
landmarks = hand ['landmarks' ]
66
- pt1 = landmarks [joint1 ][:2 ]
67
- pt2 = landmarks [joint2 ][:2 ]
72
+ pt1 = landmarks [thumb_tip ][:2 ]
73
+ pt2 = landmarks [index_tip ][:2 ]
68
74
length = two_landmark_distance (pt1 , pt2 )
69
75
70
76
# continuous control mode
71
77
if control == 'continuous' :
72
78
draw_landmarks (img , pt1 , pt2 )
73
79
finger_states = ges_detector .check_finger_states (hand )
74
80
if finger_states [4 ] > 2 :
75
- vol = np .interp (length , LEN_RANGE , VOL_RANGE )
76
- vol_bar = np .interp (length , LEN_RANGE , BAR_X_RANGE )
81
+ vol = np .interp (length , len_range , VOL_RANGE )
82
+ vol_bar = np .interp (length , len_range , BAR_X_RANGE )
77
83
osascript ("set volume output volume {}" .format (vol ))
78
84
79
85
# step control mode
80
86
if control == 'step' :
81
- if length > STEP_THRESHOLD [1 ]:
82
- draw_landmarks (img , pt1 , pt2 , LINE_COLOR_HIGH )
83
- elif length < STEP_THRESHOLD [0 ]:
84
- draw_landmarks (img , pt1 , pt2 , LINE_COLOR_LOW )
85
- else :
86
- draw_landmarks (img , pt1 , pt2 )
87
-
87
+ draw_landmarks (img , pt1 , pt2 )
88
88
trajectory = update_trajectory (length , trajectory , traj_size )
89
89
up = False
90
90
down = False
91
-
92
- if len (trajectory ) == traj_size and length > STEP_THRESHOLD [1 ]:
91
+ if len (trajectory ) == traj_size and length > step_threshold [1 ]:
93
92
up = check_trajectory (trajectory , direction = 1 )
94
93
if up :
95
94
vol = min (vol + step , VOL_RANGE [1 ])
96
95
osascript ("set volume output volume {}" .format (vol ))
97
- if len (trajectory ) == traj_size and length < STEP_THRESHOLD [0 ]:
96
+ if len (trajectory ) == traj_size and length < step_threshold [0 ]:
98
97
down = check_trajectory (trajectory , direction = - 1 )
99
98
if down :
100
99
vol = max (vol - step , VOL_RANGE [0 ])
0 commit comments