Skip to content

Commit ccba03d

Browse files
committed
Update vol control
1 parent 622e679 commit ccba03d

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

utils/templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self, label):
8787
'wrist angle': [0, 0.20],
8888
'overlap': [[4, 8], [4, 12]],
8989
'boundary': {0: 0} if label == 'right' else {1: 0}},
90-
'Pinch': {'finger states': [[0], [1], [4], [4], [4]],
90+
'Pinch': {'finger states': [[0], [0, 1], [4], [4], [4]],
9191
'direction': 'up',
9292
'wrist angle': [0.10, 0.50],
9393
'overlap': None,

vol_controller.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
CAM_W = 640 # camera width
2020
CAM_H = 480 # camera height
2121
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
2422
VOL_RANGE = [0, 100] # system volume range
2523
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
2824

2925

3026
def vol_control(control='continuous', step=10, traj_size=10):
@@ -40,61 +36,64 @@ def vol_control(control='continuous', step=10, traj_size=10):
4036
ptime = 0
4137
ctime = 0
4238
window_name = 'Volume controller'
39+
4340
trajectory = list()
44-
joint1, joint2 = 4, 8
41+
target_gestures = ['Pinch', 'C shape']
42+
wrist = 0
43+
thumb_tip, index_tip = 4, 8
4544
activated = False
45+
len_range = None
4646

4747
while True:
4848
_, img = cap.read()
4949
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
5967

6068
if activated:
61-
hands = ges_detector.hand_detector.decoded_hands
6269
if hands:
63-
# control
6470
hand = hands[-1]
6571
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]
6874
length = two_landmark_distance(pt1, pt2)
6975

7076
# continuous control mode
7177
if control == 'continuous':
7278
draw_landmarks(img, pt1, pt2)
7379
finger_states = ges_detector.check_finger_states(hand)
7480
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)
7783
osascript("set volume output volume {}".format(vol))
7884

7985
# step control mode
8086
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)
8888
trajectory = update_trajectory(length, trajectory, traj_size)
8989
up = False
9090
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]:
9392
up = check_trajectory(trajectory, direction=1)
9493
if up:
9594
vol = min(vol + step, VOL_RANGE[1])
9695
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]:
9897
down = check_trajectory(trajectory, direction=-1)
9998
if down:
10099
vol = max(vol - step, VOL_RANGE[0])

0 commit comments

Comments
 (0)