Hardware: Raspberry Pi 4 / Raspberry Pi 5
AI type: Computer Vision – Multi-Zone Counting
--------------------------------------------------
Overview
This project extends basic traffic analytics by using multiple virtual lines
(zones) and counting vehicles by direction. It enables simple intersection
analysis using AI on Raspberry Pi, fully offline and without GPUs.
--------------------------------------------------
What you build
- AI vehicle detection on Raspberry Pi
- Multiple virtual counting lines (zones)
- Direction-based counting per zone
- Fully local processing
--------------------------------------------------
Required hardware
- Raspberry Pi 4 or Raspberry Pi 5
- Camera (USB or Raspberry Pi Camera)
- microSD card
- Power supply
--------------------------------------------------
Software requirements
- Raspberry Pi OS
- Python 3
- OpenCV
- Ultralytics YOLO (lightweight model)
--------------------------------------------------
System architecture
1. Camera captures frames
2. AI model detects vehicles
3. Vehicle centers are tracked between frames
4. Each virtual line (zone) checks crossing direction
5. Counters are updated per zone and direction
--------------------------------------------------
Installation
sudo apt update
sudo apt upgrade
sudo apt install python3-opencv python3-pip
pip3 install ultralytics
--------------------------------------------------
Zone concept
Each zone is defined by a horizontal line position.
When a vehicle center crosses a line, the direction
(up or down) determines which counter is updated.
--------------------------------------------------
Python code (copy-paste)
import cv2
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture(0)
zones = [200, 300] # Y positions of virtual lines
prev_y = None
counts = {
200: {"up": 0, "down": 0},
300: {"up": 0, "down": 0}
}
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame, verbose=False)
boxes = results[0].boxes
if len(boxes) > 0:
x1, y1, x2, y2 = boxes[0].xyxy[0]
cy = int((y1 + y2) / 2)
if prev_y is not None:
for z in zones:
if prev_y < z and cy >= z:
counts[z]["down"] += 1
elif prev_y > z and cy <= z:
counts[z]["up"] += 1
prev_y = cy
for z in zones:
cv2.line(frame, (0, z), (frame.shape[1], z), (255, 0, 0), 2)
cv2.putText(frame,
f"Zone {z} Up:{counts[z]['up']} Down:{counts[z]['down']}",
(10, z - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 255, 0),
1)
cv2.imshow("Multi-Zone Traffic AI", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
--------------------------------------------------
How it works
The AI model detects vehicles and calculates their vertical center.
When the center crosses any defined zone line, the direction of movement
determines whether it is counted as upward or downward traffic.
Each zone maintains independent counters.
--------------------------------------------------
Practical applications
- Intersection traffic analysis
- Lane-based vehicle flow estimation
- Smart city traffic prototypes
- Educational computer vision projects
--------------------------------------------------
Limitations
- Assumes one main vehicle at a time
- Occlusions can affect counts
- Advanced tracking improves accuracy
--------------------------------------------------
Conclusion
Multi-zone traffic analytics demonstrate how Raspberry Pi can handle
complex AI-based counting tasks without expensive hardware.
By combining simple logic with lightweight AI models, affordable systems
can deliver useful traffic insights.
--------------------------------------------------