Skip to main content

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.

Python NumPy License Status


โœจ 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.


Built with โค๏ธ by ByIbos โ€” extracted from the Auto-ReID Drone Tracker project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hawkeye_tracker-1.0.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hawkeye_tracker-1.0.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

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

Hashes for hawkeye_tracker-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a440f4b0eb7d176486189a48976cf5dc83f7509123419e64cedd3eff1cd11f3
MD5 32973c52bac9429186337245e5f5a27a
BLAKE2b-256 06ca7422f91e5bddda4d3c8708143b11089927f55005d507c85c726bf4a90bb4

See more details on using hashes here.

File details

Details for the file hawkeye_tracker-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hawkeye_tracker-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0aa4ca96eed794a3002178421694359f9ca231912e8bfdbfc6ece47a3193ddc
MD5 da305768cea6e70da629048d71395ebb
BLAKE2b-256 fac6d61db9573c6a44a0ab3318cfaefbff9bef1dabacbc0b0e3e4b5108f0edf6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page