Skip to content
Projets

À propos

Forecast where tracked objects will move next. A lightweight extension to Ultralytics YOLO that adds Kalman-filtered trajectory prediction; stable, low-jitter, and efficient for real-time systems.

IA et apprentissage automatique

Comment ces chiffres sont mesurés

Progression mesurée des étoiles

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
Historique en cours

Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.

README.md

Original
Real-time object tracking and trajectory forecasting with Ultralytics YOLO

CI PyPI Downloads
Python 3.9+ Ultralytics 8.4.0+ Trajectory Forecasting Blog

Trajectory Forecast is a lightweight Python package for real-time object tracking and trajectory forecasting with Ultralytics YOLO. It tracks objects in video, keeps their motion history, and predicts where they are likely to move next. Use it for trajectory prediction from the command line or directly from Python.

Installation

pip install trajectory-forecast

Quick start

Run tracking and trajectory forecasting on a video:

trajectory-forecast --model yolo26n.pt --source "https://tinyurl.com/2f3yrppv" --show

If you want to save the results

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

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

Any Ultralytics-supported detection model can be used.

Common use cases

  • Vehicle trajectory prediction and traffic analysis
  • Pedestrian movement forecasting
  • Robotics and autonomous-system perception
  • Video surveillance and motion analysis

CLI options

Option Description
--model Ultralytics YOLO model to use
--source Input video, stream, or supported source
--output Path for the processed output video
--config Optional YAML configuration file
--show Display the processed video
--save Save the processed result
--imgsz Override inference image size; smaller values may improve speed but reduce accuracy
--half Request half-precision inference where supported
--device Select inference device, e.g. cpu, 0, or mps; default is automatic
--confidence_floor Minimum detection confidence used for Kalman noise scaling
--confidence_exponent Strength of confidence-based noise scaling; 0 disables it

Python

from tf import run_inference

run_inference(
    model_path="yolo26n.pt",
    source="video.mp4",
    output_path="result.mp4",
)

Configuration

The defaults work without a config file. To customize tracking or forecasting, create a YAML file:

conf: 0.5                      # Minimum detection confidence
tracker: "bytetrack.yaml"      # Tracker used to keep object IDs between frames
classes: [0, 2, 5, 6, 7]       # Object class IDs to detect and track

history: 30                    # Number of previous positions kept per object
min_points: 5                  # Positions required before forecasting starts
forecast_steps: 35             # Number of future frames to predict
min_speed: 1.0                 # Skip forecasting for nearly stationary objects
max_gap_frames: 5              # Keep motion state through short detection gaps

process_noise: 1.0             # Higher values adapt faster to motion changes
measurement_noise: 10.0        # Base measurement noise
confidence_floor: 0.1          # Clamp low scores to avoid unbounded measurement noise
confidence_exponent: 1.0       # 0 disables confidence-aware measurement noise

# Optional inference performance controls (defaults preserve Ultralytics behavior)
imgsz: 640                      # Inference image size; lower may be faster but less accurate
half: false                     # Enable half precision where supported
# device: "0"                   # Optional: "cpu", "0", or "mps"; omitted = auto

forecast_color: [255, 0, 0]    # Forecast path color in BGR format

Then pass it to the CLI:

trajectory-forecast \
  --model yolo26n.pt \
  --source "video.mp4" \
  --config config.yaml

The most useful forecasting options are:

  • forecast_steps: how many future frames to predict.
  • min_points: tracking history required before forecasting starts.
  • min_speed: skips forecasts for nearly stationary objects.
  • max_gap_frames: keeps motion state through short detection gaps.
  • process_noise: controls how quickly the motion estimate adapts.
  • measurement_noise: base measurement noise used by the Kalman filter.
  • confidence_floor and confidence_exponent: control how detection confidence scales measurement noise. Lower-confidence detections receive higher measurement noise, so the filter relies more on its motion prediction. Set the exponent to 0 to disable this behavior.

How forecasting works

YOLO detection → Object tracking → Motion history → Kalman forecasting → Future trajectory

Each tracked object has its own confidence-aware, acceleration-aware Kalman motion state. In every frame, the filter updates position, velocity, and acceleration from the latest tracked detection, then generates future points from that filtered state.

The measurement covariance adapts to each matched detection confidence:

R_t = R_0 / max(confidence, confidence_floor)^confidence_exponent

A high-confidence detection stays close to the configured base measurement noise. A lower-confidence detection increases measurement noise, reducing the influence of a potentially imprecise position measurement. Set confidence_exponent: 0 to restore fixed measurement noise. Detection confidence is a useful reliability signal, but it is not a calibrated estimate of bounding-box localization error; validate these settings on representative videos before relying on them in safety-critical applications.

Short detection gaps retain the motion state for up to max_gap_frames. If the tracker assigns a completely new ID, a new forecast state is created for that track.

The forecasting layer uses float32 state arrays and vectorized future-point generation. The inference loop transfers tracked box coordinates, IDs, confidence and classes to CPU in one batch per frame, avoiding multiple GPU synchronization points, and disables verbose per-frame Ultralytics output. Use --profile to report average per-frame time for video decoding, YOLO inference/tracking, forecasting and drawing, output writing, display, and total processing. Profiling is intended for measurement and adds a small amount of timing overhead.

For fair comparisons, use the same video, model, device, tracker, output options, and warm-up conditions. Compare multiple runs and report median FPS alongside detection/forecast quality. Lowering imgsz or enabling half may improve throughput on supported hardware but can change detection results. The package does not claim a fixed speedup because performance depends on hardware and workload.

Benchmark the tracking and forecasting pipeline

Run the reproducible synthetic microbenchmark from a source checkout:

python benchmarks/benchmark_pipeline.py --tracks 1 10 25 50 100 --frames 300 --warmup 30 --runs 5 --json benchmark-results.json

It reports median and p95 milliseconds per frame for Kalman track updates, future-point generation, and optional OpenCV drawing, with drawing-on and drawing-off scenarios. The synthetic workload uses deterministic trajectories and records the Python, platform, and NumPy versions. Adjust --forecast-steps, --history, --width, and --height to match a workload of interest.

Important: this is a component microbenchmark, not an end-to-end FPS claim. It deliberately excludes YOLO inference, detector/tracker association, video decoding, and video encoding. Results are machine-dependent and should be compared on the same machine with the same arguments. For end-to-end measurements on real footage, use the CLI's --profile option and compare the same model, video, device, tracker, and output settings.

Project structure

tf/
├── cli.py           # Command-line interface
├── config.py        # Configuration
├── drawing.py       # Visualization
├── forecasting.py   # Motion model and future prediction
├── inference.py     # Detection, tracking and forecasting pipeline
├── tracker.py       # Per-track state and history
└── utils.py         # Utilities

Contributing

Contributions and improvements are welcome. Open an issue or pull request if you find a bug or have an idea for improving tracking or forecasting.

For a practical walkthrough, see the trajectory forecasting guide.

Explorer des projets similaires

Chargement…