-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_person_tracker.py
More file actions
501 lines (400 loc) · 16.5 KB
/
multi_person_tracker.py
File metadata and controls
501 lines (400 loc) · 16.5 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""
Multi-person pose tracking using MediaPipe Pose solution.
Wraps single-person detection with player ID tracking.
Supports true multi-person detection via YOLO + MediaPipe pipeline.
"""
import cv2
import mediapipe as mp
import numpy as np
from dataclasses import dataclass
from typing import List, Optional, Tuple, Dict, TYPE_CHECKING
if TYPE_CHECKING:
from multi_person.yolo_detector import YOLOPersonDetector, BoundingBox
@dataclass
class PersonPose:
"""Pose data for a single detected person."""
player_id: int
bbox: Tuple[int, int, int, int] # x, y, width, height in pixels
pose_landmarks: any # MediaPipe NormalizedLandmarkList
pose_world_landmarks: any # MediaPipe LandmarkList (meters)
confidence: float
class MultiPersonTracker:
"""
Tracks people and their poses with unique player IDs.
Currently uses single-person detection (MediaPipe Pose).
Architecture supports future multi-person expansion.
World Coordinate System:
- Origin: hip center (midpoint between left/right hip)
- Units: meters
- X: negative = subject's left, positive = subject's right
- Y: negative = up (above hip), positive = down (below hip)
- Z: negative = away from camera, positive = toward camera
"""
# Landmark indices
NOSE_IDX = 0
LEFT_SHOULDER_IDX = 11
RIGHT_SHOULDER_IDX = 12
LEFT_WRIST_IDX = 15
RIGHT_WRIST_IDX = 16
LEFT_INDEX_IDX = 19
RIGHT_INDEX_IDX = 20
LEFT_HIP_IDX = 23
RIGHT_HIP_IDX = 24
LEFT_FOOT_INDEX_IDX = 31
RIGHT_FOOT_INDEX_IDX = 32
def __init__(
self,
model_complexity: int = 1,
min_detection_confidence: float = 0.5,
min_tracking_confidence: float = 0.5,
max_persons: int = 4,
yolo_detector: Optional['YOLOPersonDetector'] = None
):
"""
Initialize multi-person tracker.
Args:
model_complexity: Pose model complexity (0=lite, 1=full, 2=heavy)
min_detection_confidence: Minimum confidence for pose detection
min_tracking_confidence: Minimum confidence for tracking
max_persons: Maximum number of people to track
yolo_detector: Optional YOLO detector for true multi-person detection
"""
self.mp_pose = mp.solutions.pose
self.max_persons = max_persons
self.yolo_detector = yolo_detector
# Pose detector - use static_image_mode=True for YOLO crops
# (each crop is treated as independent image)
self.pose = self.mp_pose.Pose(
static_image_mode=(yolo_detector is not None),
model_complexity=model_complexity,
enable_segmentation=False,
min_detection_confidence=min_detection_confidence,
min_tracking_confidence=min_tracking_confidence
)
# Player ID tracking
self.next_player_id = 1
self.active_players: Dict[int, Tuple[float, float]] = {} # id -> (cx, cy)
self.player_timeout_frames = 30 # Frames before player ID is recycled
self.player_last_seen: Dict[int, int] = {} # id -> frame_count
self.frame_count = 0
# Store last detection for drawing
self.last_persons: List[PersonPose] = []
def _assign_player_id(self, centroid: Tuple[float, float]) -> int:
"""
Assign a player ID based on centroid proximity.
Uses simple nearest-neighbor matching with distance threshold.
"""
cx, cy = centroid
DISTANCE_THRESHOLD = 150 # pixels
# Find closest existing player
best_id = None
best_distance = float('inf')
for player_id, (px, py) in self.active_players.items():
distance = ((cx - px) ** 2 + (cy - py) ** 2) ** 0.5
if distance < best_distance and distance < DISTANCE_THRESHOLD:
best_distance = distance
best_id = player_id
if best_id is not None:
# Update existing player position
self.active_players[best_id] = (cx, cy)
self.player_last_seen[best_id] = self.frame_count
return best_id
else:
# Assign new player ID
new_id = self.next_player_id
self.next_player_id += 1
self.active_players[new_id] = (cx, cy)
self.player_last_seen[new_id] = self.frame_count
return new_id
def _cleanup_stale_players(self):
"""Remove players not seen for a while."""
stale_ids = [
pid for pid, last_frame in self.player_last_seen.items()
if self.frame_count - last_frame > self.player_timeout_frames
]
for pid in stale_ids:
del self.active_players[pid]
del self.player_last_seen[pid]
def process_frame(self, frame: np.ndarray) -> List[PersonPose]:
"""
Process a frame and return poses for all detected people.
Args:
frame: BGR image from OpenCV
Returns:
List of PersonPose objects for each detected person
"""
self.frame_count += 1
self._cleanup_stale_players()
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
height, width = frame.shape[:2]
persons = []
# Single-person detection
results = self.pose.process(frame_rgb)
if results.pose_landmarks:
# Calculate centroid from hip landmarks for player ID
left_hip = results.pose_landmarks.landmark[self.LEFT_HIP_IDX]
right_hip = results.pose_landmarks.landmark[self.RIGHT_HIP_IDX]
cx = (left_hip.x + right_hip.x) / 2 * width
cy = (left_hip.y + right_hip.y) / 2 * height
player_id = self._assign_player_id((cx, cy))
person = PersonPose(
player_id=player_id,
bbox=(0, 0, width, height), # Full frame for single-person
pose_landmarks=results.pose_landmarks,
pose_world_landmarks=results.pose_world_landmarks,
confidence=1.0
)
persons.append(person)
self.last_persons = persons
return persons
def process_frame_multi(self, frame: np.ndarray) -> List[PersonPose]:
"""
Process frame with true multi-person detection using YOLO + MediaPipe.
If yolo_detector is not configured, falls back to single-person detection.
Pipeline:
1. YOLO detects person bounding boxes
2. For each box, crop the person region
3. Run MediaPipe Pose on each crop
4. Transform landmarks back to original frame coordinates
5. Assign/track player IDs
Args:
frame: BGR image from OpenCV
Returns:
List of PersonPose objects for each detected person, sorted by x-position
"""
if self.yolo_detector is None:
return self.process_frame(frame)
self.frame_count += 1
self._cleanup_stale_players()
height, width = frame.shape[:2]
persons = []
# Step 1: YOLO person detection (returns boxes sorted by x-center)
bboxes = self.yolo_detector.detect(frame, max_persons=self.max_persons)
for bbox in bboxes:
# Step 2: Crop person region with padding
pad = 20 # Padding pixels
x1 = max(0, bbox.x - pad)
y1 = max(0, bbox.y - pad)
x2 = min(width, bbox.x + bbox.w + pad)
y2 = min(height, bbox.y + bbox.h + pad)
crop = frame[y1:y2, x1:x2]
if crop.size == 0:
continue
# Step 3: Run MediaPipe Pose on crop
crop_rgb = cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)
results = self.pose.process(crop_rgb)
if not results.pose_landmarks:
continue
# Step 4: Transform normalized landmarks to original frame coordinates
transformed_landmarks = self._transform_landmarks_to_frame(
results.pose_landmarks,
x1, y1, x2 - x1, y2 - y1,
width, height
)
# Step 5: Calculate centroid and assign player ID
left_hip = transformed_landmarks.landmark[self.LEFT_HIP_IDX]
right_hip = transformed_landmarks.landmark[self.RIGHT_HIP_IDX]
cx = (left_hip.x + right_hip.x) / 2 * width
cy = (left_hip.y + right_hip.y) / 2 * height
player_id = self._assign_player_id((cx, cy))
person = PersonPose(
player_id=player_id,
bbox=(bbox.x, bbox.y, bbox.w, bbox.h),
pose_landmarks=transformed_landmarks,
pose_world_landmarks=results.pose_world_landmarks,
confidence=bbox.confidence
)
persons.append(person)
self.last_persons = persons
return persons
def _transform_landmarks_to_frame(
self,
landmarks,
crop_x: int,
crop_y: int,
crop_w: int,
crop_h: int,
frame_w: int,
frame_h: int
):
"""
Transform normalized landmarks from crop coordinates to full frame coordinates.
MediaPipe landmarks are normalized (0-1) relative to their input image.
This transforms them to be normalized relative to the full frame.
Args:
landmarks: MediaPipe NormalizedLandmarkList
crop_x, crop_y: Top-left corner of crop in frame
crop_w, crop_h: Size of crop
frame_w, frame_h: Size of original frame
Returns:
Modified landmarks with coordinates relative to full frame
"""
# Create a copy-like structure by modifying in place
# MediaPipe landmarks are mutable
for landmark in landmarks.landmark:
# Convert from crop-relative (0-1) to crop pixel coords
pixel_x = landmark.x * crop_w
pixel_y = landmark.y * crop_h
# Offset to frame coordinates
frame_pixel_x = pixel_x + crop_x
frame_pixel_y = pixel_y + crop_y
# Normalize to frame (0-1)
landmark.x = frame_pixel_x / frame_w
landmark.y = frame_pixel_y / frame_h
return landmarks
def reset_player_ids(self):
"""Reset all player IDs and tracking state."""
self.next_player_id = 1
self.active_players.clear()
self.player_last_seen.clear()
self.frame_count = 0
def get_wrist_world_coords(
self,
person: PersonPose
) -> Tuple[Optional[Dict], Optional[Dict]]:
"""
Extract left and right wrist world coordinates.
Args:
person: PersonPose object
Returns:
Tuple of (left_wrist_coords, right_wrist_coords)
Each is {"x": float, "y": float, "z": float} or None
"""
if not person.pose_world_landmarks:
return None, None
landmarks = person.pose_world_landmarks.landmark
left_wrist = landmarks[self.LEFT_WRIST_IDX]
right_wrist = landmarks[self.RIGHT_WRIST_IDX]
# Only return if visibility is good
MIN_VISIBILITY = 0.5
left_coords = None
if left_wrist.visibility > MIN_VISIBILITY:
left_coords = {
"x": left_wrist.x,
"y": left_wrist.y,
"z": left_wrist.z
}
right_coords = None
if right_wrist.visibility > MIN_VISIBILITY:
right_coords = {
"x": right_wrist.x,
"y": right_wrist.y,
"z": right_wrist.z
}
return left_coords, right_coords
def get_index_finger_world_coords(
self,
person: PersonPose
) -> Tuple[Optional[Dict], Optional[Dict]]:
"""
Extract left and right index finger tip world coordinates.
Alternative to wrist for more precise drumstick tip tracking.
Args:
person: PersonPose object
Returns:
Tuple of (left_index_coords, right_index_coords)
"""
if not person.pose_world_landmarks:
return None, None
landmarks = person.pose_world_landmarks.landmark
left_index = landmarks[self.LEFT_INDEX_IDX]
right_index = landmarks[self.RIGHT_INDEX_IDX]
MIN_VISIBILITY = 0.3 # Index fingers often have lower visibility
left_coords = None
if left_index.visibility > MIN_VISIBILITY:
left_coords = {
"x": left_index.x,
"y": left_index.y,
"z": left_index.z
}
right_coords = None
if right_index.visibility > MIN_VISIBILITY:
right_coords = {
"x": right_index.x,
"y": right_index.y,
"z": right_index.z
}
return left_coords, right_coords
def get_nose_position(self, person: PersonPose) -> Optional[Tuple[int, int]]:
"""Get nose position in pixel coordinates for label placement."""
if not person.pose_landmarks:
return None
nose = person.pose_landmarks.landmark[self.NOSE_IDX]
return nose
def get_body_references(self, person: PersonPose) -> Optional[Dict]:
"""
Extract normalized body reference points for zone classification.
Uses pose_landmarks (normalized 0-1) for scale-invariant height comparisons.
Returns:
Dict with {"nose_y", "shoulder_y", "hip_y"} or None if landmarks unavailable
"""
if not person.pose_landmarks:
return None
landmarks = person.pose_landmarks.landmark
nose = landmarks[self.NOSE_IDX]
left_shoulder = landmarks[self.LEFT_SHOULDER_IDX]
right_shoulder = landmarks[self.RIGHT_SHOULDER_IDX]
left_hip = landmarks[self.LEFT_HIP_IDX]
right_hip = landmarks[self.RIGHT_HIP_IDX]
# Average shoulders and hips for more stable reference
shoulder_y = (left_shoulder.y + right_shoulder.y) / 2
hip_y = (left_hip.y + right_hip.y) / 2
return {
"nose_y": nose.y,
"shoulder_y": shoulder_y,
"hip_y": hip_y
}
def get_wrist_normalized_y(self, person: PersonPose) -> Optional[Dict]:
"""
Get normalized y-coordinates of wrists for body-relative comparison.
Returns:
Dict with {"left": y, "right": y} or None if landmarks unavailable
"""
if not person.pose_landmarks:
return None
landmarks = person.pose_landmarks.landmark
left_wrist = landmarks[self.LEFT_WRIST_IDX]
right_wrist = landmarks[self.RIGHT_WRIST_IDX]
MIN_VISIBILITY = 0.5
result = {"left": None, "right": None}
if left_wrist.visibility > MIN_VISIBILITY:
result["left"] = left_wrist.y
if right_wrist.visibility > MIN_VISIBILITY:
result["right"] = right_wrist.y
return result
def get_foot_world_coords(
self,
person: PersonPose
) -> Tuple[Optional[Dict], Optional[Dict]]:
"""
Extract left and right foot index world coordinates for kick detection.
Args:
person: PersonPose object
Returns:
Tuple of (left_foot_coords, right_foot_coords)
Each is {"x": float, "y": float, "z": float} or None
"""
if not person.pose_world_landmarks:
return None, None
landmarks = person.pose_world_landmarks.landmark
left_foot = landmarks[self.LEFT_FOOT_INDEX_IDX]
right_foot = landmarks[self.RIGHT_FOOT_INDEX_IDX]
MIN_VISIBILITY = 0.3 # Feet often have lower visibility
left_coords = None
if left_foot.visibility > MIN_VISIBILITY:
left_coords = {
"x": left_foot.x,
"y": left_foot.y,
"z": left_foot.z
}
right_coords = None
if right_foot.visibility > MIN_VISIBILITY:
right_coords = {
"x": right_foot.x,
"y": right_foot.y,
"z": right_foot.z
}
return left_coords, right_coords
def close(self):
"""Release resources."""
self.pose.close()