Metadata-Version: 2.5
Name: kinematic-tracker
Version: 24.3.0
Summary: Tracker using kinematic Kalman filters.
Project-URL: ChangeLog, https://kovalp.github.io/kinematic-tracker-docs/CHANGELOG
Project-URL: Docs, https://kovalp.github.io/kinematic-tracker-docs/
Author-email: Peter Koval <pkoval@vicomtech.org>
License-Expression: MIT
Requires-Python: >=3.10
Requires-Dist: association-quality-clavia>=0.1.0
Requires-Dist: binary-classification-ratios>=0.3.0
Requires-Dist: buf-view>=0.1.3
Requires-Dist: numpy>=1.22
Requires-Dist: scipy>=1.15.3
Requires-Dist: shapely>=2.1.2
Provides-Extra: gui
Requires-Dist: opencv-python>=4.11.0.86; extra == 'gui'
Provides-Extra: headless
Requires-Dist: opencv-python-headless>=4.11.0.86; extra == 'headless'
Description-Content-Type: text/markdown

# Kinematic Tracker

<img src="https://kovalp.github.io/kinematic-tracker-docs/logo-kinematic-tracker.png" width="96" alt="logo">
<img src="https://kovalp.github.io/kinematic-tracker-docs/logo-vicomtech.svg" width="192" alt="logo">


📖 Live docs: https://kovalp.github.io/kinematic-tracker-docs/

This tracker is based on kinematic Kalman filters. The system state can consist of multiple parts,
each defined by its *kinematic order* and *dimensionality*. For example, to track cuboids
aligned with Cartesian axes, we might decide for a constant-acceleration motion model
for Cartesian coordinates and a constant-position motion model for the sizes of the cuboids.
This choice is encoded by `orders = [3, 1]`. The dimensionality of both parts (coordinates and sizes)
is `3`. Therefore `dimensions = [3, 3]` in our example.

The state of the cuboids aligned with Cartesian axes will have 12 variables.
Internally, block-diagonal states are used, i.e.

```state = p_x, v_x, a_x, p_y, v_y, a_y, p_z, v_z, a_z, s_x, s_y, s_z```

To simplify the input/output processing, one might find attractive a derivative-sorted order

```state = p_x, p_y, p_z, s_x, s_y, s_z, v_x, v_y, v_z, a_x, a_y, a_z```

This package provides utility classes for such conversion (block-diagonal ↔ derivative sorted).

## Process noise models

The tracker supports the following process noise models:

  - White-noise based, adaptive (`finite_diff`): a custom model adapting the noise variance.
  - White-noise based, fixed variance (`const`).
  - Diagonal, fixed variance (`diagonal`).

## Association metrics

A number of association metrics are implemented. Some of the metrics are experimental.

  - Mahalanobis with the innovation covariance (H P Hᵀ + R) -- **default**.
  - Axes-aligned GIoU for segments, bounding boxes, cuboids etc.
  - 3D yaw-only GIoU (volumetric GIoU for cuboids oriented by yaw).
  - Mahalanobis with size-modulated covariance (experimental).
  - IoU-like two-yaw (experimental): based on Mahalanobis distance with diagonal covariance.

## Installation

The tracker needs OpenCV (`cv2`), but does not force a particular OpenCV distribution.
If your project already uses OpenCV (`opencv-python`, `opencv-python-headless`,
`opencv-contrib-python`, ...), just install the tracker and it will use that OpenCV

```shell
pip install kinematic-tracker
```

Otherwise, let the tracker bring OpenCV via an extra: `headless` (no GUI) or `gui` (with HighGUI)

```shell
pip install kinematic-tracker[headless]
```

Do not install several OpenCV distributions side by side: they share the same `cv2` module
and break each other. Importing the tracker without any OpenCV raises an `ImportError`
explaining the options.

## Usage

Track cuboids aligned with Cartesian axes (constant-acceleration positions, constant sizes):

```python
import numpy as np

from kinematic_tracker import NdKkfTracker


tracker = NdKkfTracker(orders_x=[3, 1], num_dims=[3, 3])
tracker.set_measurement_std_dev(0, 0.5)  # positions [m]
tracker.set_measurement_std_dev(1, 1.0)  # sizes [m]

# Two cuboids moving along x; detections are rows (pos_x, pos_y, pos_z, size_x, size_y, size_z).
for step in range(5):
    stamp_ns = step * 100_000_000  # 10 Hz
    det_rz = np.array(
        [[0.0 + 0.5 * step, 0.0, 0.0, 4.5, 2.0, 1.7], [10.0 - 0.3 * step, 5.0, 0.0, 8.7, 2.7, 3.1]]
    )
    ids_r = np.array([7, 15])  # detection IDs (optional)
    tracker.advance(stamp_ns, det_rz, ids_r)

for track in tracker.tracks_c[0]:  # tracks of class 0
    x = track.kkf.kalman_filter.statePost.ravel()  # block-diagonal order, see above
    print(track.creation_id, 'position', x[[0, 3, 6]], 'velocity', x[[1, 4, 7]])
```

More examples are available in the `test/tutorials` folder.
The `test` folder is available in the source package

```shell
pip download kinematic-tracker --no-binary :all: --no-deps
```

## Use kinematic-tracker as a dependency

If the project already depends on an OpenCV distribution, then

```shell
uv add kinematic-tracker
```

Otherwise, add it with an extra providing OpenCV: `headless` or, if you need a GUI-enabled
OpenCV, `gui`

```shell
uv add kinematic-tracker --extra headless
```
