-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_code
More file actions
40 lines (34 loc) · 1.58 KB
/
Copy pathPython_code
File metadata and controls
40 lines (34 loc) · 1.58 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
import cv2
import mediapipe as mp
import pyautogui
cam = cv2.VideoCapture(0)
face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
screen_w, screen_h = pyautogui.size()
while True :
_, frame = cam.read()
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output= face_mesh.process(rgb_frame) #for detecting face
landmark_points = output.multi_face_landmarks
frame_h, frame_w, _ = frame.shape
if landmark_points:
landmarks = landmark_points[0].landmark #for one face only
for id, landmark in enumerate(landmarks[474 : 478]): #only for landmarks in 2d not 3d(z)
x= int(landmark.x * frame_w)
y = int(landmark.y * frame_h)
cv2.circle(frame , (x,y), 3, (0,0,255))
if id ==1:
screen_x = (screen_w+3*frame_w/2)/ frame_w * x
screen_y = (screen_h +3*frame_h/2) /frame_h * y
if screen_x < screen_w + frame_w/4 and screen_y < screen_h + frame_h/4:
pyautogui.moveTo(screen_x-frame_w/4, screen_y-frame_h/4)
left = [landmarks[145], landmarks[159]]
for landmark in left:
x = int(landmark.x * frame_w)
y = int(landmark.y * frame_h)
cv2.circle(frame, (x, y), 3, (0, 255, 0))
if(left[0].y - left[1].y) < 0.009:
pyautogui.click()
pyautogui.sleep(1)
cv2.imshow('eye controlled mouse', frame) #to show somw image.
cv2.waitKey(1) #to make cv2 to wait for 1 sec for key