Adaptive 2D Kalman Filter with NIS-based process noise scaling and polynomial trajectory prediction.
Project description
๐ฏ hawkeye
Adaptive 2D Kalman Filter & Trajectory Predictor
Real-time object tracking with NIS-based adaptive process noise โ designed for drones, robotics, and computer vision.
โจ Why hawkeye?
| Feature | hawkeye | filterpy | pykalman |
|---|---|---|---|
| 6-state model (pos + vel + accel) | โ | Manual setup | Manual setup |
| NIS-based adaptive Q matrix | โ | โ | โ |
| Auto maneuver detection | โ | โ | โ |
| Trajectory prediction (polyfit) | โ | โ | โ |
| Confidence scoring | โ | โ | โ |
| Covariance ellipse (visualization) | โ | โ | โ |
| Zero config needed | โ | โ | โ |
| Lightweight (2 files, ~400 lines) | โ | โ | โ |
Most Kalman filter libraries give you the math primitives. hawkeye gives you a ready-to-use 2D tracker that handles real-world situations like sudden maneuvers, occlusions, and noisy sensors out of the box.
๐ฆ Installation
pip install hawkeye
Or install from source:
git clone https://github.com/ByIbos/hawkeye.git
cd hawkeye
pip install -e .
๐ Quick Start
Basic Tracking
from hawkeye import KalmanFilter2D
# Create filter (30 FPS video)
kf = KalmanFilter2D(dt=1/30)
# Initialize with first detection
kf.init_state(x=320, y=240)
# Every frame:
px, py = kf.predict() # Predict next position
fx, fy = kf.update(325, 238) # Correct with measurement
print(f"Position: ({fx:.1f}, {fy:.1f})")
print(f"Speed: {kf.speed:.1f} px/frame")
print(f"Confidence: {kf.confidence:.0%}")
Handling Occlusions (No Measurement)
# When the target is hidden behind an obstacle:
for frame in occluded_frames:
px, py = kf.predict() # Just predict, don't update!
# The Kalman filter will coast using velocity + acceleration
# Confidence will gradually decrease
print(f"Predicted: ({px:.1f}, {py:.1f}) | Conf: {kf.confidence:.0%}")
# When the target reappears:
fx, fy = kf.update(new_x, new_y) # Snap back to reality
Trajectory Prediction
from hawkeye import KalmanFilter2D, TrajectoryPredictor
kf = KalmanFilter2D(dt=1/30)
tp = TrajectoryPredictor(min_points=10, predict_frames=45)
# After tracking for a while...
kf.init_state(100, 200)
for x, y in detections:
kf.predict()
kf.update(x, y)
# Predict future path (1.5 seconds ahead)
future_path = tp.predict(kf.position_history)
print(f"Prediction confidence: {tp.confidence:.0%}")
# Draw the predicted trajectory
for point in future_path:
draw_point(point) # Your visualization function
Adaptive Behavior (Maneuver Detection)
kf = KalmanFilter2D(dt=1/30, process_noise=1.0)
# During normal flight โ filter is tight, smooth output
# During sharp turn โ NIS spikes โ Q auto-scales โ filter loosens
# After maneuver โ Q gradually returns to normal
# You can monitor the adaptation:
print(f"Adaptive scale: {kf.adaptive_scale:.1f}") # 1.0 = normal, >5 = maneuver
print(f"NIS history: {list(kf.nis_history)}")
๐ง API Reference
KalmanFilter2D(dt, process_noise, measurement_noise)
| Parameter | Type | Default | Description |
|---|---|---|---|
dt |
float | 1/30 | Time step between frames (seconds) |
process_noise |
float | 1.0 | Base process noise (higher = more responsive) |
measurement_noise |
float | 5.0 | Measurement noise (higher = smoother) |
Methods
| Method | Returns | Description |
|---|---|---|
init_state(x, y) |
None | Initialize with first measurement |
predict() |
(x, y) | Predict next state |
update(z_x, z_y) |
(x, y) | Correct with measurement |
reset() |
None | Reset to uninitialized |
Properties
| Property | Type | Description |
|---|---|---|
position |
(x, y) | Current position estimate |
velocity |
(vx, vy) | Current velocity (px/frame) |
acceleration |
(ax, ay) | Current acceleration (px/frameยฒ) |
speed |
float | Speed magnitude |
confidence |
float | Confidence score [0.0 - 1.0] |
position_history |
deque | Past positions (max 120) |
covariance_ellipse() |
(w, h, angle) | 95% confidence ellipse |
TrajectoryPredictor(min_points, predict_frames, poly_degree)
| Parameter | Type | Default | Description |
|---|---|---|---|
min_points |
int | 10 | Minimum points needed |
predict_frames |
int | 45 | Frames to predict ahead |
poly_degree |
int | 2 | Polynomial degree (2=quadratic) |
Methods
| Method | Returns | Description |
|---|---|---|
predict(history) |
[(x,y), ...] | Predict future path from position history |
reset() |
None | Clear stored predictions |
๐ฏ Use Cases
- ๐ Drone Tracking โ Track aerial targets with sudden maneuvers
- ๐ค Robot Navigation โ Smooth sensor fusion for SLAM pipelines
- ๐น Video Surveillance โ Persistent object tracking through occlusions
- ๐ฎ Game AI โ Predict player/NPC trajectories
- ๐ฐ๏ธ Satellite Tracking โ 2D angular tracking of orbital objects
๐ How It Works
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ hawkeye Pipeline โ
โ โ
โ Measurement โโโ Innovation โโโ NIS Check โ
โ (x,y) (y=z-Hx) โ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโ โ
โ โ NIS > 5.99? โ โ
โ โ (Maneuver!) โ โ
โ โโโโโฌโโโโโโฌโโโโโโ โ
โ Yes โ โ No โ
โ โ โ โ
โ Scale Q Shrink Q โ
โ * 1.5 * 0.85 โ
โ โ โ โ
โ โ โ โ
โ Kalman Update (K, P, x) โ
โ โ โ
โ โ โ
โ Filtered Position โโโ Output โ
โ โ โ
โ โ โ
โ History Buffer โ
โ โ โ
โ โ โ
โ Trajectory Prediction โ
โ (Polynomial Extrapolation) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ License
MIT License โ use it anywhere, commercially or personally.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hawkeye_tracker-1.0.0.tar.gz.
File metadata
- Download URL: hawkeye_tracker-1.0.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a440f4b0eb7d176486189a48976cf5dc83f7509123419e64cedd3eff1cd11f3
|
|
| MD5 |
32973c52bac9429186337245e5f5a27a
|
|
| BLAKE2b-256 |
06ca7422f91e5bddda4d3c8708143b11089927f55005d507c85c726bf4a90bb4
|
File details
Details for the file hawkeye_tracker-1.0.0-py3-none-any.whl.
File metadata
- Download URL: hawkeye_tracker-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0aa4ca96eed794a3002178421694359f9ca231912e8bfdbfc6ece47a3193ddc
|
|
| MD5 |
da305768cea6e70da629048d71395ebb
|
|
| BLAKE2b-256 |
fac6d61db9573c6a44a0ab3318cfaefbff9bef1dabacbc0b0e3e4b5108f0edf6
|