-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbin_test_mediapipe_detection.py
More file actions
306 lines (263 loc) · 10.6 KB
/
Copy pathbin_test_mediapipe_detection.py
File metadata and controls
306 lines (263 loc) · 10.6 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
"""
MediaPipe Face Detection Test
Test application to visualize MediaPipe face landmarks detection.
Usage:
python bin_test_mediapipe_detection.py # Use default camera
python bin_test_mediapipe_detection.py --camera 1 # Use camera device 1
Controls:
- 'b': Toggle background on/off
- 'q': Quit
"""
from time import time
import cv2
import numpy as np
import mediapipe as mp
from mediapipe.tasks.python import vision
from mediapipe.tasks.python.vision import drawing_utils
from mediapipe.tasks.python.vision import drawing_styles
# Landmark Investigation Configuration
# Define groups of landmark indices to display with different colors
LANDMARK_GROUPS = [
# Left eye (from viewer's perspective)
{
'name': 'Left Eye',
'indices': [362, 385, 387, 263, 373, 380],
'color': (0, 255, 0), # Green (BGR)
},
# Right eye (from viewer's perspective)
{
'name': 'Right Eye',
'indices': [33, 160, 158, 133, 153, 144],
'color': (255, 0, 0), # Blue (BGR)
},
# TODO: Left eye corners
# {
# 'name': 'Left Eye Corners',
# 'indices': [362, 263],
# 'color': (0, 255, 255), # Yellow (BGR)
# },
{
'name': 'Left Eye Corners',
'indices': [474, 476],
'color': (0, 255, 255), # Yellow (BGR)
},
# TODO: Right eye corners
# {
# 'name': 'Right Eye Corners',
# 'indices': [33, 133],
# 'color': (255, 255, 0), # Cyan (BGR)
# },
{
'name': 'Right Eye Corners',
'indices': [471, 469],
'color': (255, 255, 0), # Cyan (BGR)
},
# Nose tip and related
{
'name': 'Nose',
'indices': [1, 4, 5, 6],
'color': (0, 0, 255), # Red (BGR)
},
# Mouth corners
{
'name': 'Mouth',
'indices': [61, 291, 0, 17],
'color': (255, 0, 255), # Magenta (BGR)
},
]
def detect_camera_resolution(camera_id: int) -> tuple[int, int]:
"""
Detect the actual resolution of the specified camera.
Args:
camera_id: Camera device ID
Returns:
Tuple of (width, height) in pixels
"""
cap = cv2.VideoCapture(camera_id)
if not cap.isOpened():
print(f"Warning: Could not open camera {camera_id}, using default resolution")
return (640, 480)
# Get the actual resolution
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()
print(f"Detected camera resolution: {width}x{height}")
return (width, height)
def run_face_detection(camera_id: int, model_path: str = './models/face_landmarker.task'):
"""
Run MediaPipe face detection and visualization.
Args:
camera_id: Camera device ID
model_path: Path to MediaPipe face landmarker model
"""
# Detect camera resolution
video_size = detect_camera_resolution(camera_id)
# Setup MediaPipe
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=model_path),
running_mode=VisionRunningMode.VIDEO
)
print(f"Starting camera {camera_id}...")
print("Press 'b' to toggle background, 'c' to toggle contours, 'p' to toggle landmark points, 'q' to quit")
# State
show_background = True
show_landmark_points = False
show_contours = True
with FaceLandmarker.create_from_options(options) as landmarker:
cap = cv2.VideoCapture(camera_id)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, video_size[0])
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, video_size[1])
start_time = time()
while True:
ret, frame = cap.read()
if not ret:
print("Cannot receive frame")
break
# Calculate the timestamp (in milliseconds) from the start
# VIDEO mode requires a strictly increasing timestamp
frame_timestamp_ms = int((time() - start_time) * 1000)
# Process frame with MediaPipe
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
face_landmarker_result = landmarker.detect_for_video(mp_image, frame_timestamp_ms)
face_landmarks_list = face_landmarker_result.face_landmarks
# Use original frame or black background based on toggle
if show_background:
annotated_image = np.copy(frame)
else:
annotated_image = np.zeros_like(frame)
# Loop through the detected faces to visualize
for idx in range(len(face_landmarks_list)):
face_landmarks = face_landmarks_list[idx]
# Draw face mesh and contours if enabled
if show_contours:
# Draw face mesh tesselation
drawing_utils.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=vision.FaceLandmarksConnections.FACE_LANDMARKS_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=drawing_styles
.get_default_face_mesh_tesselation_style()
)
# Draw face contours
drawing_utils.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=vision.FaceLandmarksConnections.FACE_LANDMARKS_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=drawing_styles
.get_default_face_mesh_contours_style()
)
# Draw left iris
drawing_utils.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=vision.FaceLandmarksConnections.FACE_LANDMARKS_LEFT_IRIS,
landmark_drawing_spec=None,
connection_drawing_spec=drawing_styles
.get_default_face_mesh_iris_connections_style()
)
# Draw right iris
drawing_utils.draw_landmarks(
image=annotated_image,
landmark_list=face_landmarks,
connections=vision.FaceLandmarksConnections.FACE_LANDMARKS_RIGHT_IRIS,
landmark_drawing_spec=None,
connection_drawing_spec=drawing_styles
.get_default_face_mesh_iris_connections_style()
)
# Draw landmark points if enabled
if show_landmark_points:
h, w = annotated_image.shape[:2]
for group in LANDMARK_GROUPS:
color = group['color']
for landmark_idx in group['indices']:
if landmark_idx < len(face_landmarks):
landmark = face_landmarks[landmark_idx]
x = int(landmark.x * w)
y = int(landmark.y * h)
# Draw circle
cv2.circle(annotated_image, (x, y), 4, color, -1)
cv2.circle(annotated_image, (x, y), 5, (255, 255, 255), 1)
# Draw index label
cv2.putText(
annotated_image,
str(landmark_idx),
(x + 8, y - 8),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
color,
1,
cv2.LINE_AA
)
# Draw legend
legend_y = 30
for group in LANDMARK_GROUPS:
cv2.putText(
annotated_image,
f"{group['name']}: {group['indices']}",
(10, legend_y),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
group['color'],
1,
cv2.LINE_AA
)
legend_y += 20
cv2.imshow('MediaPipe Face Landmarker', annotated_image)
key = cv2.waitKey(10) & 0xFF
if key == ord('q'):
break
elif key == ord('b'):
show_background = not show_background
status = "ON" if show_background else "OFF"
print(f"Background: {status}")
elif key == ord('c'):
show_contours = not show_contours
status = "ON" if show_contours else "OFF"
print(f"Contours: {status}")
elif key == ord('p'):
show_landmark_points = not show_landmark_points
status = "ON" if show_landmark_points else "OFF"
print(f"Landmark Points: {status}")
cap.release()
cv2.destroyAllWindows()
print("Shutdown complete")
def main():
"""Main entry point."""
import argparse
parser = argparse.ArgumentParser(
description="MediaPipe Face Detection Test",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Controls:
'b' - Toggle background on/off
'c' - Toggle face contours on/off
'p' - Toggle landmark points display
'q' - Quit the application
Examples:
%(prog)s # Use default camera
%(prog)s --camera 1 # Use camera device 1
""",
)
parser.add_argument(
"--camera",
type=int,
default=0,
help="Camera device ID (default: 0)",
)
parser.add_argument(
"--model",
type=str,
default="./models/face_landmarker.task",
help="Path to MediaPipe face landmarker model (default: ./models/face_landmarker.task)",
)
args = parser.parse_args()
run_face_detection(args.camera, args.model)
if __name__ == "__main__":
main()