-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualMouse.py
More file actions
47 lines (35 loc) · 1.27 KB
/
VirtualMouse.py
File metadata and controls
47 lines (35 loc) · 1.27 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
import cv2
import mediapipe as mp
import pyautogui
import numpy as np
# --- SETUP ---
pyautogui.PAUSE = 0
pyautogui.FAILSAFE = False
screen_w, screen_h = pyautogui.size()
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
cap = cv2.VideoCapture(0)
# Smoothing variables to reduce jitter
prev_x, prev_y = 0, 0
smoothing = 0.2
while cap.isOpened():
success, frame = cap.read()
if not success: break
frame = cv2.flip(frame, 1)
h, w, _ = frame.shape
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if results.multi_hand_landmarks:
lms = results.multi_hand_landmarks[0].landmark
# Track Index Finger Tip (Landmark 8)
tx, ty = lms[8].x * screen_w, lms[8].y * screen_h
# Apply smoothing
curr_x = prev_x + (tx - prev_x) * smoothing
curr_y = prev_y + (ty - prev_y) * smoothing
pyautogui.moveTo(curr_x, curr_y)
prev_x, prev_y = curr_x, curr_y
# Draw a small pointer on screen for feedback
cv2.circle(frame, (int(lms[8].x * w), int(lms[8].y * h)), 10, (0, 255, 0), -1)
cv2.imshow("Standalone Virtual Mouse", frame)
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release()
cv2.destroyAllWindows()