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
1. Install OpenCV
sudo apt install python3-opencv -y
2. Install TensorFlow Lite
pip3 install tflite-runtime
3. 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.
FAQs
Is Raspberry Pi good for object detection?
Yes, Raspberry Pi 5 is a good choice for lightweight object detection tasks. With improved CPU and GPU performance, it supports real-time inference using optimized models like TensorFlow Lite or YOLO Tiny.
Which algorithm is best for object detection?
Popular algorithms include YOLO (for speed), SSD (for balance), and Faster R-CNN (for accuracy). For Raspberry Pi, YOLO Tiny or MobileNet SSD are typically the best options.
Which YOLO version is best for Raspberry Pi?
YOLOv5 Nano, YOLOv8 Nano, and YOLOv4 Tiny are best suited for Raspberry Pi due to their small size and fast inference speed.
How to do object tracking with Raspberry Pi and your drone?
Combine a lightweight detection model with OpenCV trackers (e.g., CSRT or MOSSE) for real-time tracking. Integrate with drone control libraries like dronekit
or DJI SDK
to respond to object position.
Can Raspberry Pi 5 run TensorFlow and OpenCV together?
Yes, Raspberry Pi 5 can efficiently run both TensorFlow Lite and OpenCV, making it suitable for real-time object detection and image processing applications.
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.
For further improvements, consider:
-
Running object detection with a more optimized model for better accuracy and speed.
-
Integrating edge TPU (e.g., Coral USB Accelerator) for faster inference.
-
Deploying the detection system in a real-world project with IoT integration.