Reid Extractor
app.infrastructure.tracking.reid_extractorDeterministic, part-based appearance embeddings for object tracking.
See Also
DeepSortStrategy— uses appearance features for associationBotSortStrategy— uses appearance features for conservative re-identification
Layer: Infrastructure. Context: Implements concrete processing and I/O behind application-facing contracts.
ReIDExtractorProtocol
Bases: Protocol
Interface for extracting one optional appearance vector per box.
Note
Implementations may use a hand-crafted descriptor or a learned backbone; callers rely only on input-order preservation and unit-normalized vectors.
Layer: Infrastructure. Context: Implements concrete processing or I/O behind application-facing contracts.
extract_for_boxes(frame: RGBFrame | None, boxes: Sequence[BBoxViewModel]) -> list[np.ndarray | None]
Extract embeddings aligned with boxes, using None for invalid crops.
| PARAMETER | DESCRIPTION |
|---|---|
frame
|
RGB image for the current video frame, or
TYPE:
|
boxes
|
Bounding boxes in frame pixel coordinates.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[ndarray | None]
|
A list with one normalized float32 vector or |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an implementation receives malformed frame data. |
Note
Result positions must correspond exactly to the input box positions.
Example
vectors = extractor.extract_for_boxes(frame, boxes)
PartBasedAppearanceEmbedder
Build compact, illumination-tolerant color and texture descriptors.
| ATTRIBUTE | DESCRIPTION |
|---|---|
embedding_dim |
Requested output size; supported values are 128, 256, and 512.
|
Note
Each crop is resized to a canonical 64-by-128 grid. HSV and CIE-LAB histograms encode chromatic appearance while Sobel orientation histograms preserve local texture and edge structure. The 128 mode prioritizes speed, 256 adds a center-weighted core, and 512 uses a six-region spatial pyramid.
Example
embedder = PartBasedAppearanceEmbedder(embedding_dim=256)
Layer: Infrastructure. Context: Implements concrete processing or I/O behind application-facing contracts.
embedding_dim = embedding_dim
instance-attribute
__init__(embedding_dim: int = 256) -> None
| PARAMETER | DESCRIPTION |
|---|---|
embedding_dim
|
Output dimension, either 128, 256, or 512.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the requested dimension is unsupported. |
Note: No learned weights or network downloads are required.
Example
embedder = PartBasedAppearanceEmbedder(embedding_dim=128)
extract_for_boxes(frame: RGBFrame | None, boxes: Sequence[BBoxViewModel]) -> list[np.ndarray | None]
Extract an appearance descriptor for every usable box crop.
| PARAMETER | DESCRIPTION |
|---|---|
frame
|
RGB image for the current frame, or
TYPE:
|
boxes
|
Boxes to crop in their original input order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[ndarray | None]
|
Unit-normalized float32 vectors, or |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input frame does not have an RGB array shape. |
Note
Coordinates are clamped before cropping, and crops smaller than two pixels in either dimension are rejected.
Example
vectors = embedder.extract_for_boxes(frame, detections)
cosine_similarity(vec_a: np.ndarray, vec_b: np.ndarray) -> float
Return cosine similarity for two equal-length vectors.
| PARAMETER | DESCRIPTION |
|---|---|
vec_a
|
First feature vector.
TYPE:
|
vec_b
|
Second feature vector.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Cosine similarity clamped to the range |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the vectors have different shapes. |
Note
For nonzero inputs, similarity is
If either input norm is at most \(10^{-8}\), the implementation returns zero instead of dividing by zero.
Example
similarity = cosine_similarity(first, second)
cosine_distance(vec_a: np.ndarray, vec_b: np.ndarray) -> float
Return cosine distance, defined as one minus cosine similarity.
| PARAMETER | DESCRIPTION |
|---|---|
vec_a
|
First feature vector.
TYPE:
|
vec_b
|
Second feature vector.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
A distance in |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the vectors have different shapes. |
Note
The implementation uses \(d_{\cos}(\mathbf{a},\mathbf{b}) = 1-\operatorname{sim}(\mathbf{a},\mathbf{b})\). For unit-normalized nonzero vectors, this reduces to \(1-\mathbf{a}^{\mathsf T}\mathbf{b}\). If either input norm is at most \(10^{-8}\), the similarity fallback is zero and the distance is one.
Example
distance = cosine_distance(first, second)
update_ema_embedding(prototype: np.ndarray | None, new_vec: np.ndarray, momentum: float = 0.9) -> np.ndarray
Blend a new unit feature into a normalized exponential-moving prototype.
| PARAMETER | DESCRIPTION |
|---|---|
prototype
|
Prior normalized appearance vector, if any.
TYPE:
|
new_vec
|
Newly observed appearance vector.
TYPE:
|
momentum
|
Weight assigned to the existing prototype in
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
A normalized float32 vector. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If momentum or vector shapes are invalid. |
Note
For prior prototype \(\mathbf{p}\), observation \(\mathbf{x}\), and momentum \(m\), the blend and normalized update are
Inputs are converted to float32; an existing prototype is expected to already be normalized. The denominator's epsilon floor prevents division by zero.
Example
prototype = update_ema_embedding(previous, observation, momentum=0.9)