-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (85 loc) · 3.7 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
86
87
88
89
90
import cv2
import mediapipe as mp
import pyautogui
pyautogui.FAILSAFE = False
cap = cv2.VideoCapture(0)
hand_detector = mp.solutions.hands.Hands()
drawing_utils = mp.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
radius = 30
index_y = 0
index_x = 0
indexf_x = 0
indexf_y = 0
indexfnew_x = 0
indexfnew_y = 0
x = 0
y = 0
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
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
cv2.circle(img=frame, center=(int(frame_width/2), int(frame_height/2)), radius=0, color=(0,0,0), thickness=frame_width+frame_height)
if hands:
for hand in hands:
landmarks = hand.landmark
drawing_utils.draw_landmarks(frame, hand)
for id, landmark in enumerate(landmarks):
x = int(landmark.x*frame_width)
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_width*x
index_y = screen_height/frame_height*y
if abs(index_y - thumb_y) < radius:
pyautogui.keyUp("d")
pyautogui.keyUp('a')
pyautogui.keyUp('s')
pyautogui.keyUp('w')
if id == 4:
cv2.circle(img=frame, center=(x,y), radius=10, color=(0, 0, 255))
thumb_x = screen_width/frame_width*x
thumb_y = screen_height/frame_height*y
pyautogui.moveTo(index_x, index_y+200)
if id == 12:
cv2.circle(img=frame, center=(x,y), radius=10, color=(255, 255, 0))
indexfnew_x = screen_width/frame_width*x
indexfnew_y = screen_height/frame_height*y
print(abs(indexf_y - thumb_y))
if abs(indexf_y - thumb_y) < radius and not indexf_x == 0:
if index_y > (screen_height / 2):
pyautogui.keyDown("s")
pyautogui.keyUp('w')
if index_y < (screen_height / 2):
pyautogui.keyDown("w")
pyautogui.keyUp('s')
if index_x < (screen_width / 2):
pyautogui.keyDown("a")
pyautogui.keyUp('d')
if index_x > (screen_width / 2):
pyautogui.keyDown("d")
pyautogui.keyUp('a')
indexf_x = screen_width/frame_width*x
indexf_y = screen_height/frame_height*y
if id == 16:
cv2.circle(img=frame, center=(x,y), radius=10, color=(255, 0, 255))
index_x = screen_width/frame_width*x
index_y = screen_height/frame_height*y
if abs(index_y - thumb_y) < radius:
pyautogui.leftClick()
if id == 20:
cv2.circle(img=frame, center=(x,y), radius=10, color=(255, 0, 0))
index_x = screen_width/frame_width*x
index_y = screen_height/frame_height*y
if abs(index_y - thumb_y) < radius:
pyautogui.rightClick()
cv2.imshow('Virtual Mouse', frame)
cv2.waitKey(1)
if hands == None:
pyautogui.keyUp("d")
pyautogui.keyUp('a')
pyautogui.keyUp('s')
pyautogui.keyUp('w')