-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempCodeRunnerFile.py
71 lines (38 loc) · 1.61 KB
/
tempCodeRunnerFile.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
import cv2
import mediapipe as mp
import numpy as np
import pyautogui
cap = cv2.VideoCapture(0)
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_drawing = mp.solutions.drawing_utils
prev_finger_pos = None
SOUND_CHANGE_THRESHOLD = 20
SOUND_CHANGE_AMOUNT = 5
while True:
ret, frame = cap.read()
if not ret:
break
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
height, width, _ = frame.shape
x = int(index_finger_tip.x * width)
y = int(index_finger_tip.y * height)
if prev_finger_pos is None:
prev_finger_pos = (x, y)
finger_movement = y - prev_finger_pos[1]
prev_finger_pos = (x, y)
if abs(finger_movement) > SOUND_CHANGE_THRESHOLD:
if finger_movement < 0:
pyautogui.press('volumeup', presses=SOUND_CHANGE_AMOUNT)
else:
pyautogui.press('volumedown', presses=SOUND_CHANGE_AMOUNT)
cv2.imshow('Hand Gesture Control', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()