-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (65 loc) · 2.77 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
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
import cv2
import time
import mediapipe as mp
import pyautogui
cap = cv2.VideoCapture(0)
mphands = mp.solutions.hands
hand_detector = mp.solutions.hands.Hands()
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
index_y = 0
pTime = 0
middle_y = 0
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame_height, frame_width, _ = frame.shape
#convert the frame into rgb color
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,mphands.HAND_CONNECTIONS)
landmarks = hand.landmark
for id, landmark in enumerate(landmarks):
x = int(landmark.x*frame_width)
y = int(landmark.y*frame_height)
#for index finger
if id == 8:
cv2.circle(img=frame,center=(x,y),radius=10,color=(255,255,0),thickness=cv2.FILLED)
index_x = screen_width/frame_width*x
index_y = screen_height/frame_height*y
#for middle finger
if id == 12:
cv2.circle(img=frame,center=(x,y),radius=10,color=(0,0,255),thickness=cv2.FILLED)
middle_x = screen_width/frame_width*x
middle_y = screen_height/frame_height*y
#for thumb finger
if id == 4:
cv2.circle(img=frame,center=(x,y),radius=10,color=(255,0,0),thickness=cv2.FILLED)
thumb_x = screen_width/frame_width*x
thumb_y = screen_height/frame_height*y
#printing the abs distances b/w index vs thumb and middle vs thumb fingers
print('outside', abs(index_y - thumb_y),abs(middle_y-thumb_y))
#checking the distance b/w thumb and index fingers
if abs(index_y - thumb_y) < 55:
pyautogui.click()
pyautogui.sleep(1)
elif abs(middle_y-thumb_y) < 45 :
pyautogui.rightClick()
pyautogui.sleep(1)
elif abs(index_y - thumb_y) < 500:
pyautogui.moveTo(index_x, index_y)
# frame rate
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(frame,str(int(fps)),(20,50),cv2.FONT_HERSHEY_PLAIN,3,(255,0,0),3)
#display the frame
cv2.imshow('Virtual Mouse', frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()