-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
138 lines (96 loc) · 3.47 KB
/
Copy pathtempCodeRunnerFile.py
File metadata and controls
138 lines (96 loc) · 3.47 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import cv2
import mediapipe as mp
import numpy as np
import pyautogui
pyautogui.FAILSAFE = False
import time
BaseOptions = mp.tasks.BaseOptions
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode
options = FaceLandmarkerOptions(
base_options=BaseOptions(model_asset_path="face_landmarker.task"),
running_mode=VisionRunningMode.VIDEO,
num_faces=1
)
landmarker = FaceLandmarker.create_from_options(options)
cap = cv2.VideoCapture(0)
screen_w, screen_h = pyautogui.size()
# ========================
# FULL CAMERA CALIBRATION
# ========================
cv2.namedWindow("Calibration", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("Calibration", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
ret, frame = cap.read()
cam_h, cam_w, _ = frame.shape
calibration_points = [
(80, 80), # top-left
(cam_w - 80, 80), # top-right
(80, cam_h - 80), # bottom-left
(cam_w - 80, cam_h - 80) # bottom-right
]
calibration_data = []
for point in calibration_points:
captured = False
while not captured:
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
result = landmarker.detect_for_video(mp_image, int(time.time()*1000))
# Draw calibration dot ON CAMERA FRAME
cv2.circle(frame, point, 20, (0,0,255), -1)
cv2.putText(frame,
"Look at dot & press SPACE | ESC to quit",
(30,50),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255,255,255),
2)
cv2.imshow("Calibration", frame)
if result.face_landmarks:
landmarks = result.face_landmarks[0]
left_iris = landmarks[468]
right_iris = landmarks[473]
center_x = (left_iris.x + right_iris.x) / 2
center_y = (left_iris.y + right_iris.y) / 2
key = cv2.waitKey(1)
if key == 27: # ESC
cap.release()
cv2.destroyAllWindows()
exit()
if key == 32: # SPACE
calibration_data.append((center_x, center_y))
captured = True
cv2.destroyWindow("Calibration")
print("Calibration Complete!")
# Get boundaries
xs = [p[0] for p in calibration_data]
ys = [p[1] for p in calibration_data]
min_x, max_x = min(xs), max(xs)
min_y, max_y = min(ys), max(ys)
print("Calibration Complete!")
while True:
ret, frame = cap.read()
if not ret:
break
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
result = landmarker.detect_for_video(mp_image, int(time.time()*1000))
if result.face_landmarks:
landmarks = result.face_landmarks[0]
left_iris = landmarks[468]
right_iris = landmarks[473]
center_x = (left_iris.x + right_iris.x) / 2
center_y = (left_iris.y + right_iris.y) / 2
# Normalize
norm_x = (center_x - min_x) / (max_x - min_x)
norm_y = (center_y - min_y) / (max_y - min_y)
norm_x = 1 - norm_x
screen_x = int(norm_x * screen_w)
screen_y = int(norm_y * screen_h)
print("Mapped:", screen_x, screen_y)
pyautogui.moveTo(screen_x, screen_y)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()