Sub topic 2
Detections as the Tracker's Input
Prerequisites and the input contract
This chapter assumes you know basic Python, NumPy, object detection, bounding boxes, vectors, and matrices. The key idea is that a tracker usually does not inspect video pixels directly. A separate detector processes each frame and sends the tracker a list of detected objects.
A detection is a measurement from one frame. It normally contains:
- A bounding box, often written as
[x1, y1, x2, y2]. - A class label, such as
personorcar. - A confidence score between 0 and 1.
- Sometimes an appearance feature vector for later identity matching.
The coordinates describe the top-left corner (x1, y1) and bottom-right corner (x2, y2). The origin is usually the top-left pixel of the frame. The right and downward directions are positive.
The detector produces new measurements at every frame. The tracker must decide whether a new measurement belongs to an existing track or starts a new one.
Choosing which detections to pass on
A detector can produce many weak predictions. A common first step is confidence filtering:
from dataclasses import dataclass
@dataclass
class Detection:
box: tuple[float, float, float, float]
label: str
confidence: float
def prepare_detections(
detections: list[Detection],
frame_width: int,
frame_height: int,
min_confidence: float = 0.4,
) -> list[Detection]:
prepared = []
for detection in detections:
x1, y1, x2, y2 = detection.box
if detection.confidence < min_confidence:
continue
if x2 <= x1 or y2 <= y1:
continue
clipped_box = (
max(0.0, min(x1, frame_width)),
max(0.0, min(y1, frame_height)),
max(0.0, min(x2, frame_width)),
max(0.0, min(y2, frame_height)),
)
if clipped_box[2] <= clipped_box[0] or clipped_box[3] <= clipped_box[1]:
continue
prepared.append(
Detection(clipped_box, detection.label, detection.confidence)
)
return prepared
detections = [
Detection((20, 30, 90, 180), "person", 0.93),
Detection((200, 40, 240, 110), "car", 0.22),
]
print(prepare_detections(detections, frame_width=320, frame_height=240))The confidence threshold controls a trade-off. A high threshold removes false positives, but it can also remove true objects that are partly hidden or far away. A low threshold preserves possible objects, but it gives the tracker more noise.
The best threshold depends on the detector, class, camera, and application. For example, a safety system may prefer recall, which means finding more real objects. A counting system may prefer precision, which means accepting fewer false detections.
Keep the coordinate system consistent
The detector and tracker must use the same coordinate system. Common mistakes include:
- Passing normalized coordinates such as
0.5when the tracker expects pixels. - Swapping
(x, y)with(row, column). - Using
[x, y, width, height]where the tracker expects corner coordinates. - Forgetting to scale boxes after resizing the frame.
- Mixing boxes from different video frames.
Each detection should also retain its class. A person detection should not normally be matched to a car track. Class-aware filtering or matching prevents many impossible assignments.
Exercise
A detector returns [0.25, 0.5, 0.4, 0.9] for a frame that is 800 pixels wide and 600 pixels high. What does this box become if the coordinates are normalized corner coordinates?
Show answer
[200, 300, 320, 540], found by multiplying x coordinates by 800 and y coordinates by 600.