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]
β¨ 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
-
Install dependencies:
pip install opencv-python ultralytics numpy
-
Download a YOLOv8 model (if you don't already have one):
- The default uses
yolov8n.pt
, available from Ultralytics YOLOv8 models.
- The default uses
-
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()
π 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.
Top comments (0)