-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_mouse_game_controller.py
More file actions
145 lines (96 loc) · 3.17 KB
/
Copy pathhand_mouse_game_controller.py
File metadata and controls
145 lines (96 loc) · 3.17 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import cv2
import mediapipe as mp
import pyautogui
import numpy as np
import time
import math
import webbrowser
# ---------------- OPEN GAME ----------------
GAME_URL = "https://poki.com"
webbrowser.open(GAME_URL)
time.sleep(5) # give browser time to load
# ---------------- SCREEN ----------------
screen_w, screen_h = pyautogui.size()
# ---------------- MEDIAPIPE ----------------
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7
)
mp_draw = mp.solutions.drawing_utils
# ---------------- CAMERA ----------------
cap = cv2.VideoCapture(0)
cam_w, cam_h = 640, 480
# ---------------- SMOOTHING ----------------
prev_x, prev_y = 0, 0
smooth = 0.2
# ---------------- CLICK ----------------
click_cd = 0
CLICK_DELAY = 0.4
# ---------------- DRAG ----------------
dragging = False
# ---------------- MAIN LOOP ----------------
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, (cam_w, cam_h))
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb)
index_tip = None
thumb_tip = None
middle_tip = None
if result.multi_hand_landmarks:
for hand in result.multi_hand_landmarks:
mp_draw.draw_landmarks(
frame,
hand,
mp_hands.HAND_CONNECTIONS
)
h, w, _ = frame.shape
def get_point(i):
lm = hand.landmark[i]
return int(lm.x * w), int(lm.y * h)
index_tip = get_point(8)
thumb_tip = get_point(4)
middle_tip = get_point(12)
cv2.circle(frame, index_tip, 10, (0,255,0), -1)
# ---------------- CURSOR MOVE ----------------
if index_tip:
x = np.interp(index_tip[0], (0, cam_w), (0, screen_w))
y = np.interp(index_tip[1], (0, cam_h), (0, screen_h))
curr_x = prev_x + (x - prev_x) * smooth
curr_y = prev_y + (y - prev_y) * smooth
pyautogui.moveTo(curr_x, curr_y)
prev_x, prev_y = curr_x, curr_y
# ---------------- GESTURES ----------------
now = time.time()
if index_tip and thumb_tip:
pinch = math.dist(index_tip, thumb_tip)
# click
if pinch < 30 and now > click_cd:
pyautogui.click()
click_cd = now + CLICK_DELAY
cv2.putText(frame, "CLICK", (20,60),
cv2.FONT_HERSHEY_SIMPLEX,
1.5, (0,0,255), 3)
# drag gesture (index + middle pinch)
if index_tip and middle_tip:
pinch2 = math.dist(index_tip, middle_tip)
if pinch2 < 30 and not dragging:
pyautogui.mouseDown()
dragging = True
cv2.putText(frame, "DRAG", (20,120),
cv2.FONT_HERSHEY_SIMPLEX,
1.5, (255,0,0), 3)
elif pinch2 > 40 and dragging:
pyautogui.mouseUp()
dragging = False
# ---------------- DISPLAY ----------------
cv2.imshow("Gesture Game Controller", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()