-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgames.py
More file actions
100 lines (93 loc) · 3.7 KB
/
Copy pathgames.py
File metadata and controls
100 lines (93 loc) · 3.7 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import cv2
import mediapipe as mp
import pyautogui
import time
import numpy as np
import threading
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(
static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.7,
min_tracking_confidence=0.7
)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
threshold_x = 28
threshold_y = 14
cooldown = 0.25 # Lowered for faster response
last_action_time = [0, 0]
prev_coords = [None, None]
gesture_hold_frames = [0, 0]
hold_required = 3 # Fewer frames for less lag
current_gesture = [None, None]
# Both hands control arrow keys for maximum compatibility
HAND_KEY_MAP = [
{"Left": "left", "Right": "right", "Up": "up", "Down": "down"},
{"Left": "left", "Right": "right", "Up": "up", "Down": "down"}
]
HAND_COLOR = [
(255, 0, 0), # Left hand: blue
(0, 255, 0) # Right hand: green
]
# Optional: play a beep for feedback (comment out if not needed)
def play_beep():
pass # Remove sound for speed and browser focus
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb_frame)
gestures = [None, None]
if results.multi_hand_landmarks and results.multi_handedness:
for idx, (hand_landmarks, handedness) in enumerate(zip(results.multi_hand_landmarks, results.multi_handedness)):
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
h, w, _ = frame.shape
x = int(hand_landmarks.landmark[8].x * w)
y = int(hand_landmarks.landmark[8].y * h)
if prev_coords[idx] is not None:
dx = x - prev_coords[idx][0]
dy = y - prev_coords[idx][1]
if abs(dx) > abs(dy):
if dx > threshold_x:
gestures[idx] = "Right"
elif dx < -threshold_x:
gestures[idx] = "Left"
else:
if dy > threshold_y:
gestures[idx] = "Down"
elif dy < -threshold_y:
gestures[idx] = "Up"
prev_coords[idx] = (x, y)
# Gesture hold logic
if gestures[idx] == current_gesture[idx] and gestures[idx] is not None:
gesture_hold_frames[idx] += 1
else:
gesture_hold_frames[idx] = 1 if gestures[idx] else 0
current_gesture[idx] = gestures[idx]
# Only trigger if gesture is held for required frames
if gestures[idx] and gesture_hold_frames[idx] >= hold_required and (time.time() - last_action_time[idx] > cooldown):
key = HAND_KEY_MAP[idx].get(gestures[idx])
if key:
pyautogui.press(key)
# play_beep() # Uncomment if you want sound feedback
last_action_time[idx] = time.time()
cv2.putText(frame, f"{gestures[idx]} (sent {key})", (10, 50 + idx*40), cv2.FONT_HERSHEY_SIMPLEX, 1, HAND_COLOR[idx], 2)
elif gestures[idx]:
cv2.putText(frame, f"{gestures[idx]}", (10, 50 + idx*40), cv2.FONT_HERSHEY_SIMPLEX, 1, HAND_COLOR[idx], 2)
else:
cv2.putText(frame, "No gesture", (10, 50 + idx*40), cv2.FONT_HERSHEY_SIMPLEX, 1, (128,128,128), 2)
else:
prev_coords = [None, None]
gesture_hold_frames = [0, 0]
current_gesture = [None, None]
cv2.imshow('Both Hands Gesture Control', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
hands.close()
cv2.destroyAllWindows()