-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (48 loc) · 2.16 KB
/
main.py
File metadata and controls
55 lines (48 loc) · 2.16 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
import cv2
import mediapipe as mp
import pyautogui
cap = cv2.VideoCapture(0)
hand_detector = mp.solutions.hands.Hands()
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
# Global variables to check the positions with the other fingers
thumb_y = 0
index_y = 0
def position_on_screen(x, y):
global screen_width, screen_height
frame_on_screen = [screen_width / frame_width, screen_height / frame_height]
return [int(frame_on_screen[0] * x), int(frame_on_screen[1] * y)]
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1) # flip the frame
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
if hands:
for hand in hands:
drawing_utils.draw_landmarks(frame, hand)
landmarks = hand.landmark
for id, landmark in enumerate(landmarks):
x = int(landmark.x * frame_width)
y = int(landmark.y * frame_height)
if id == 20:
pinky_x, pinky_y = position_on_screen(x, y)
cv2.circle(img=frame, center=(x, y), radius=10, color=(0, 255, 255))
if abs(thumb_y - pinky_y) < 30:
pyautogui.rightClick()
pyautogui.sleep(1) # wait 1 sec for the click
print('right')
if id == 8: # check position of index
cv2.circle(img=frame, center=(x, y), radius=10, color=(0, 255, 255))
index_x, index_y = position_on_screen(x, y)
pyautogui.moveTo(index_x, index_y)
if id == 4: # check position of thumb
cv2.circle(img=frame, center=(x, y), radius=10, color=(0, 255, 255))
thumb_x, thumb_y = position_on_screen(x, y)
if abs(index_y - thumb_y) < 40:
print('left')
pyautogui.click()
pyautogui.sleep(1) # wait 1 sec for the click
cv2.imshow('Virtual Mouse', frame)
cv2.waitKey(1)