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.pyBy 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)