-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_controller_v1.py
More file actions
89 lines (73 loc) · 3.53 KB
/
mouse_controller_v1.py
File metadata and controls
89 lines (73 loc) · 3.53 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
import cv2
import mediapipe as mp
import pyautogui
import math
import time # For better cooldowns
# Setup
hand_detector = mp.solutions.hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
cap = cv2.VideoCapture(0)
# Cooldown variables to avoid spamming shortcuts
last_action_time = 0
action_cooldown = 1.0 # 1 second
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame_height, frame_width, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output = hand_detector.process(rgb_frame)
hands = output.multi_hand_landmarks
curr_time = time.time()
if hands:
for hand in hands:
drawing_utils.draw_landmarks(frame, hand)
landmarks = hand.landmark
# --- 1. DEFINE FINGER STATES (Up or Down) ---
# Index(8), Middle(12), Ring(16), Pinky(20), Thumb(4)
is_thumb_up = landmarks[4].x < landmarks[3].x # For right hand
is_index_up = landmarks[8].y < landmarks[6].y
is_middle_up = landmarks[12].y < landmarks[10].y
is_ring_up = landmarks[16].y < landmarks[14].y
is_pinky_up = landmarks[20].y < landmarks[18].y
# --- 2. GET INDEX COORDINATES FOR MOUSE ---
ix = int(landmarks[8].x * frame_width)
iy = int(landmarks[8].y * frame_height)
screen_x = screen_width/frame_width * ix
screen_y = screen_height/frame_height * iy
# --- 3. GESTURE LOGIC ---
# A. REST MODE (All fingers up) -> Do Nothing
if is_index_up and is_middle_up and is_ring_up and is_pinky_up:
cv2.putText(frame, "REST MODE", (50, 50), 1, 2, (0, 255, 0), 2)
# Check for Snap Windows (Move full open hand left/right)
if curr_time - last_action_time > action_cooldown:
if landmarks[0].x < 0.3: # Hand on left
pyautogui.hotkey('win', 'left')
last_action_time = curr_time
elif landmarks[0].x > 0.7: # Hand on right
pyautogui.hotkey('win', 'right')
last_action_time = curr_time
# B. MOUSE MODE (Only Index UP)
elif is_index_up and not is_middle_up and not is_ring_up:
pyautogui.moveTo(screen_x, screen_y)
# Scrolling inside Mouse Mode:
# If Index is bent slightly compared to middle joint
if landmarks[8].y > landmarks[7].y:
pyautogui.scroll(-100) # Scroll Down
# C. CLICKING (Index and Middle are both UP)
elif is_index_up and is_middle_up and not is_ring_up:
# Use distance between Index (8) and Middle (12) to click
dist = math.hypot(landmarks[8].x - landmarks[12].x, landmarks[8].y - landmarks[12].y)
if dist < 0.05: # Pinch gesture
pyautogui.click()
time.sleep(0.1) # Small buffer, much better than 1s
# D. ALT + TAB (Fist Mode - All fingers down)
elif not is_index_up and not is_middle_up and not is_ring_up:
if curr_time - last_action_time > action_cooldown:
pyautogui.hotkey('alt', 'tab')
last_action_time = curr_time
cv2.imshow("Live Capture", frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()