Deploying Efficient Object Detection on Raspberry Pi Using TensorFlow Lite

This guide will walk you through the process of setting up real-time object detection on a Raspberry Pi using a camera module, OpenCV, and TensorFlow Lite. We will be using a pre-trained MobileNet SSD model for object detection, which is well-suited for resource-constrained devices like the Raspberry Pi. By the end, you will have a fully functional object detection system using the Raspberry Pi camera module.

Before you begin, gather the following components –

  •  Raspberry Pi (preferably Raspberry Pi 4-5 for better performance)
  • Raspberry Pi Camera Module (v2 or later for higher resolution)
  • MicroSD card (32GB recommended with Raspberry Pi OS pre-installed)
  • Power supply (5V, 3A power adapter for Raspberry Pi)
  • Monitor, keyboard, and mouse (or SSH/VNC for remote access)
  • Internet connection (for installing necessary packages)

#Step 1 – Set Up the Raspberry Pi Camera

I. Turn off the Raspberry Pi if it is currently powered on.
II. Connect the Raspberry Pi Camera Module to the dedicated camera port on the Raspberry Pi board. Here is how –
a) Locate the camera port near the HDMI connector.
b) Lift the plastic latch on the port.
c) Insert the camera’s ribbon cable into the port with the metal contacts facing away from the HDMI port.
d) Secure the cable by pressing down the plastic latch.

#2 – Turn on the Raspberry Pi and wait for it to boot

#Step 3 – Enable the Camera in Raspberry Pi OS

Before you can use the camera, it must be enabled via the Raspberry Pi configuration tool.

I, Open a terminal on the Raspberry Pi desktop or access it via SSH.
II, Run the Raspberry Pi configuration tool –

sudo raspi-config

III, In the menu –

  •  Navigate to Interface Options.
  • Select Camera and choose Enable.

IV, After enabling the camera, exit the configuration tool and reboot the system:

sudo reboot

This will ensure the camera is ready for use.

#Step 4 – Install Required Libraries

To perform object detection, you will need several Python libraries, such as OpenCV for image processing and TensorFlow Lite for running a machine learning model. Ensure your Raspberry Pi OS is up-to-date before installing these libraries.

# 4.1 Update and Upgrade Your System

First, update and upgrade your system to avoid any compatibility issues:

sudo apt update
sudo apt upgrade

# 4.2 Install OpenCV for Image Processing

OpenCV is a widely used library for computer vision tasks. Install it by running:

sudo apt install python3-opencv

# 4.3 Install TensorFlow Lite for Object Detection

TensorFlow Lite is a lightweight version of TensorFlow, designed for edge devices like the Raspberry Pi. To install the TensorFlow Lite runtime:

pip3 install tflite-runtime
> If `pip3` is not installed, install it first with the following - 
sudo apt install python3-pip

# 4.4 Install the `picamera` Library for Camera Access

The picamera library allows you to interface with the Raspberry Pi camera module via Python –

pip3 install picamera

#Step 5 – Test the Raspberry Pi Camera

To ensure the camera is functioning correctly, run a simple Python script to capture and display images.

1. Open your favorite text editor (e.g., `nano`) and create a test script –

nano camera_test.py

2. Write the following Python code in the script –

from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.start_preview()
sleep(5) # Show camera preview for 5 seconds
camera.stop_preview()

3. Save the file and run it –

python3 camera_test.py

This should open a live camera preview for 5 seconds. If the preview displays correctly, your camera is functioning properly.

#Step 6 – Download a Pre-Trained Object Detection Model

We will use a pre-trained MobileNet SSD model for object detection, which is optimized for efficiency on devices like the Raspberry Pi. This model can detect objects in real-time and is lightweight enough for low-power devices.

# 6.1 Download the TensorFlow Lite Model

Download the MobileNet SSD model from the TensorFlow Hub. To do this, use the following commands –

wget http - //download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz

# 6.2 Extract the Model Files

Extract the downloaded model –

tar -xvzf ssd_mobilenet_v1_coco_2017_11_17.tar.gz

