DEV Community

Cover image for πŸš— Python Yolov8 - Advanced Vehicle Counter
Christian Zigah
Christian Zigah

Posted on

πŸš— Python Yolov8 - Advanced Vehicle Counter

A Python-based advanced vehicle counting system using YOLOv8 and OpenCV. Tracks and counts vehicles crossing a virtual line, records output video, and logs detailed count statistics for each class in both directions.

[πŸŽ₯ Watch Advance Car Counter - Output Recording on YouTube]


https://youtu.be/ha5XGrtyo9c

✨ Features

βœ… YOLO-based object detection

βœ… Class-specific counting: cars, trucks, buses, motorcycles

βœ… Two-way counting: UP and DOWN directions

βœ… Tracks individual vehicles by ID (without displaying ID on screen)

βœ… Overlays detection boxes with class & confidence

βœ… Video recording of processed output

βœ… Detailed text logging with timestamps

βœ… Real-time counting statistics panel

βœ… Visual indicator for each crossing event

βœ… Saves final summary report


πŸš€ Quick Start

  1. Install dependencies:

    pip install opencv-python ultralytics numpy
    
  2. Download a YOLOv8 model (if you don't already have one):

  3. Run the counter:

    python main.py
    

    By default, it processes traffic.mp4, drawing a counting line at 70% height.


πŸ› οΈ Usage

Create an instance of AdvancedVehicleCounter with your desired parameters:

from your_module import AdvancedVehicleCounter

counter = AdvancedVehicleCounter(
    model_path="yolov8n.pt",             # Path to your YOLOv8 model
    video_source="traffic.mp4",          # Path to your input video
    line_position=0.7,                   # Vertical position of the counting line (0-1 scale)
    output_folder="monitoring_results"   # Folder to save logs and processed video
)
counter.run()
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Outputs

  • Processed Video: Saved in the output folder with a timestamped filename (e.g., output_20250630_123456.mp4).

  • python-replCopyEditTimestamp,Class,Direction,TotalCount2025-06-30 12:34:56.789,car,DOWN,1...

  • python-replCopyEditFINAL SUMMARYClass,DOWN,UP,TOTALcar,10,8,18motorcycle,3,2,5...

πŸ“ˆ How It Works

πŸ”Ή DetectionUses YOLOv8 to detect vehicles in each frame.

πŸ”Ή TrackingMaintains a per-object path using YOLO’s tracker, enabling detection of line crossings.

πŸ”Ή CountingWhen a vehicle crosses the counting line:

  • UP: moves from below the line to above.

  • DOWN: moves from above the line to below.Counts are updated for the specific class.

πŸ”Ή Visualization

  • Draws bounding boxes with class name and confidence.

  • Highlights vehicles that crossed the line.

  • Displays a real-time panel with UP/DOWN counts for each class.

πŸ”Ή Output

  • Saves an annotated video to the output folder.

  • Logs every crossing event with timestamp, class, direction, and total count.

  • Appends a final summary at the end.

βš™οΈ Configuration Options

ParameterDescriptionDefaultmodel_pathYOLOv8 model weights file path"yolov8n.pt"video_sourceInput video file path"video2.mp4"line_positionVertical position of counting line (0-1 scale)0.5line_thicknessThickness of counting line in pixels2output_folderDirectory to save video and logs"output"

πŸ“Œ Notes

  • Requires a GPU or fast CPU for real-time performance.

  • Tested with YOLOv8 and OpenCV 4.x.

  • Supports Linux, macOS, and Windows.

  • Press Q to exit the application early.

πŸ“Έ Example

Once running, you will see:

  • Green detection boxes labeled with class & confidence.

  • A horizontal counting line at the configured position.

  • Visual indicators when a crossing happens (colored circles + text).

  • A panel showing real-time UP/DOWN counts per class.

Python Advance Car Counter on GitHub - Give me Stars

Top comments (0)