kalman
sleap_nn.tracking.mot.kalman
¶
Kalman filters for the MOT trackers (numpy only).
Three constant-velocity box filters, one per parameterization used in the literature:
- :class:
KalmanFilterXYSR-- SORT / OC-SORT:z = [cx, cy, s=area, r=w/h], 7-D state, fixed noise. - :class:
KalmanFilterXYAH-- DeepSORT / StrongSORT / ByteTrack:z = [cx, cy, a=w/h, h], 8-D state, noise scaled by box height. - :class:
KalmanFilterXYWH-- BoT-SORT / Deep OC-SORT:z = [cx, cy, w, h], 8-D state, noise scaled by width (x, w) and height (y, h).
Filters are stateless configuration objects; each track owns its
(mean, cov). All filters share the interface of :class:_KalmanBase, accept
and return boxes as xyxy, clamp degenerate sizes to >= 1 px, keep
covariances symmetric and never produce NaN/inf. The OC-SORT freeze /
observation-centric re-update (ORU) helpers work with every filter.
The measurement model of every filter observes the first four state entries
(H = [I_4 | 0]) and the motion model adds each velocity once per step. The
noise constants follow the published trackers (cited per class); the updates
use the Joseph form of the covariance correction, which is algebraically
identical to the references' P - K S K^T but keeps P positive
semi-definite under rounding.
Classes:
| Name | Description |
|---|---|
KalmanFilterXYAH |
|
KalmanFilterXYSR |
|
KalmanFilterXYWH |
|
Functions:
| Name | Description |
|---|---|
make_kalman_filter |
Return a filter by name: |
oru_replay |
OC-SORT observation-centric re-update (ORU). |
KalmanFilterXYAH
¶
Bases: _KalmanBase
[cx, cy, a, h] constant-velocity filter (DeepSORT, StrongSORT, ByteTrack).
DeepSORT constants with p = 1/20, v = 1/160:
P0 std [2p h, 2p h, 1e-2, 2p h, 10v h, 10v h, 1e-5, 10v h],
Q std [p h, p h, 1e-2, p h, v h, v h, 1e-5, v h],
R std [p h, p h, 1e-1, p h].
P0 uses the first measurement's height, Q and R the current
state's (clamped to >= 1 px). Prediction zeroes va (vh) when
a + va <= 0 (h + vh <= 0); this only fires for degenerate boxes.
Methods:
| Name | Description |
|---|---|
xyxy_from_mean |
Convert state mean(s) to |
z_from_xyxy |
Convert |
Source code in sleap_nn/tracking/mot/kalman.py
xyxy_from_mean(mean)
¶
Convert state mean(s) to xyxy box(es) (h and a*h >= 1 px).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
ndarray
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The box(es) |
Source code in sleap_nn/tracking/mot/kalman.py
z_from_xyxy(xyxy)
¶
Convert xyxy box(es) to [cx, cy, w/h, h] (sides >= 1 px).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xyxy
|
ndarray
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The measurement(s), same leading shape as |
Source code in sleap_nn/tracking/mot/kalman.py
KalmanFilterXYSR
¶
Bases: _KalmanBase
[cx, cy, s, r] filter with 7-D state [cx, cy, s, r, vx, vy, vs] (SORT, OC-SORT).
SORT constants: R = diag(1, 1, 10, 10),
P0 = diag(10, 10, 10, 10, 1e4, 1e4, 1e4),
Q = diag(1, 1, 1, 1, 0.01, 0.01, 1e-4). Prediction zeroes vs when it
would make the area non-positive.
Methods:
| Name | Description |
|---|---|
xyxy_from_mean |
Convert state mean(s) to |
z_from_xyxy |
Convert |
Source code in sleap_nn/tracking/mot/kalman.py
xyxy_from_mean(mean)
¶
Convert state mean(s) to xyxy box(es) (sides >= 1 px).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
ndarray
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The box(es) |
Source code in sleap_nn/tracking/mot/kalman.py
z_from_xyxy(xyxy)
¶
Convert xyxy box(es) to [cx, cy, w*h, w/h] (sides >= 1 px).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xyxy
|
ndarray
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The measurement(s), same leading shape as |
Source code in sleap_nn/tracking/mot/kalman.py
KalmanFilterXYWH
¶
Bases: _KalmanBase
[cx, cy, w, h] constant-velocity filter (BoT-SORT, Deep OC-SORT).
Noise standard deviations: position 1/20 and velocity 1/160 of the
box width (x and w terms) or height (y and h terms).
P0 std [2p w, 2p h, 2p w, 2p h, 10v w, 10v h, 10v w, 10v h] (sizes of
the first measurement), Q std [p w, p h, p w, p h, v w, v h, v w, v h]
and R std [p w, p h, p w, p h] (sizes of the current state), with
p = 1/20, v = 1/160 and sizes clamped to >= 1 px. Like Deep OC-SORT,
prediction zeroes vw (vh) when w + vw <= 0 (h + vh <= 0).
Methods:
| Name | Description |
|---|---|
xyxy_from_mean |
Convert state mean(s) to |
z_from_xyxy |
Convert |
Source code in sleap_nn/tracking/mot/kalman.py
xyxy_from_mean(mean)
¶
Convert state mean(s) to xyxy box(es) (sides >= 1 px).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
ndarray
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The box(es) |
Source code in sleap_nn/tracking/mot/kalman.py
z_from_xyxy(xyxy)
¶
Convert xyxy box(es) to [cx, cy, w, h] (sides >= 1 px).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xyxy
|
ndarray
|
A |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The measurement(s), same leading shape as |
Source code in sleap_nn/tracking/mot/kalman.py
make_kalman_filter(kind, nsa=False)
¶
Return a filter by name: "xywh", "xyah" or "xysr".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
Filter name (case-insensitive). |
required |
nsa
|
bool
|
Enable StrongSORT's NSA measurement-noise scaling. |
False
|
Returns:
| Type | Description |
|---|---|
_KalmanBase
|
A new filter instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in sleap_nn/tracking/mot/kalman.py
oru_replay(kf, frozen_mean, frozen_cov, last_obs_xyxy, new_obs_xyxy, n_gap, conf=None, frozen_steps=1)
¶
OC-SORT observation-centric re-update (ORU).
Restore the state frozen at a track's first miss and replay n_gap steps of
predict + update with virtual observations linearly interpolated (in
cx, cy, w, h) from last_obs_xyxy to new_obs_xyxy; the last virtual
observation is new_obs_xyxy itself. The caller then applies the real
update (the reference applies the real observation twice; kept for fidelity).
Virtual observation j (1 <= j <= n_gap) sits j / n_gap of the way
from the last observation to the new one. The replay updates the state at
every step from frozen_steps on and predicts once between consecutive
steps, so with the reference freeze (frozen_steps=1: the state was saved
right after the first missed step's prediction) it runs n_gap updates and
n_gap - 1 predictions and ends at the new observation's step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kf
|
_KalmanBase
|
The track's filter. |
required |
frozen_mean
|
ndarray
|
State mean saved at the first miss (after that frame's update). |
required |
frozen_cov
|
ndarray
|
State covariance saved at the first miss. |
required |
last_obs_xyxy
|
ndarray
|
Last real observation before the gap. |
required |
new_obs_xyxy
|
ndarray
|
The re-matched observation. |
required |
n_gap
|
int
|
Steps between the last observation and the new one (>= 1). |
required |
conf
|
Optional[float]
|
Detection confidence for NSA filters. |
None
|
frozen_steps
|
int
|
Steps the frozen state had been predicted past
|
1
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
|