Skip to main content

A lightweight Python package for real-time object tracking and trajectory forecasting using Ultralytics YOLO.

Project description

Trajectory Forecast

😍😍😍 You can use any Ultralytics-supported model here. 😍😍😍

Trajectory forecast CI Ultralytics 8.4.0 Python 3.10 | Python3.11 | Python 3.12 | 3.13 | 3.14 Visitors PyPI Downloads

trajectory-forecast-usage-code-snippet

Trajectory Forecast is a lightweight, modular extension built on top of Ultralytics YOLO that enables real-time multi-object tracking with future motion prediction. It combines detection, tracking, motion history modeling, and velocity-based forecasting into a unified pipeline that can be used both as a command-line tool and as a Python library. The system is designed for practical computer vision applications such as traffic analytics, surveillance systems, robotics pipelines, and edge AI deployments.

https://github.com/user-attachments/assets/9a1267c2-4ba4-49f6-9802-e80fed5e682f

Installation

pip install trajectory-forecast

Usage

CLI

Run tracking and forecasting on a video.

trajectory-forecast --model yolo26n.pt \
                    --source "https://tinyurl.com/2f3yrppv" \
                    --output result.mp4 --show --save

If you want to adjust tracking and forecasting configuration, create a config.yaml in the directory and paste the mentioned content:

# object detection confidence threshold
conf: 0.5

# tracker selection, i.e., "botsort.yaml" | "bytetrack.yaml"
# "ocsort.yaml", "deepocsort.yaml", "fasttrack.yaml", "tracktrack.yaml"                
tracker: "bytetrack.yaml"

# classes for object detection
classes: [2, 3, 5]

# store tracking history for the number of frames        
history: 40

# minimum tracking history to start calculating forecasting                 
min_points: 8

# total steps for forecasting; larger values extend the prediction horizon.             
forecast_steps: 35

# minimum speed (px/sec) before a forecast is drawn; filters out standing objects.
min_speed: 1.0

# Kalman motion flexibility; higher reacts faster, lower is smoother.
process_noise: 1.0

# Kalman detection trust; higher smooths harder (more noise assumed).
measurement_noise: 10.0

# Forecast point color (B, G, R)           
forecast_color: [255, 0, 0]

# Drawing sizes below are optional. If left out, they auto-scale to the video
# resolution. Set any of them to override.
# line_thickness: 2       # tracking box + trail thickness
# forecast_thickness: 2   # forecast line thickness
# forecast_radius: 6      # forecast marker dot radius
# font_scale: 1.2         # label text size
# font_thickness: 3       # label text thickness
# padding: 8              # label box padding

After that, you can run the code using the command mentioned below.

trajectory-forecast --model yolo26n.pt \
                    --source "https://tinyurl.com/2f3yrppv" \
                    --config "path/to/config.yaml"

Python

from tf import run_inference
from tf.config import ForecastConfig

config = ForecastConfig(
              conf=0.5,
              forecast_steps=50,
              measurement_noise=10.0,
              classes=[0, 2, 5, 6, 7]
            )

run_inference(
          model_path="yolo26s.pt",
          source="https://tinyurl.com/2f3yrppv",
          output_path="output.mp4",
          config=config
        )

Forecasting methodology

Each tracked object is smoothed with a constant-velocity Kalman filter:

  • The filter keeps a running estimate of position and velocity for every track.
  • On each frame it predicts the next state, then corrects it with the new detection.
  • Future positions are forecast by rolling that motion model forward forecast_steps frames.
  • Objects slower than min_speed are skipped so standing targets don't get a forecast.

The earlier version estimated velocity by differencing adjacent frames ((p[i] - p[i-1]) / dt), which divides a tiny per-frame delta by dt = 1/fps and so amplifies detection noise by a factor of fps — the main source of forecast jitter. The Kalman filter instead weighs each noisy detection against the predicted motion, so the estimated velocity, and therefore the forecast, stays steady. On a straight, constant-velocity track with noisy detections this cut the frame-to-frame movement of the forecast endpoint by roughly 30×.

Two knobs control the smoothing: measurement_noise (how much detections are trusted; higher smooths harder) and process_noise (how quickly the motion is allowed to change; higher reacts faster). The filter also keeps predicting through short detection gaps, which helps during brief occlusions.

Project structure

high-level component structure image
tf/
│
├── config.py        # Configuration and resolution-based auto-scaling
├── drawing.py       # Visualization utilities
├── forecasting.py   # Kalman filter and forecasting
├── tracker.py       # Per-track filter and history management
├── inference.py     # Core pipeline
└── cli.py           # Command-line interface
└── utils.py         # For downloading assets from GitHub.

Contributing

The contributions are always welcome. If you would like to extend the forecasting models or improve tracking integration, please open an issue or submit a pull request.

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

trajectory_forecast-1.0.1.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

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

trajectory_forecast-1.0.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file trajectory_forecast-1.0.1.tar.gz.

File metadata

  • Download URL: trajectory_forecast-1.0.1.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trajectory_forecast-1.0.1.tar.gz
Algorithm Hash digest
SHA256 95e636978d4d0593c448162aba89467790dd5d6f008c10d975a983b15f651250
MD5 e1c2c8535fb25b193eacc08bf4f5df13
BLAKE2b-256 4b2ee6717eee7a421541390cf7223f8a3fed0f81dd3ba2669bdd1f689ea0cb04

See more details on using hashes here.

File details

Details for the file trajectory_forecast-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for trajectory_forecast-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 30cbe5750ad8ec913d5a04f1724c59447e76e1557f0769aa42c6290c110433c0
MD5 6b6444bdae1e2fa7bcdc1a7fba275c18
BLAKE2b-256 d2e27d98e60b491154b133794587cceb07f887965f618272f2da802259222e9b

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