-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcv_controller_mouse.py
More file actions
73 lines (67 loc) · 2.51 KB
/
cv_controller_mouse.py
File metadata and controls
73 lines (67 loc) · 2.51 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
import cv2
import mediapipe as mp
import pyautogui
import pydirectinput
from math import dist
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
pydirectinput.PAUSE = 0
pydirectinput.FAILSAFE = False
prev_x, prev_y = 0, 0
prev_dx, prev_dy = 0, 0
screen_width, screen_height = pyautogui.size()
acc = 0.9
accd = 0.4
close = 0.12
smooth = 0.8
dragging = False
def is_hand_closed(hand_landmarks):
wrist = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST]
middle_tip = hand_landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP]
distance = dist((wrist.x, wrist.y), (middle_tip.x, middle_tip.y))
return distance < close
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, frame = cap.read()
if not success:
break
frame = cv2.flip(frame, 1)
frame_height, frame_width, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb_frame)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
wrist = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST]
cur_x, cur_y = wrist.x * frame_width, wrist.y * frame_height
if prev_x != 0 and prev_y != 0:
dx = cur_x - prev_x
dy = cur_y - prev_y
velocity_x = dx - prev_dx
velocity_y = dy - prev_dy
if dragging:
scaled_dx = dx * (1 + abs(velocity_x) * accd)
scaled_dy = dy * (1 + abs(velocity_y) * accd)
else:
scaled_dx = dx * (1 + abs(velocity_x) * acc)
scaled_dy = dy * (1 + abs(velocity_y) * acc)
pydirectinput.moveRel(int(smooth*scaled_dx), int(smooth*scaled_dy))
prev_dx, prev_dy = dx, dy
prev_x, prev_y = cur_x, cur_y
if is_hand_closed(hand_landmarks):
if not dragging:
pydirectinput.mouseDown()
dragging = True
else:
if dragging:
pydirectinput.mouseUp()
dragging = False
mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
else:
prev_x, prev_y = 0, 0
prev_dx, prev_dy = 0, 0
cv2.imshow("Hand Mouse Control (Acceleration)", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()