This project detects and tracks vehicles in a video using YOLOv8 and adds an intelligent bridge control system:
- It continuously estimates the current load on a bridge.
- The bridge has a capacity limit of 20 tons.
- When admitting a new vehicle would exceed the capacity, the system blocks entry (no new vehicles can pass) until enough weight exits.
The video feed shows real-time status overlays: current load, capacity, and whether the bridge is OPEN or CLOSED.
- Vehicle detection (cars, buses, trucks) with YOLOv8
- Lightweight multi-object tracking to assign stable IDs per vehicle
- Entry/exit line logic to know when a vehicle enters/leaves the bridge
- Per-class weight estimates (customizable)
- Real-time bridge capacity management (default 20 tons)
-
Install Python 3.9+ (recommended)
-
Create and activate a virtual environment (optional but recommended)
python3 -m venv .venv
source .venv/bin/activate- Install dependencies
pip install ultralytics opencv-python pandas cvzone- Run the app
python yolov8-multiple-vehicle-detection/mainh.pyYou should see a window with the processed video. Press Esc to quit.
- Read a video frame.
- Detect objects using YOLOv8.
- Keep only vehicles of interest: car, bus, truck.
- Track each vehicle across frames to give it a stable ID.
- Use two horizontal lines:
- Entry line (vehicle enters bridge when crossing this line)
- Exit line (vehicle leaves bridge when crossing this line)
- When a tracked vehicle crosses the entry line:
- Check its estimated weight (based on class) and the current total on-bridge load.
- If admitting it keeps the total load <= 20 tons, it is allowed and the load increases.
- Otherwise, the bridge is CLOSED for new entries (the vehicle is visually blocked) until weight drops.
- When a tracked vehicle crosses the exit line and it was on the bridge, we subtract its weight.
- The overlay shows:
- OPEN/CLOSED
- Current load vs capacity (tons)
- A red barrier strip when CLOSED
flowchart TD
A[Start / Read frame] --> B[YOLO detect vehicles]
B --> C[Group by class: car/bus/truck]
C --> D[Track each class -> stable IDs]
D --> E[Compute vehicle center Y]
E --> F{Crossed Entry line?}
F -- Yes --> G[Would load + vehicle weight <= capacity?]
G -- Yes --> H[Allow entry: add ID, increase load]
G -- No --> I[Block entry: do NOT add]
E --> J{Crossed Exit line?}
J -- Yes --> K[If on-bridge: remove ID, decrease load]
H --> L[Update overlay]
I --> L
K --> L
L --> M[Draw status OPEN/CLOSED]
M --> N[Display frame]
N --> A
Open yolov8-multiple-vehicle-detection/mainh.py and look at the top constants:
CAPACITY_TONS = 20.0
VEHICLE_WEIGHTS_TONS = {
'car': 1.5,
'bus': 12.0,
'truck': 15.0,
}
ENTRY_LINE_Y = 184 # Entry line position (in pixels, Y axis)
EXIT_LINE_Y = 209 # Exit line position (in pixels, Y axis)
LINE_TOLERANCE = 8 # Crossing tolerance (pixels)- Change capacity: set
CAPACITY_TONSto your desired limit. - Adjust weights: edit
VEHICLE_WEIGHTS_TONSto better match your environment. - Move lines: tweak
ENTRY_LINE_YandEXIT_LINE_Yto match your scene.
Tip: Use the mouse to inspect coordinates in the window if needed (debugging hook exists in code).
yolov8-multiple-vehicle-detection/mainh.py: main app with detection, tracking, and bridge logicyolov8-multiple-vehicle-detection/tracker.py: simple multi-object tracker used to keep stable IDsyolov8-multiple-vehicle-detection/coco.txt: class labels used by the YOLO modelyolov8-multiple-vehicle-detection/tf.mp4: sample input video (you can replace it)yolov8-multiple-vehicle-detection/yolov8s.pt: YOLOv8 weights file (pretrained)
- A vehicle is considered “on the bridge” only after it crosses the entry line and is admitted.
- If admitting a vehicle would exceed the capacity, the system:
- Keeps the bridge CLOSED to new entries
- Shows a red barrier at the entry line and a message: “NO ENTRY - OVER CAPACITY”
- Continues to allow vehicles already on the bridge to exit; once enough weight exits, the bridge reopens automatically.
This is purely a logic/visual block in this demo; it does not physically stop vehicles. In a real system, you’d connect this signal to a hardware barrier.
- Put your own video file in
yolov8-multiple-vehicle-detection/and update this line inmainh.py:
cap = cv2.VideoCapture('tf.mp4') # change to your filenameFor live cameras, pass an index (e.g., 0) or RTSP URL to VideoCapture.
- Python 3.9+
- Packages:
ultralytics(YOLOv8)opencv-pythonpandascvzone
Install via:
pip install ultralytics opencv-python pandas cvzoneIf you have a compatible GPU and want acceleration, follow Ultralytics’ docs for CUDA/torch install instructions, then install ultralytics.
- "No module named cv2" →
pip install opencv-python - "No module named ultralytics" →
pip install ultralytics - Video window not showing → some environments (like headless servers) can’t open GUI windows. Run locally or set up a virtual display.
- Model loads slow on first run → weights are large; after the first load it’s faster.
- Detections look off → ensure the video resolution fits the scene and lines (
ENTRY_LINE_Y,EXIT_LINE_Y) are placed correctly.
- Added real-time bridge capacity logic with a default 20-ton limit.
- Added per-class weight estimates and gating logic at the entry line.
- Added OPEN/CLOSED overlay and a red visual barrier when closed.
- Switched to ID-based entry/exit accounting so load is accurate as vehicles leave.
This repository builds on YOLOv8 (Ultralytics). Review and respect their license when using the model weights and code.