Introduction
Object detection is a crucial aspect of computer vision, enabling machines to recognize and locate objects within an image or video. With the Raspberry Pi 5, a powerful and cost-effective single-board computer, you can implement real-time object detection for various applications, such as security systems, automation, and robotics.
This guide will walk you through setting up and running object detection on Raspberry Pi 5 using OpenCV and TensorFlow Lite.
Requirements
- Raspberry Pi 5 with Raspberry Pi OS installed
- Raspberry Pi Camera Module or USB webcam
- MicroSD card (32GB recommended)
- Power supply for Raspberry Pi 5
- Internet connection
- HDMI display (optional, for setup)
- Keyboard and mouse (optional, for setup)
Step 1: Setting Up Raspberry Pi 5
- Install Raspberry Pi OS
- Download the latest Raspberry Pi OS from the official website.
- Flash the OS onto a microSD card using tools like Raspberry Pi Imager or Balena Etcher.
- Insert the microSD card into the Raspberry Pi and power it on.
- Update System Packages
- sudo apt update && sudo apt upgrade -y
- Enable Camera Module (if using Raspberry Pi Camera)
- sudo raspi-config
- Navigate to Interface Options → Camera → Enable.
- Reboot the Raspberry Pi:
- sudo reboot
Step 2: Install Dependencies
- Install OpenCV
sudo apt install python3-opencv -y
- Install TensorFlow Lite
pip3 install tflite-runtime
- Install Additional Python Libraries
pip3 install numpy pillow imutils
Step 3: Download Pre-trained Object Detection Model
TensorFlow Lite provides pre-trained models optimized for edge devices like Raspberry Pi.
- Create a project directory:
- mkdir ~/object_detection && cd ~/object_detection
- Download a pre-trained model (e.g., MobileNet SSD v2):
- wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip
- Extract the model files:
- unzip coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip
- Download the COCO dataset labels:
- wget https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/mscoco_label_map.pbtxt
Step 4: Write the Object Detection Script
Create a Python script detect_objects.py in the project directory:
import cv2
import numpy as np
import tflite_runtime.interpreter as tflite
# Load model and labels
MODEL_PATH = “detect.tflite”
LABEL_PATH = “labelmap.txt”
# Load labels
def load_labels(path):
with open(path, “r”) as f:
return [line.strip() for line in f.readlines()]
labels = load_labels(LABEL_PATH)
# Load TFLite model
interpreter = tflite.Interpreter(model_path=MODEL_PATH)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Initialize camera
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Preprocess frame
input_shape = input_details[0][‘shape’]
frame_resized = cv2.resize(frame, (input_shape[1], input_shape[2]))
input_data = np.expand_dims(frame_resized, axis=0).astype(np.uint8)
# Run inference
interpreter.set_tensor(input_details[0][‘index’], input_data)
interpreter.invoke()
# Get detection results
boxes = interpreter.get_tensor(output_details[0][‘index’])[0] # Bounding boxes
classes = interpreter.get_tensor(output_details[1][‘index’])[0] # Class index
scores = interpreter.get_tensor(output_details[2][‘index’])[0] # Confidence scores
# Draw detected objects
height, width, _ = frame.shape
for i in range(len(scores)):
if scores[i] > 0.5: # Confidence threshold
ymin, xmin, ymax, xmax = boxes[i]
(xmin, ymin, xmax, ymax) = (int(xmin * width), int(ymin * height), int(xmax * width), int(ymax * height))
label = f”{labels[int(classes[i])]}: {int(scores[i] * 100)}%”
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
cv2.putText(frame, label, (xmin, ymin – 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow(“Object Detection”, frame)
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
cap.release()
cv2.destroyAllWindows()
Step 5: Run Object Detection
- Navigate to the project directory:
- cd ~/object_detection
- Run the script:
- python3 detect_objects.py
- The camera feed should open, displaying detected objects with bounding boxes and labels.
Conclusion
Congratulations! You have successfully implemented object detection on Raspberry Pi 5 using TensorFlow Lite and OpenCV. This setup can be extended to various applications, such as real-time surveillance, smart automation, and interactive projects.
FAQs
Is Raspberry Pi good for object detection?
Raspberry Pi is a good choice for object detection, especially for lightweight models optimized for edge computing, such as TensorFlow Lite or YOLO Tiny.
While it may not match the performance of high-end GPUs, Raspberry Pi 5 has improved processing power and can handle real-time object detection with optimized models.
Which algorithm is best for object detection?
The best algorithm depends on the use case. Some popular object detection algorithms include:
YOLO (You Only Look Once) – Fast and efficient, suitable for real-time applications.
SSD (Single Shot MultiBox Detector) – Good balance between speed and accuracy.
Faster R-CNN – More accurate but computationally intensive, not ideal for Raspberry Pi.
Which YOLO version is best for Raspberry Pi?
YOLOv5 Nano or YOLOv8 Nano are the best versions for Raspberry Pi, as they are optimized for low-power edge devices. YOLOv4 Tiny is another good option, as it offers a trade-off between speed and accuracy while being lightweight.
How to do object tracking with Raspberry Pi and your drone?
Use a Raspberry Pi camera module or an external webcam to capture frames.
Implement an object detection model like YOLO or MobileNet SSD to detect the target object.
Use OpenCV’s cv2.Tracker module (e.g., KCF, CSRT, or MOSSE) for real-time tracking.
Send movement commands to the drone using a communication protocol like MAVLink or Python-based drone libraries such as dronekit-python
or DJI Tello SDK
.
Optimize processing by running the model on a Coral TPU or Jetson Nano for better performance.