-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (63 loc) · 2.48 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
import cv2
import mediapipe as mp
import pyautogui
import threading
from maze import app
from flask import Flask
import time
running = True
def eye_tracking():
global running
cam = cv2.VideoCapture(0)
face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
screen_w, screen_h = pyautogui.size()
cv2.namedWindow('Face Mesh', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('Face Mesh', cv2.WND_PROP_TOPMOST, 1)
while running:
ret, frame = cam.read()
if not ret:
print("Failed to grab frame")
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output = face_mesh.process(rgb_frame)
landmark_points = output.multi_face_landmarks
frame_h, frame_w, _ = frame.shape
if landmark_points:
for face_landmarks in landmark_points:
for id, landmark in enumerate(face_landmarks.landmark[474:478]):
x = int(landmark.x * frame_w)
y = int(landmark.y * frame_h)
cv2.circle(frame, (x, y), 3, (0, 255, 0))
if id == 1:
screen_x = screen_w / frame_w * x
screen_y = screen_h / frame_h * y
pyautogui.moveTo(screen_x, screen_y)
left_eye_landmarks = [face_landmarks.landmark[145], face_landmarks.landmark[159]]
for landmark in left_eye_landmarks:
x = int(landmark.x * frame_w)
y = int(landmark.y * frame_h)
cv2.circle(frame, (x, y), 3, (0, 255, 255))
if (left_eye_landmarks[0].y - left_eye_landmarks[1].y) < 0.01:
pyautogui.click()
pyautogui.sleep(1)
cv2.imshow('Face Mesh', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
running = False
break
cam.release()
cv2.destroyAllWindows()
def flask_thread():
app.run(debug=False, use_reloader=False)
if __name__ == '__main__':
eye_tracking_thread = threading.Thread(target=eye_tracking)
eye_tracking_thread.start()
flask_thread = threading.Thread(target=flask_thread)
flask_thread.start()
try:
while running:
time.sleep(0.1)
except KeyboardInterrupt:
running = False
eye_tracking_thread.join()
flask_thread.join()