-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreid_tracking.py
More file actions
257 lines (211 loc) Β· 8.92 KB
/
Copy pathreid_tracking.py
File metadata and controls
257 lines (211 loc) Β· 8.92 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
#!/usr/bin/env python3
"""Single-camera object tracking with appearance-based ReID (v1.9.2).
Demonstrates how to enable ReID in mata.track() to improve track-ID recovery
after occlusion or target re-entry β using BotSort's appearance-distance branch.
Supplying ``reid_model`` is sufficient to activate ReID after the C1 fix.
Features demonstrated:
- ``mata.track()`` with ``reid_model`` kwarg
- Inspecting ``Instance.embedding`` vectors
- Low-level persistent tracking via ``TrackingAdapter`` with ``reid_encoder``
- ReID config alias in ``.mata/models.yaml``
Usage (mock mode β no GPU or real models required):
python examples/track/reid_tracking.py
Usage (real video + model):
python examples/track/reid_tracking.py --real examples/videos/cup.mp4
Requirements:
pip install datamata
"""
from __future__ import annotations
import argparse
# ---------------------------------------------------------------------------
# Mock helpers (used when --real is not supplied)
# ---------------------------------------------------------------------------
def _make_mock_detector():
"""Return a minimal detector mock that produces synthetic detections."""
from unittest.mock import Mock
from mata.core.types import Instance, VisionResult
frame_count = {"n": 0}
_LABEL_NAMES = {0: "person", 2: "car"}
def mock_predict(image, **kwargs):
n = frame_count["n"]
frame_count["n"] += 1
x = 80 + n * 3 # person drifts right each frame
return VisionResult(
instances=[
Instance(bbox=(x, 50, x + 90, 290), label=0,
score=0.92, label_name="person"),
Instance(bbox=(350, 110, 510, 270), label=2,
score=0.85, label_name="car"),
],
meta={"frame_idx": n},
)
det = Mock()
det.predict = mock_predict
# Provide id2label so TrackingAdapter can resolve label names
det.id2label = _LABEL_NAMES
return det
def _make_mock_reid_encoder(embedding_dim: int = 128):
"""Return a mock ReID encoder that produces random unit-norm embeddings."""
import numpy as np
from unittest.mock import Mock
def mock_predict(crops):
if not crops:
return np.empty((0, 0), dtype=np.float32)
n = len(crops)
raw = np.random.randn(n, embedding_dim).astype(np.float32)
norms = np.linalg.norm(raw, axis=1, keepdims=True)
return raw / np.where(norms == 0, 1.0, norms)
encoder = Mock()
encoder.predict = mock_predict
return encoder
# ---------------------------------------------------------------------------
# Example 1 β mata.track() one-liner with reid_model
# ---------------------------------------------------------------------------
def run_one_liner(video_path: str, *, real: bool = False) -> None:
"""Show mata.track() one-liner API with reid_model."""
print("\n=== Example 1: mata.track() one-liner with ReID ===\n")
if not real:
import numpy as np
from mata.core.types import Instance, VisionResult
print(" [mock] mata.track() would be called as:\n")
print(" results = mata.track(")
print(' "video.mp4",')
print(' model="facebook/detr-resnet-50",')
print(' tracker="botsort",')
print(' reid_model="openai/clip-vit-base-patch32",')
print(" conf=0.3,")
print(" save=False,")
print(" )\n")
print(" [mock] Simulating 10 frames of tracked objects...\n")
for frame_idx in range(10):
emb = np.random.randn(128).astype(np.float32)
emb /= np.linalg.norm(emb)
result = VisionResult(
instances=[
Instance(
bbox=(80 + frame_idx * 3, 50, 170 + frame_idx * 3, 290),
label=0, score=0.92, label_name="person",
track_id=1, embedding=emb,
),
],
meta={"frame_idx": frame_idx},
)
for inst in result.instances:
emb_str = (
f"shape=({inst.embedding.shape[0]},) "
f"norm={np.linalg.norm(inst.embedding):.4f}"
if inst.embedding is not None else "None"
)
print(
f" Frame {frame_idx:02d} | Track #{inst.track_id} "
f"{inst.label_name:<8} score={inst.score:.2f} | "
f"embedding {emb_str}"
)
print("\n β
ReID embeddings populated in Instance.embedding\n")
return
# Real mode β downloads model on first run
import mata
import numpy as np
results = mata.track(
video_path,
model="facebook/detr-resnet-50",
tracker="botsort",
reid_model="openai/clip-vit-base-patch32",
conf=0.3,
save=False,
)
for frame_idx, result in enumerate(results):
for inst in result.instances:
emb_info = ""
if inst.embedding is not None:
emb_info = (
f"embedding shape=({inst.embedding.shape[0]},) "
f"norm={np.linalg.norm(inst.embedding):.4f}"
)
print(
f" Frame {frame_idx:02d} | Track #{inst.track_id} "
f"{inst.label_name:<10} score={inst.score:.2f} | {emb_info}"
)
# ---------------------------------------------------------------------------
# Example 2 β Low-level TrackingAdapter with reid_encoder
# ---------------------------------------------------------------------------
def run_low_level(num_frames: int = 5) -> None:
"""Show low-level TrackingAdapter with reid_encoder."""
import numpy as np
from mata.adapters.tracking_adapter import TrackingAdapter
print("\n=== Example 2: Low-level TrackingAdapter with reid_encoder ===\n")
mock_detector = _make_mock_detector()
mock_encoder = _make_mock_reid_encoder(embedding_dim=128)
adapter = TrackingAdapter(
mock_detector,
tracker_config={"tracker_type": "botsort"},
frame_rate=25,
reid_encoder=mock_encoder,
)
print(f" TrackingAdapter created. reid_encoder set: {adapter._reid_encoder is not None}")
print(f" tracker.with_reid auto-enabled: {getattr(adapter._tracker, 'with_reid', None)}\n")
for frame_idx in range(num_frames):
frame = np.zeros((480, 640, 3), dtype=np.uint8) # blank synthetic frame
result = adapter.update(frame)
for inst in result.instances:
emb_str = "None"
if inst.embedding is not None:
emb_str = (
f"shape=({inst.embedding.shape[0]},) "
f"norm={np.linalg.norm(inst.embedding):.4f}"
)
label = str(inst.label_name) if inst.label_name is not None else "?"
print(
f" Frame {frame_idx} | Track #{inst.track_id} "
f"{label:<8} | embedding {emb_str}"
)
print("\n β
Low-level ReID tracking complete\n")
# ---------------------------------------------------------------------------
# Example 3 β YAML config alias with reid_model
# ---------------------------------------------------------------------------
def print_config_example() -> None:
"""Print example .mata/models.yaml config for ReID-enabled tracking."""
print("\n=== Example 3: Config alias with reid_model ===\n")
print(" Place this in .mata/models.yaml:\n")
config = """\
models:
track:
smart-cam:
source: "facebook/detr-resnet-50"
tracker: botsort
reid_model: "openai/clip-vit-base-patch32"
with_reid: true # optional here; auto-enabled when reid_model is present
frame_rate: 30
tracker_config:
track_high_thresh: 0.6
appearance_thresh: 0.25
track_buffer: 60
"""
print(config)
print(" Then load with a single call:\n")
print(' import mata')
print(' tracker = mata.load("track", "smart-cam") # ReID auto-enabled from reid_model')
print(" result = tracker.update(frame)\n")
print(" β
Config alias with reid_model demonstrated\n")
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def _parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description="MATA ReID tracking example (v1.9.2)")
p.add_argument(
"--real", metavar="VIDEO",
help="Path to a real video file (downloads model on first run)",
)
return p.parse_args()
def main() -> None:
args = _parse_args()
real = bool(args.real)
video = args.real or "video.mp4"
run_one_liner(video, real=real)
run_low_level()
print_config_example()
print("=" * 60)
print("Done.")
print("See examples/track/cross_camera_reid.py for Valkey cross-camera ReID.")
if __name__ == "__main__":
main()