Hardware: Raspberry Pi 4 / Raspberry Pi 5
AI type: Computer Vision – Traffic Analytics
--------------------------------------------------
Overview
This project combines AI-based object counting and speed estimation to build
a simple traffic analytics system on Raspberry Pi.
The system detects vehicles, counts them, and estimates their speed using
camera input and lightweight AI models. Everything runs locally without
cloud services or GPUs.
--------------------------------------------------
What you build
- Vehicle detection using AI
- Real-time vehicle counting
- Approximate speed estimation
- Fully offline traffic analytics
--------------------------------------------------
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 video frames
2. AI model detects vehicles
3. Each detected vehicle is counted
4. Vehicle movement between frames is tracked
5. Speed is estimated using distance and time
--------------------------------------------------
Installation
sudo apt update
sudo apt upgrade
sudo apt install python3-opencv python3-pip
pip3 install ultralytics
--------------------------------------------------
AI model
A lightweight YOLO Nano model is used for vehicle detection.
The model is pre-trained and used only for inference.
--------------------------------------------------
Python code (copy-paste)
import cv2
import time
import math
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture(0)
PIXELS_PER_METER = 60
prev_positions = {}
vehicle_id = 0
counted = set()
while True:
ret, frame = cap.read()
if not ret:
break
results = model(frame, verbose=False)
boxes = results[0].boxes
current_time = time.time()
current_positions = {}
for box in boxes:
x1, y1, x2, y2 = box.xyxy[0]
cx = int((x1 + x2) / 2)
cy = int((y1 + y2) / 2)
matched = False
for vid, (px, py, pt) in prev_positions.items():
dist = math.hypot(cx - px, cy - py)
if dist < 50:
speed = (dist / PIXELS_PER_METER) / (current_time - pt)
current_positions[vid] = (cx, cy, current_time)
matched = True
break
if not matched:
current_positions[vehicle_id] = (cx, cy, current_time)
counted.add(vehicle_id)
vehicle_id += 1
prev_positions = current_positions
cv2.putText(frame, f"Vehicles counted: {len(counted)}",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (0, 255, 0), 2)
cv2.imshow("AI Traffic Analytics", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
--------------------------------------------------
How it works
Vehicles are detected using AI and represented by their center point.
By comparing movement between frames and measuring time differences,
the system estimates approximate speed.
Counting is performed by assigning each detected vehicle a unique ID.
--------------------------------------------------
Calibration note
The PIXELS_PER_METER value depends on camera position and scene scale.
It must be adjusted experimentally for realistic speed values.
--------------------------------------------------
Practical applications
- Traffic flow monitoring
- Speed trend estimation
- Smart city prototypes
- Educational AI projects
--------------------------------------------------
Limitations
- Speed values are approximate
- Camera angle affects accuracy
- Not suitable for legal speed enforcement
--------------------------------------------------
Conclusion
This project demonstrates that meaningful traffic analytics can be built
on Raspberry Pi using AI, without expensive hardware or cloud services.
Affordable hardware combined with smart software design enables
practical AI solutions.
--------------------------------------------------