-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhandtracking_mouse.py
108 lines (70 loc) · 3.11 KB
/
handtracking_mouse.py
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
import cv2
import mediapipe as mp
import pyautogui as pag
import numpy as np
# Initialize Mediapipe Hand solution
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.1,
min_tracking_confidence=0.1)
mp_drawing = mp.solutions.drawing_utils
#open the camera
cap = cv2.VideoCapture(1)
# error check to make sure the camera is open
if not cap.isOpened():
print("Error")
exit()
# Set the screen resolution (width, height)
screen_width, screen_height = pag.size()
mouseDown = False
#Main loop
while True:
#capture frame by frame from the camera
success, frame = cap.read()
if not success:
break
# Flip the frame horizontally
frame = cv2.flip(frame, 1)
# Convert the frame color from BGR to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the RGB frame with MediaPipe Hands
results = hands.process(rgb_frame)
#frame resoulution
frame_height, frame_width, _ = frame.shape
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# Draw landmarks
mp_drawing.draw_landmarks(frame, hand_landmarks,mp_hands.HAND_CONNECTIONS)
index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
#get the midpoint between the thumb and index finger
midpoint_x = (index_finger_tip.x + thumb_tip.x) /2
midpoint_y = (index_finger_tip.y + thumb_tip.y) /2
# Get the distance between the thumb and index finger
distance = np.sqrt((index_finger_tip.x - thumb_tip.x)**2 + (index_finger_tip.y - thumb_tip.y)**2)
if distance < 0.1 and mouseDown == False:
#mouse down
pag.mouseDown()
mouseDown = True
if distance > 0.3 and mouseDown == True:
#mouse up
pag.mouseUp()
mouseDown = False
if mouseDown:
#draw a circle at the midpoint with radius 10
cv2.circle(frame, (int(midpoint_x*frame_width), int(midpoint_y * frame_height)), 10, (0, 255,0), -1)
else:
#draw a circle at the midpoint with radius 10
cv2.circle(frame, (int(midpoint_x*frame_width), int(midpoint_y * frame_height)), 10, (0, 255,0), 1)
# Map the position to the screen resolution
x_mapped = np.interp(midpoint_x, (0,1), (0, screen_width))
y_mapped = np.interp(midpoint_y, (0,1), (0, screen_height))
# Set the mouse position
pag.moveTo(x_mapped, y_mapped, duration= 0.1)
# Display the resulting frame
cv2.imshow("Medipipe Hands", frame)
cv2.waitKey(1)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()