0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Do Object Detection on Raspberry Pi Easily

To do object detection on a Raspberry Pi, install TensorFlow Lite and use a pre-trained model like MobileNet SSD. Write a Python script to capture camera input and run detection with the model for real-time object identification.
📐

Syntax

Here is the basic syntax to perform object detection on Raspberry Pi using TensorFlow Lite:

  • import tflite_runtime.interpreter as tflite: Load the TensorFlow Lite interpreter.
  • interpreter = tflite.Interpreter(model_path='model.tflite'): Load the detection model.
  • interpreter.allocate_tensors(): Prepare the model for inference.
  • input_data = preprocess(image): Prepare the camera image for the model.
  • interpreter.set_tensor(input_index, input_data): Set input tensor.
  • interpreter.invoke(): Run the model.
  • output_data = interpreter.get_tensor(output_index): Get detection results.

This pattern captures camera frames, processes them, and runs detection.

python
import tflite_runtime.interpreter as tflite

# Load the TensorFlow Lite model
interpreter = tflite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()

# Get input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Prepare input data (example placeholder)
input_data = ...  # Preprocessed image data

# Set input tensor
interpreter.set_tensor(input_details[0]['index'], input_data)

# Run inference
interpreter.invoke()

# Get output tensor
output_data = interpreter.get_tensor(output_details[0]['index'])
💻

Example

This example shows how to run object detection on a Raspberry Pi camera feed using TensorFlow Lite and OpenCV. It loads a pre-trained MobileNet SSD model, captures video frames, runs detection, and draws boxes around detected objects.

python
import cv2
import numpy as np
import tflite_runtime.interpreter as tflite

# Load TFLite model and allocate tensors
interpreter = tflite.Interpreter(model_path='detect.tflite')
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Open camera
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Resize frame to model input size
    input_shape = input_details[0]['shape']
    resized = cv2.resize(frame, (input_shape[2], input_shape[1]))

    # Convert to RGB and normalize
    input_data = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
    input_data = np.expand_dims(input_data, axis=0)
    input_data = (np.float32(input_data) - 127.5) / 127.5

    # Set input tensor
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()

    # Get detection results
    boxes = interpreter.get_tensor(output_details[0]['index'])[0]  # Bounding box coordinates
    classes = interpreter.get_tensor(output_details[1]['index'])[0]  # Class index
    scores = interpreter.get_tensor(output_details[2]['index'])[0]  # Confidence scores

    height, width, _ = frame.shape

    # Draw boxes for detections with confidence > 0.5
    for i in range(len(scores)):
        if scores[i] > 0.5:
            ymin, xmin, ymax, xmax = boxes[i]
            (left, right, top, bottom) = (int(xmin * width), int(xmax * width), int(ymin * height), int(ymax * height))
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
            label = f'ID: {int(classes[i])} {scores[i]:.2f}'
            cv2.putText(frame, label, (left, top - 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()
Output
A window opens showing the camera feed with green boxes around detected objects labeled with their class ID and confidence score. Press 'q' to quit.
⚠️

Common Pitfalls

  • Model compatibility: Using a TensorFlow model not converted to TensorFlow Lite will cause errors.
  • Input shape mismatch: Feeding images not resized or normalized as the model expects leads to wrong results.
  • Performance issues: Raspberry Pi is limited in power; use lightweight models like MobileNet SSD for real-time detection.
  • Camera setup: Not initializing the camera properly or using wrong device index causes capture failure.
python
import tflite_runtime.interpreter as tflite

# Wrong: Using a full TensorFlow model file
interpreter = tflite.Interpreter(model_path='model.pb')  # This will fail

# Right: Use a TensorFlow Lite model file
interpreter = tflite.Interpreter(model_path='model.tflite')
📊

Quick Reference

  • Use tflite_runtime for lightweight TensorFlow Lite support on Raspberry Pi.
  • Choose a small, efficient model like MobileNet SSD for real-time detection.
  • Preprocess images: resize to model input size, convert to RGB, normalize.
  • Allocate tensors before inference with interpreter.allocate_tensors().
  • Use OpenCV to capture camera frames and display results.

Key Takeaways

Use TensorFlow Lite with a compatible model for efficient object detection on Raspberry Pi.
Preprocess camera images correctly by resizing, converting color, and normalizing before inference.
Lightweight models like MobileNet SSD balance accuracy and speed on limited hardware.
Test camera setup and model loading carefully to avoid common errors.
Use OpenCV to capture video and display detection results in real time.