Lesson 7 of 9 · 25m
Object Tracking and Temporal State
Use OpenCV trackers, detection-plus-tracking architectures, occlusion handling, and identity state to reason across time instead of frame-by-frame guesses.
A detector sees the world one frame at a time. Users experience objects continuously. That gap is why tracking exists: preserve identity, fill short detection gaps, reduce flicker, and estimate motion. The dangerous assumption is that a tracker knows what it is following. Most trackers know only that some local appearance pattern is still statistically convenient.
Tracking by detection is usually the right default
Modern systems often run a detector every frame or every few frames and associate detections over time. Classic OpenCV trackers remain useful when detection is expensive or objects are simple:
tracker = cv2.TrackerCSRT_create()
ok = tracker.init(frame, bbox)
ok, bbox = tracker.update(frame)CSRT is accurate but expensive; KCF is faster but more fragile; MOSSE is extremely fast and extremely easy to lose. If your OpenCV build lacks TrackerCSRT_create, check whether you installed the contrib modules — many trackers live in opencv-contrib-python.
Track state is more than a rectangle
A minimal track record needs identity, geometry, age, hit streak, missed frames, velocity, and confidence:
track = {
"id": 17,
"bbox": (421, 210, 88, 142),
"hits": 12,
"misses": 1,
"vx": 3.8,
"vy": -0.4,
"last_seen": frame_index,
}The rectangle is a temporary observation. The track is your system-level belief about the object. Separating those concepts is what makes occlusion handling possible.
Association is the hard part
If two people cross paths, appearance changes and bounding boxes overlap. Naive nearest-center assignment swaps identities. Better systems use IoU, motion prediction, appearance embeddings, or a Kalman filter. Even simple motion gating helps:
def iou(a, b):
ax, ay, aw, ah = a
bx, by, bw, bh = b
x1, y1 = max(ax, bx), max(ay, by)
x2, y2 = min(ax + aw, bx + bw), min(ay + ah, by + bh)
inter = max(0, x2 - x1) * max(0, y2 - y1)
union = aw * ah + bw * bh - inter
return inter / union if union else 0.0Association thresholds must reflect frame rate and object speed. At 30 fps, a slow forklift barely moves between frames; at 2 fps, it can jump across the image.
Drift is the default failure mode
Template and correlation trackers drift when the object changes scale, rotates, deforms, becomes occluded, or the background resembles the object. The tracker does not know it failed. Add supervision: redetect periodically, validate against detector output, estimate appearance change, and kill tracks that miss too long.
Occlusion policy is product policy
When an object disappears, how long do you preserve identity? A person behind a pillar for five frames is probably the same person. A forklift missing for five minutes is not a tracking problem anymore; it is a re-identification problem. Encode timeouts and spatial re-entry rules explicitly.
Temporal smoothing can hide reality
Smoothing bounding boxes reduces jitter, but it also lags fast motion. The same is true for confidence smoothing and track deletion delays. If the system controls a machine, latency is not cosmetic. Tune for the operational consequence: counting objects, triggering a robot, measuring speed, or annotating a video are different products.
Field notes
- Always record whether a bbox came from detection, tracking prediction, or interpolation.
- Do not count a track as confirmed after one detection; require a hit streak.
- Cap maximum track lifetime after missed detections. Infinite-memory trackers become ghost generators.
- Identity switches are often worse than brief track loss. Measure both separately.
- Frame rate is part of the algorithm. A tracker tuned at 30 fps may be unusable at 5 fps.
Was this lesson helpful?