Repository navigation
Expand file tree
/
Copy pathsubwaysurf.py
More file actions
85 lines (65 loc) · 2.35 KB
/
Copy pathsubwaysurf.py
File metadata and controls
85 lines (65 loc) · 2.35 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
import cv2
import mediapipe as mp
import pyautogui
import time
mp_hands = mp.solutions.hands
mp_draw = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
last_action_time = 0
ACTION_DELAY = 0.6 # anti spam
def finger_up(lm, tip, pip):
return lm.landmark[tip].y < lm.landmark[pip].y
with mp_hands.Hands(
min_detection_confidence=0.7,
min_tracking_confidence=0.7
) as hands:
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)
action = None
if result.multi_hand_landmarks:
for hand in result.multi_hand_landmarks:
mp_draw.draw_landmarks(frame, hand, mp_hands.HAND_CONNECTIONS)
index_up = finger_up(hand, 8, 6)
middle_up = finger_up(hand, 12, 10)
ring_up = finger_up(hand, 16, 14)
pinky_up = finger_up(hand, 20, 18)
now = time.time()
if now - last_action_time > ACTION_DELAY:
# ☝️ JUMP
if index_up and not middle_up and not ring_up and not pinky_up:
pyautogui.press("up")
action = "JUMP"
# ✊ ROLL
elif not index_up and not middle_up and not ring_up and not pinky_up:
pyautogui.press("down")
action = "ROLL"
# ✌️ LEFT
elif index_up and middle_up and not ring_up and not pinky_up:
pyautogui.press("left")
action = "LEFT"
# 🤟 RIGHT
elif index_up and pinky_up and not middle_up and not ring_up:
pyautogui.press("right")
action = "RIGHT"
if action:
print(action)
last_action_time = now
cv2.putText(
frame,
"FINGER CONTROLLER | ESC to exit",
(20, 40),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
(0, 255, 0),
2
)
cv2.imshow("Finger Controller", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()