نظام ذكي لعد المركبات واتجاهات المرور في الوقت الحقيقي
Smart Traffic-flow Real-Time Vehicle Counter
A real-time AI-powered traffic monitoring system based on YOLOv8 that detects,
tracks, and counts vehicles moving IN and OUT by analyzing their direction as they
cross a virtual line in a video stream.
Project Overview
This project implements an intelligent traffic flow analysis system that automatically
detects and counts vehicles entering and exiting an area using a single camera feed.
By combining YOLOv8 object detection with custom centroid-based tracking and the
Hungarian Algorithm, the system ensures accurate, real-time bidirectional counting
without duplicate entries.
Key Features
YOLOv8-Based Vehicle Detection
- 🚗 Fast and lightweight vehicle detection using yolov8n.pt.
- ⚡ Real-time performance on standard hardware.
ID-Based Object Tracking
- 🆔 Assigns persistent IDs to vehicles across frames.
- 🔁 Prevents duplicate counting for the same vehicle.
Bidirectional IN / OUT Counting
- ⬇️ Counts vehicles moving top-to-bottom (IN).
- ⬆️ Counts vehicles moving bottom-to-top (OUT).
Centroid Smoothing & Line Logic
- 🧠 Smooths object movement using moving averages.
- 📏 Direction determined via virtual centerline crossing.
Visualization & Video Export
- 🧾 Displays bounding boxes, IDs, and live counters.
- 💾 Saves annotated output video using cv2.VideoWriter.
Technology Stack
- 🐍 Programming Language: Python 3.x
- 🧠 Object Detection: YOLOv8 (Ultralytics)
- 🎥 Computer Vision: OpenCV
- 📐 Math & Arrays: NumPy
- 🔢 Tracking Logic: Hungarian Algorithm (SciPy)
Use Cases
- 🚦 Traffic monitoring at intersections and toll gates.
- 🏙️ Smart city real-time vehicle flow analysis.
- 📊 Transport planning and congestion studies.
- 🧾 Audit-ready traffic reporting with video proof.
Conclusion
This smart traffic-flow vehicle counter demonstrates how YOLOv8 combined with
custom tracking logic can deliver accurate, real-time IN/OUT vehicle analytics.
The system is lightweight, extensible, and ideal for smart surveillance, traffic
engineering, and intelligent transportation applications.