Hardware: Raspberry Pi 4 / Raspberry Pi 5
AI type: Computer Vision – Detection + Speed Estimation
Overview
This project shows how a Raspberry Pi can estimate the speed of moving objects using AI-based object detection and simple motion analysis.
The system runs fully offline and does not require GPUs or cloud services.
The goal is not absolute precision, but a practical and reproducible AI approach using affordable hardware.
What you will build
AI-based object detection on Raspberry Pi
Tracking object movement across frames
Estimating object speed using pixel distance and time
Fully local processing
Required hardware
Software requirements
Project architecture
Camera captures video frames
AI model detects objects
Object positions are tracked
Speed is calculated from movement over time
Installation steps
Update system and install dependencies:
sudo apt update
sudo apt upgrade
sudo apt install python3-opencv python3-pip
pip3 install ultralytics
AI model choice
A lightweight YOLO Nano model is used for detection.
The model is pre-trained and used only for inference.
import cv2
from ultralytics import YOLO
import time
import math
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture(0)
prev_pos = None
prev_time = None
speed = 0
PIXELS_PER_METER = 50 # calibration value
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]
cx = int((x1 + x2) / 2)
cy = int((y1 + y2) / 2)
current_time = time.time()
if prev_pos is not None:
dist_pixels = math.hypot(cx - prev_pos[0], cy - prev_pos[1])
dist_meters = dist_pixels / PIXELS_PER_METER
dt = current_time - prev_time
if dt > 0:
speed = dist_meters / dt
prev_pos = (cx, cy)
prev_time = current_time
cv2.circle(frame, (cx, cy), 5, (0, 255, 0), -1)
cv2.putText(frame, f"Speed: {speed:.2f} m/s",
(10, 30), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 255, 0), 2)
cv2.imshow("AI Speed Estimation", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
How it works
The AI model detects an object and extracts its center position.
The distance between positions across frames is measured in pixels and converted to meters using a calibration factor.
Speed is calculated as distance divided by time.
Calibration note
The PIXELS_PER_METER value depends on camera position and scene scale.
It must be adjusted experimentally for better accuracy.
Practical applications
Traffic speed monitoring
Robot motion analysis
Conveyor belt monitoring
Educational AI projects
Limitations
Approximate speed estimation
Sensitive to camera angle and calibration
Not suitable for high-precision measurements
Conclusion
This project demonstrates that Raspberry Pi can perform meaningful AI-based speed estimation without GPUs or cloud services.
With lightweight models and simple math, affordable hardware can deliver practical AI solutions.
AI without millions remains an engineering reality.
