-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesture_controller.py
More file actions
47 lines (37 loc) · 1.25 KB
/
gesture_controller.py
File metadata and controls
47 lines (37 loc) · 1.25 KB
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
import cv2
import mediapipe as mp
import numpy as np
import joblib
import pyautogui
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
model = joblib.load("gesture_model.pkl")
cap = cv2.VideoCapture(0)
actions = {
"volume_up": lambda: pyautogui.press("volumeup"),
"pause": lambda: pyautogui.press("space"),
"next": lambda: pyautogui.hotkey("command", "right"), # MacOS next track
}
while True:
ret, frame = cap.read()
if not ret:
continue
frame = cv2.flip(frame, 1)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
coords = []
for lm in hand_landmarks.landmark:
coords.append([lm.x, lm.y, lm.z])
data = np.array(coords).flatten().reshape(1, -1)
gesture = model.predict(data)[0]
cv2.putText(frame, gesture, (30, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
if gesture in actions:
actions[gesture]()
cv2.imshow("Gesture Controller", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()