The extracted folder will contain –

  •  A `.tflite` file, which is the TensorFlow Lite model
  • A label map that maps numeric class indices to object names (e.g., “person”, “car”, etc.)

#Step 6 – Write the Object Detection Script

Now, we will write a Python script to run object detection using the Raspberry Pi camera and the downloaded model.

Create a new Python file for object detection –

nano object_detection.py

Write the following Python code in the file, step by step –

# 6.1 Import Libraries

Start by importing the required libraries –

import cv2
import numpy as np
import tensorflow as tf
from picamera.array import PiRGBArray
from picamera import PiCamera

# 6.2 Load the TensorFlow Lite Model

Load the TensorFlow Lite model and set up the interpreter –

interpreter = tf.lite.Interpreter(model_path="ssd_mobilenet_v1_coco_2017_11_17.tflite")
interpreter.allocate_tensors()
# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Get the shape of the input image
input_shape = input_details[0]['shape']

# 6.3 Load the Label Map

The label map file maps the numeric class IDs to human-readable object names. For simplicity, here is a shortened label list –

labels = ["background", "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", ...] # Shortened for brevity

# 6.4 Set Up the Camera

Initialize the Raspberry Pi camera and set its resolution and framerate –

camera = PiCamera()
camera.resolution = (640, 480) # You can adjust this for higher or lower resolution
camera.framerate = 30
raw_capture = PiRGBArray(camera, size=(640, 480))

# Allow the camera to warm up
camera.start_preview()

# 6.5 Capture Video and Perform Object Detection

Capture frames continuously from the camera, process each frame using the TensorFlow Lite model, and display the detected objects –

```
for frame in camera.capture_continuous(raw_capture, format="bgr", use_video_port=True) -
image = frame.array

# Pre-process the image to fit the model input size (e.g., 300x300 for MobileNet SSD)
input_image = cv2.resize(image, (300, 300))
input_image = np.expand_dims(input_image, axis=0)

# Set the tensor input and invoke the interpreter for detection
interpreter.set_tensor(input_details[0]['index'], input_image)
interpreter.invoke()

# Retrieve detection results
boxes = interpreter.get_tensor(output_details[0]['index'])[0] # Bounding boxes
classes = interpreter.get_tensor(output_details[1]['index'])[0] # Class IDs
scores = interpreter.get_tensor(output_details[2]['index'])[0] # Confidence scores
# Loop through each detection
for i in range(len(scores)) -
if scores[i] > 0.5 - # Only consider objects with confidence scores above 0.5
box = boxes[i]
label = labels[int(classes[i])]

# Calculate bounding box dimensions
(startY, startX, endY, endX) = (int(box[0] 480), int(box[1] 640),
int(box[2] 480), int(box[3] 640))

# Draw the bounding box on the image
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)
cv2.putText(image, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

# Display the image with the detected objects
cv2.imshow("Object Detection", image)
# Clear the stream for the next frame
raw_capture.truncate(0)

# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q') -
break

# Cleanup
cv2.destroyAllWindows()
camera.stop_preview()

“`

 

#Step 7 – Run the Object Detection Script

After writing the script, save it and run it using Python –

python3 object_detection.py

This will start real-time object detection, capturing video from the Raspberry Pi camera and displaying the detected objects with bounding boxes.

# Step 8 – Performance Optimization

You can optimize the performance of the Raspberry Pi object detection system by –

I. Lowering the Camera Resolution – Reducing the resolution from 640×480 to 320×240 will significantly speed up detection but reduce image clarity.
II. Using a Faster Model – TensorFlow Lite models like EfficientDet are more optimized for edge devices.
III. Offloading Inference to Hardware – You can attach a Coral USB Accelerator to handle TensorFlow Lite inference, significantly improving speed.

By following this step-by-step guide, you have set up a real-time object detection system using a Raspberry Pi camera, TensorFlow Lite, and OpenCV. This project can be extended with custom-trained models, improved performance optimizations, or used as a foundation for advanced applications like security cameras, smart home automation, or robotics.