supervision/examples/speed_estimation
Jirka Borovec 16814acff3
feat(utils): add `TkImageWindow` to unblock switch to `opencv-python-headless` (#2320)
- Added `sv.ImageWindow`, a Tkinter/Pillow-based desktop image viewer with BGR, grayscale, and BGRA support, keyboard polling, left-click callbacks, context-manager usage, window-state checks, and clean close handling
- Added responsive image resizing with optional aspect-ratio preservation and correctly mapped mouse coordinates after scaling or letterboxing
- Updated compatible runnable examples to use `sv.ImageWindow`, while retaining OpenCV display APIs for worker-thread streaming examples that are incompatible with Tkinter
- Improved `sv.cv2_to_pillow` to support grayscale and BGRA images
- Updated webcam guidance to clarify capture ownership and explicit `VideoCapture` cleanup
- Fixed image-window event handling to prevent stale keypresses, ghost windows, close-time races, and blocked waits after the window closes

---------

Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-07-15 15:06:55 +02:00
..
.gitignore ready for review 2024-01-17 12:11:31 +01:00
README.md chore: update `mdformat` hook arguments to disable wrapping (#2307) 2026-06-09 16:15:05 +02:00
inference_example.py feat(utils): add `TkImageWindow` to unblock switch to `opencv-python-headless` (#2320) 2026-07-15 15:06:55 +02:00
requirements.txt examples/speed_estimation: improve CLI argument parsing (#2056) 2026-02-03 12:02:22 +01:00
ultralytics_example.py feat(utils): add `TkImageWindow` to unblock switch to `opencv-python-headless` (#2320) 2026-07-15 15:06:55 +02:00
video_downloader.py feat: supervison downloader added 2024-01-09 05:15:28 +03:00
yolo_nas_example.py feat(utils): add `TkImageWindow` to unblock switch to `opencv-python-headless` (#2320) 2026-07-15 15:06:55 +02:00

README.md

speed estimation

Colab YouTube

👋 hello

This example performs speed estimation analysis using various object-detection models and ByteTrack - a simple yet effective online multi-object tracking method. It uses the supervision package for multiple tasks such as tracking, annotations, etc.

https://github.com/roboflow/supervision/assets/26109316/d50118c1-2ae4-458d-915a-5d860fd36f71

[!IMPORTANT] Adjust the SOURCE and TARGET configuration if you plan to run a speed estimation script on your video file. Those must be adjusted separately for each camera view. You can learn more from our YouTube tutorial.

💻 install

  • clone repository and navigate to example directory

    git clone --depth 1 -b develop https://github.com/roboflow/supervision.git
    cd supervision/examples/speed_estimation
    
  • setup python environment and activate it [optional]

    uv venv
    source .venv/bin/activate
    
  • install required dependencies

    uv pip install -r requirements.txt
    
  • download vehicles.mp4 file

    python video_downloader.py
    

🛠️ script arguments

  • --roboflow_api_key (optional): The API key for Roboflow services. If not provided directly, the script tries to fetch it from the ROBOFLOW_API_KEY environment variable. Follow this guide to acquire your API KEY.

  • --model_id (optional): Designates the Roboflow model ID to be used. The default value is "yolov8x-1280".

  • --source_weights_path: Required. Specifies the path to the YOLO model's weights file, which is essential for the object detection process. This file contains the data that the model uses to identify objects in the video.

  • --source_video_path: Required. The path to the source video file that will be analyzed. This is the input video on which traffic flow analysis will be performed.

  • --target_video_path: The path to save the output video with annotations. If not specified, the processed video will be displayed in real-time without being saved.

  • --confidence_threshold (optional): Sets the confidence threshold for the YOLO model to filter detections. Default is 0.3. This determines how confident the model should be to recognize an object in the video.

  • --iou_threshold (optional): Specifies the IOU (Intersection Over Union) threshold for the model. Default is 0.7. This value is used to manage object detection accuracy, particularly in distinguishing between different objects.

⚙️ run

  • yolo-nas

    python yolo_nas_example.py \
        --source_video_path data/vehicles.mp4 \
        --target_video_path data/vehicles-result.mp4 \
        --confidence_threshold 0.3 \
        --iou_threshold 0.5
    
  • inference

    python inference_example.py \
        --roboflow_api_key "ROBOFLOW_API_KEY" \
        --source_video_path data/vehicles.mp4 \
        --target_video_path data/vehicles-result.mp4 \
        --confidence_threshold 0.3 \
        --iou_threshold 0.5
    
  • ultralytics

    python ultralytics_example.py \
        --source_video_path data/vehicles.mp4 \
        --target_video_path data/vehicles-result.mp4 \
        --confidence_threshold 0.3 \
        --iou_threshold 0.5
    

© license

This demo integrates two main components, each with its own licensing:

  • ultralytics: The object detection model used in this demo, YOLOv8, is distributed under the AGPL-3.0 license. You can find more details about this license here.

  • supervision: The analytics code that powers the zone-based analysis in this demo is based on the Supervision library, which is licensed under the MIT license. This makes the Supervision part of the code fully open source and freely usable in your projects.