supervision/examples/traffic_analysis
Onuralp SEZER 212e69b5a4
refactor: 🧪 type hints to use the new union syntax for optional types
- Updated type hints across multiple files to replace `Optional[X]` with `X | None` for better readability and consistency.
- Added `from __future__ import annotations` to files to enable postponed evaluation of type annotations.
- Adjusted function signatures and variable types in various modules including `polygon_zone.py`, `smoother.py`, `transformers.py`, `vlm.py`, and others.
- Ensured compatibility with Python 3.10+ type hinting features.

Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
2025-07-15 16:36:01 +03:00
..
.gitignore fix(pre_commit): 🎨 auto format pre-commit hooks 2024-01-17 11:14:03 +00:00
README.md docs: 📝 git clone examples with single branch and single depth added into example docs 2024-11-19 17:59:58 +03:00
inference_example.py refactor: 🧪 type hints to use the new union syntax for optional types 2025-07-15 16:36:01 +03:00
requirements.txt chore: 🧹 update requirements files to remove version constraints 2025-07-15 12:15:55 +02:00
setup.sh Rename script.sh to setup.sh 2023-10-24 12:37:52 -03:00
ultralytics_example.py refactor: 🧪 type hints to use the new union syntax for optional types 2025-07-15 16:36:01 +03:00

README.md

traffic analysis

👋 hello

This script performs traffic flow analysis using YOLOv8, an object-detection method 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/c9436828-9fbf-4c25-ae8c-60e9c81b3900

💻 install

  • clone repository and navigate to example directory

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

    python3 -m venv venv
    source venv/bin/activate
    
  • install required dependencies

    pip install -r requirements.txt
    
  • download traffic_analysis.pt and traffic_analysis.mov files

    ./setup.sh
    

🛠️ script arguments

  • ultralytics

    • --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 (optional): 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.

  • inference

    • --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 "vehicle-count-in-drone-video/6".

    • --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 (optional): 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

  • ultralytics

    python ultralytics_example.py \
        --source_weights_path data/traffic_analysis.pt \
        --source_video_path data/traffic_analysis.mov \
        --confidence_threshold 0.3 \
        --iou_threshold 0.5 \
        --target_video_path data/traffic_analysis_result.mov
    
  • inference

    python inference_example.py \
        --roboflow_api_key <ROBOFLOW API KEY> \
        --source_video_path data/traffic_analysis.mov \
        --confidence_threshold 0.3 \
        --iou_threshold 0.5 \
        --target_video_path data/traffic_analysis_result.mov
    

© 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.