-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (40 loc) · 1.54 KB
/
main.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
import cv2
import mediapipe as mp
import pyautogui
cap = cv2.VideoCapture(0)
hand_detactor = mp.solutions.hands.Hands()
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
index_y=0
while True:
_, frame = cap.read()
frame = cv2.flip(frame,1)
frame_height, frame_wdth, _ = frame.shape
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output = hand_detactor.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_wdth)
y=int(landmark.y*frame_height)
if id==8:
cv2.circle(img=frame,center=(x,y),radius=10,color=(0,255,255))
index_x=screen_width/frame_wdth*x
index_y=screen_height/frame_height*y
pyautogui.moveTo(index_x,index_y)
if id==4:
cv2.circle(img=frame,center=(x,y),radius=10,color=(0,255,255))
thumb_x=screen_width/frame_wdth*x
thumb_y=screen_height/frame_height*y
print(abs(thumb_x-index_x))
if abs(thumb_x-index_x)<20:
pyautogui.click()
pyautogui.sleep(1)
cv2.imshow('virtualMouse', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()