-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol mouse pointer with middle and index fingers.py
46 lines (46 loc) · 1.58 KB
/
control mouse pointer with middle and index fingers.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
import cv2
import mediapipe
import pyautogui
import time
start_time = None
capture_hands = mediapipe.solutions.hands.Hands()
drawing_option = mediapipe.solutions.drawing_utils
screen_width, screen_height = pyautogui.size()
camera = cv2.VideoCapture(0)
x1 = y1 = x2 = y2 = 0
while True:
_,image = camera.read()
image_height, image_width, _ = image.shape
image = cv2.flip(image, 1)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
output_hands = capture_hands.process(rgb_image)
all_hands = output_hands.multi_hand_landmarks
if all_hands:
for hand in all_hands:
drawing_option.draw_landmarks(image, hand)
one_hand_landmarks = hand.landmark
for id, lm in enumerate(one_hand_landmarks):
x = int(lm.x * image_width)
y = int(lm.y * image_height)
#print(x,y)
if id==8:
mouse_x = int(2 * screen_width/image_width * x)
mouse_y = int(screen_height/image_height * y)
cv2.circle(image, (x,y),10,(0,255,255))
pyautogui.moveTo(mouse_x, mouse_y)
x1 = x
y1 = y
if id==12:
x2 = x
y2 = y
cv2.circle(image, (x,y),10,(0,255,255))
dist = x2 - x1
print(dist)
if (dist<15):
pyautogui.click()
cv2.imshow("HAND MOVEMENT VIDEO CAPTURE", image)
key = cv2.waitKey(100)
if key == 27:
break
camera.release()
cv2.destroyAllWindows()