-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.py
More file actions
105 lines (79 loc) · 3 KB
/
Copy pathpointer.py
File metadata and controls
105 lines (79 loc) · 3 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
import autopy
import cv2
import numpy as np
import mediapipe as mp
import time as time
import hand_tracking_mod as htm
import autopy as ap
import os
import math
import pyautogui # Added for drag/drawing
# Initialize camera and screen resolution
cap = cv2.VideoCapture(0)
detector = htm.HandDetector(max_num_hands=1)
wScr, hScr = autopy.screen.size()
smoothtening = 7
plocx, plocy = 0, 0
clocx, clocy = 0, 0
h, w = 480, 640
cap.set(3, w)
cap.set(4, h)
frameR = 100 # Frame region to define the area for movement
cTime = 0
pTime = 0
drawing = False # This stores drag (drawing) state
while True:
# Step 1: Find Hand Landmarks
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findposition(img, draw=False)
# Step 2: Get Tip of Middle Finger and Index Finger
if len(lmList) != 0:
x1, y1 = lmList[8][1:] # Index finger
x2, y2 = lmList[12][1:] # Middle finger
# Step 3: Check which fingers are up
fingers = detector.fingersup()
# ----------------------------------------------------------
# 1 FINGER = MOVE MOUSE
# ----------------------------------------------------------
if fingers[1] == 1 and fingers[2] == 0:
# If drawing was active → stop drawing
if drawing:
pyautogui.mouseUp()
drawing = False
# Convert coordinates
x3 = np.interp(x1, (frameR, w - frameR), (0, wScr))
y3 = np.interp(y1, (frameR, h - frameR), (0, hScr))
# Smooth movement
clocx = plocx + (x3 - plocx) / smoothtening
clocy = plocy + (y3 - plocy) / smoothtening
# FIXED: natural left-right movement
autopy.mouse.move(clocx, clocy)
cv2.rectangle(img, (frameR, frameR), (w - frameR, h - frameR), (255, 0, 0), 2)
cv2.circle(img, (x1, y1), 8, (0, 255, 0), -1)
plocx, plocy = clocx, clocy
# ----------------------------------------------------------
# 2 FINGERS = DRAW / DRAG (Like MS Paint)
# ----------------------------------------------------------
if fingers[1] == 1 and fingers[2] == 1:
# Convert finger coordinates
x3 = np.interp(x1, (frameR, w - frameR), (0, wScr))
y3 = np.interp(y1, (frameR, h - frameR), (0, hScr))
clocx = plocx + (x3 - plocx) / smoothtening
clocy = plocy + (y3 - plocy) / smoothtening
# Start drawing (press left mouse)
if not drawing:
pyautogui.mouseDown() # hold left button
drawing = True
# FIXED: natural left-right movement
autopy.mouse.move(clocx, clocy)
plocx, plocy = clocx, clocy
# ----------------------------------------------------------
# FPS Counter
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (20, 70),
cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 255), 2)
cv2.imshow("Image", img)
cv2.waitKey(1)