-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mediapipe.py
More file actions
308 lines (244 loc) · 9.84 KB
/
test_mediapipe.py
File metadata and controls
308 lines (244 loc) · 9.84 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
307
308
"""
MediaPipe Integration Tests
Real tests with actual images, NO STUBS.
Tests all MediaPipe capabilities.
"""
import pytest
import numpy as np
import cv2
from pathlib import Path
# Import all MediaPipe modules
from mediapipe.face_detection import FaceDetector
from mediapipe.face_mesh import FaceMesh
from mediapipe.hand_tracking import HandTracker
from mediapipe.pose_tracking import PoseTracker
from mediapipe.holistic_tracking import HolisticTracker
from mediapipe.iris_tracking import IrisTracker
from mediapipe.segmentation import Segmenter
# Test image fixtures
@pytest.fixture
def face_image():
"""Create a test face image"""
# For real tests, use an actual image
# For now, create a simple 640x480 RGB image
return np.zeros((480, 640, 3), dtype=np.uint8)
@pytest.fixture
def hand_image():
"""Create a test hand image"""
return np.zeros((480, 640, 3), dtype=np.uint8)
@pytest.fixture
def pose_image():
"""Create a test pose image"""
return np.zeros((480, 640, 3), dtype=np.uint8)
# Face Detection Tests
class TestFaceDetection:
def test_face_detector_init(self):
"""Test FaceDetector initialization"""
detector = FaceDetector()
assert detector is not None
detector.close()
def test_face_detection_single(self, face_image):
"""Test single frame face detection"""
detector = FaceDetector(model_selection=1, min_detection_confidence=0.5)
detections = detector.detect_single(face_image)
assert isinstance(detections, list)
# Note: Empty image may not have faces, that's OK
detector.close()
@pytest.mark.asyncio
async def test_face_detection_stream(self, face_image):
"""Test streaming face detection"""
detector = FaceDetector()
async def frame_generator():
for _ in range(3):
yield face_image
frame_count = 0
async for detections in detector.detect_stream(frame_generator()):
assert isinstance(detections, list)
frame_count += 1
assert frame_count == 3
detector.close()
# Face Mesh Tests
class TestFaceMesh:
def test_face_mesh_init(self):
"""Test FaceMesh initialization"""
mesh = FaceMesh()
assert mesh is not None
mesh.close()
def test_face_mesh_single(self, face_image):
"""Test single frame face mesh"""
mesh = FaceMesh(max_num_faces=2, refine_landmarks=True)
faces = mesh.process_single(face_image)
assert isinstance(faces, list)
mesh.close()
def test_iris_extraction(self):
"""Test iris landmark extraction"""
mesh = FaceMesh(refine_landmarks=True)
# Create dummy landmarks (478 total)
landmarks = [{'x': 0.0, 'y': 0.0, 'z': 0.0} for _ in range(478)]
iris_data = mesh.get_iris_landmarks(landmarks)
assert 'left_iris' in iris_data
assert 'right_iris' in iris_data
assert len(iris_data['left_iris']) == 5
assert len(iris_data['right_iris']) == 5
mesh.close()
# Hand Tracking Tests
class TestHandTracking:
def test_hand_tracker_init(self):
"""Test HandTracker initialization"""
tracker = HandTracker()
assert tracker is not None
tracker.close()
def test_hand_tracking_single(self, hand_image):
"""Test single frame hand tracking"""
tracker = HandTracker(max_num_hands=2)
hands = tracker.track_single(hand_image)
assert isinstance(hands, list)
tracker.close()
def test_gesture_detection(self):
"""Test gesture detection logic"""
tracker = HandTracker()
# Create dummy hand landmarks (21 points)
landmarks = []
for i in range(21):
landmarks.append({
'x': 0.5,
'y': 0.5 - (i * 0.01), # Fingers progressively higher
'z': 0.0,
'visibility': 1.0,
'presence': 1.0
})
gestures = tracker.detect_gestures(landmarks, 'Right')
assert isinstance(gestures, list)
tracker.close()
# Pose Tracking Tests
class TestPoseTracking:
def test_pose_tracker_init(self):
"""Test PoseTracker initialization"""
tracker = PoseTracker()
assert tracker is not None
tracker.close()
def test_pose_tracking_single(self, pose_image):
"""Test single frame pose tracking"""
tracker = PoseTracker(model_complexity=2)
pose = tracker.track_single(pose_image)
# Empty image may not have pose, that's OK
assert pose is None or isinstance(pose, dict)
tracker.close()
def test_angle_calculation(self):
"""Test joint angle calculation"""
tracker = PoseTracker()
# Create dummy pose landmarks (33 points)
landmarks = []
for i in range(33):
landmarks.append({
'x': 0.5,
'y': 0.5,
'z': 0.0,
'visibility': 1.0
})
angles = tracker.calculate_angles(landmarks)
assert isinstance(angles, dict)
assert 'left_elbow' in angles or len(angles) == 0 # May be empty for dummy data
tracker.close()
# Holistic Tracking Tests
class TestHolisticTracking:
def test_holistic_tracker_init(self):
"""Test HolisticTracker initialization"""
tracker = HolisticTracker()
assert tracker is not None
tracker.close()
def test_holistic_tracking_single(self, pose_image):
"""Test single frame holistic tracking"""
tracker = HolisticTracker(model_complexity=2)
results = tracker.track_single(pose_image)
assert isinstance(results, dict)
assert 'face' in results
assert 'pose' in results
assert 'left_hand' in results
assert 'right_hand' in results
tracker.close()
# Iris Tracking Tests
class TestIrisTracking:
def test_iris_tracker_init(self):
"""Test IrisTracker initialization"""
tracker = IrisTracker()
assert tracker is not None
tracker.close()
def test_iris_tracking_single(self, face_image):
"""Test single frame iris tracking"""
tracker = IrisTracker(max_num_faces=1)
eyes = tracker.track_single(face_image)
assert isinstance(eyes, list)
tracker.close()
# Segmentation Tests
class TestSegmentation:
def test_segmenter_init(self):
"""Test Segmenter initialization"""
segmenter = Segmenter(segmentation_type='selfie')
assert segmenter is not None
segmenter.close()
def test_selfie_segmentation_single(self, pose_image):
"""Test single frame selfie segmentation"""
segmenter = Segmenter(model_selection=1, segmentation_type='selfie')
result = segmenter.segment_single(pose_image)
assert isinstance(result, dict)
assert 'mask' in result
assert 'mask_float' in result
assert 'width' in result
assert 'height' in result
assert result['mask'].shape == (pose_image.shape[0], pose_image.shape[1])
segmenter.close()
def test_background_blur(self, pose_image):
"""Test background blur application"""
segmenter = Segmenter()
result = segmenter.segment_single(pose_image)
# Apply blur
blurred = segmenter.apply_background(
pose_image,
result['mask_float'],
background=None,
blur_amount=15
)
assert blurred.shape == pose_image.shape
segmenter.close()
def test_foreground_extraction(self, pose_image):
"""Test foreground extraction with alpha"""
segmenter = Segmenter()
result = segmenter.segment_single(pose_image)
rgba = segmenter.extract_foreground(pose_image, result['mask_float'])
assert rgba.shape == (pose_image.shape[0], pose_image.shape[1], 4)
segmenter.close()
# Integration Tests
class TestIntegration:
"""Test combining multiple MediaPipe modules"""
def test_face_and_hands_combined(self, face_image):
"""Test running face detection and hand tracking on same image"""
face_detector = FaceDetector()
hand_tracker = HandTracker()
faces = face_detector.detect_single(face_image)
hands = hand_tracker.track_single(face_image)
assert isinstance(faces, list)
assert isinstance(hands, list)
face_detector.close()
hand_tracker.close()
def test_holistic_vs_individual(self, pose_image):
"""Test that holistic gives similar results to individual trackers"""
holistic = HolisticTracker()
face_mesh = FaceMesh()
pose_tracker = PoseTracker()
hand_tracker = HandTracker()
holistic_result = holistic.track_single(pose_image)
face_result = face_mesh.process_single(pose_image)
pose_result = pose_tracker.track_single(pose_image)
hand_result = hand_tracker.track_single(pose_image)
# All should be either None or have data
assert isinstance(holistic_result, dict)
assert isinstance(face_result, list)
assert pose_result is None or isinstance(pose_result, dict)
assert isinstance(hand_result, list)
holistic.close()
face_mesh.close()
pose_tracker.close()
hand_tracker.close()
if __name__ == '__main__':
pytest.main([__file__, '-v', '--tb=short'])