How to Set Up Object Detection on Raspberry Pi

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

The first time I tried object detection with the Raspberry Pi was when I was working on a line-following robot some years back. The logic was simple: follow the white line, and if you come across an object, pick it and put it aside. With the rise of AI, the Raspberry Pi can now do more than sense or detect an object.

By combining the Raspberry Pi camera with powerful AI models, the Raspberry Pi can see, identify, and even tell whether the object in front is a human face, a license plate, a pet, or even a cup.

In this post, I will guide you through setting up object detection on your Raspberry Pi. From setting up your hardware, installing the necessary software packages, and the most exciting bit – interacting with the various AI models. Let’s dive right in.

If you’re like me and sometimes mix up syntax between programming languages, I’ve got just the thing for you. I’ve put together a Python cheat sheet with all the essential syntax in one place, so you can keep it handy and avoid any confusion. Download it here for free!

What You Need for Object Detection on Raspberry Pi

Below are the hardware requirements needed for this project:

  • Raspberry Pi: I recommend using a Raspberry Pi 4 or Raspberry Pi 5 with at least 4GB of RAM for this project. Since we will be running YOLOv8 for object detection, the system needs sufficient memory to load the model and process video frames efficiently.
  • Camera: You will need a camera to capture images or video for object detection. Personally, I recommend the Raspberry Pi Camera V2, but you can also use the Raspberry Pi High Quality Camera or even a USB webcam.

    If you want better performance and faster object detection, consider using the Raspberry Pi AI Camera instead. Unlike standard camera modules, the AI Camera includes built-in AI acceleration designed specifically for computer vision tasks.

  • Power supply: You will need the official Raspberry Pi power supply for this project. Please do not use other options, like your phone’s USB Type-C cable. You can learn more about this topic in our article on powering your Raspberry Pi.
  • Micro SD Card: Ensure you have a quality Micro SD Card with at least 8GB of space. Proceed to install the official Raspberry Pi OS.

  • Keyboard & Mouse: You will need these two to control and write the necessary code. You can either use the USB or wireless options.
  • Monitor: If you haven’t attached a TV screen or monitor to the Raspberry Pi before, read our complete guide on how to connect your Raspberry Pi to a monitor.

Tip: If you don’t want to use a keyboard, mouse, and monitor with your Raspberry Pi, you can still use other options like SSH and VNC, which allow you to access and control the Raspberry Pi remotely.

Setting Up the Software Environment

Up to this point, I hope you have all the hardware in place, and your Raspberry Pi with Raspberry Pi OS.

Hopefully, you’ve also already installed your Pi camera and tested if it works. If not, please read this article first and come back: Install a Camera on Your Raspberry Pi: The Ultimate Guide.

In this section, I will guide you through setting up the software for object detection.

Create a Python Virtual Environment

Before I dive into explaining what a Python virtual environment is or even creating one, you first need to confirm whether you have Python installed. Open the Terminal and run the command below to check:
python -V
or
python --version

Prefer reading without ads and popups?
Members get an ad-free version of every guide, plus exclusive project support.
Join the Community | Sign In

You should see an output of at least Python 3. It might not be the exact version number like mine, but as long as it’s 3, you are good to go. If that’s not the case for you, check our article on how to install the latest Python version on your Raspberry Pi.

Once you have that sorted, you can proceed to set up the virtual environment.

Launch the terminal and execute this command to create a Python virtual environment:
python3 -m venv --system-site-packages rpitips_object_detection

A virtual environment is like a “safe box” where you can do all your Python stuff without risking breaking the rest of the system. The command above will create a “rpitips_object_detection” folder.

Now navigate inside this directory:
source rpitips_object_detection/bin/activate

You will immediately notice your terminal prompt change, as ilustrated in the image above.

Quick note: If you find it hard to remember all these commands, I’ve put them all on a one-page cheat sheet. You can download it for free here so you have it handy whenever you're working on a project.

Install Python Packages for Object Detection

Up to this point, you have a virtual environment successfully set up. Next, you will need to install the Python packages needed for this project.

Update Your System and Install Python Tools

Launch your terminal and update your system’s package list:
sudo apt update

Next, install the Python package manager (PIP):
sudo apt install python3-pip -y

Next, you need to update PIP. Even though you just installed pip, the version from the repository may be outdated, so this ensures you’re using the newest features and bug fixes:
pip install -U pip

Install Ultralytics

Ultralytics is a company and open-source software team best known for building YOLO (You Only Look Once) models. Some of the fastest and most popular object detection and computer vision models in the world.

You can check their X (Twitter) account, and you will see some amazing samples they have listed there.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now

Ultralytics is the team behind the latest YOLO models. Their package takes care of most of the hard work by installing OpenCV and all the necessary infrastructure required to run YOLO:
pip install ultralytics[export]

Tip: Most times, this command will fail a couple of times. Don’t fret. Just re-run the command a couple of times. If all that fails, in my case, I resorted to the commands below:

Set Up Thonny for Raspberry Pi

Thonny is a popular free and open-source integrated development environment for Python. This is the IDE that I will be using for this project. It is quite easy to navigate and is designed for beginners.

Now, when you first launch Thonny, you might see a note at the top-right saying “Switch to regular mode.” If you see this, it means Thonny is running in simplified mode. In this mode, the IDE hides some menus to focus on minimalism, which is ideal for small screens.

Click the “Switch to regular mode” link to enable regular mode on Thonny. You will need to restart your Thonny IDE by closing and reopening it.

Regular mode in the Thonny IDE is the standard, full-featured user interface mode, designed to provide a comprehensive development environment with access to all menus, tools, and configurations.

As you can see in the image below, even the Thonny interface has changed, especially with the menus and icons.

The first thing you need to do is configure Thonny to use the Python interpreter present in the virtual environment you just created in the previous step.

Click Run -> Configure interpreter.

That’s it. Now, whenever you close and open Thonny, it will use the Python interpreter inside the rpitips_object_detection virtual environment.

If you want to change, you can always select “/usr/bin/python3.***” executable.

Running Your First Object Detection Example

Now you have reached the “juicy” part of this tutorial. If you have never worked on an AI project before, then welcome to the world of Artificial Intelligence.

In this project, we will work with a model popularly known as YOLOv8.

What YOLOv8 Does (in Plain English)

YOLOv8 is a computer vision model that can look at an image or a live camera feed and recognize what’s inside it.

For example, you can run this model on a Raspberry Pi and point a camera at a cat, and the model will instantly recognize it and label it as a cat. The same applies to people, cars, cups, and many other everyday objects, all detected in real time.

Run YOLOv8

Now that you have a good understanding of what the YOLOv8 model does, I will guide you in writing a Python script that will run YOLOv8 on a live camera feed from a Raspberry Pi.

When you run this script, it will download some models into your directory. Therefore, we highly recommend you create a directory to store this script. You can use the command below:
mkdir yolov8_script

Let me take you step-by-step through what every line in this script does. However, you can copy-paste the whole code below.

1. Set up the Raspberry Pi camera.
from ultralytics import YOLO
model = YOLO("yolov8n.pt")

  • Configures the camera resolution and format.
  • Starts the camera so it can capture live frames.

2. Load the YOLOv8 model.

from ultralytics import YOLO
model = YOLO("yolov8n.pt")

  • Loads a pre-trained YOLOv8 model (yolov8n.pt).
  • This model is ready to detect objects like people, cats, cars, cups, etc.

3. Capture frames and run object detection in real time.

frame = picam2.capture_array()
results = model(frame)

  • Captures a single image from the camera.
  • YOLOv8 analyzes the frame and identifies objects.
  • Detection results include bounding boxes, labels, and confidence scores.

4. Draw the detections on the frame.

annotated_frame = results[0].plot()

  • Draws boxes around detected objects.
  • Adds labels like cat, person, or car on the frame.

5. Calculate and display FPS (Frames Per Second).

inference_time = results[0].speed['inference']
fps = 1000 / inference_time

  • Measures how fast the model detects objects.
  • Displays FPS on the camera window so you know the real-time speed.
Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

6. Show the live annotated camera feed.

cv2.imshow("Camera", annotated_frame)

  • Opens a window showing the camera feed with detected objects and FPS.
  • Updates in real time as the camera sees new objects.

7. Exit on pressing q.

if cv2.waitKey(1) == ord("q"):
break

  • Stops the program and closes the window when you press q.

Here is the whole script. Copy and paste the code into your Thonny IDE. Save the file in the folder you created and ensure it has the “.py” file extension.

import cv2
from picamera2 import Picamera2
from ultralytics import YOLO

# Set up the camera with Picam
picam2 = Picamera2()
picam2.preview_configuration.main.size = (1280, 1280)
picam2.preview_configuration.main.format = "RGB888"
picam2.preview_configuration.align()
picam2.configure("preview")
picam2.start()

# Load YOLOv8
model = YOLO("yolov8n.pt")

while True:
# Capture a frame from the camera
frame = picam2.capture_array()

# Run YOLO model on the captured frame and store the results
results = model(frame)

# Output the visual detection data, we will draw this on our camera preview window
annotated_frame = results[0].plot()

# Get inference time
inference_time = results[0].speed['inference']
fps = 1000 / inference_time # Convert to milliseconds
text = f'FPS: {fps:.1f}'

# Define font and position
font = cv2.FONT_HERSHEY_SIMPLEX
text_size = cv2.getTextSize(text, font, 1, 2)[0]
text_x = annotated_frame.shape[1] - text_size[0] - 10 # 10 pixels from the right
text_y = text_size[1] + 10 # 10 pixels from the top

# Draw the text on the annotated frame
cv2.putText(annotated_frame, text, (text_x, text_y), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

# Display the resulting frame
cv2.imshow("Camera", annotated_frame)

# Exit the program if q is pressed
if cv2.waitKey(1) == ord("q"):
break

# Close all windows
cv2.destroyAllWindows()

Click the Green button to run the script.

It might take a few minutes to download the model, but once done, it will open a window showing a preview and highlighting any object detected by YOLO.

Below you see me detected by the YOLO model.

To stop the script, press the letter “Q” on your keyboard.


🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Want to connect with other Raspberry Pi fans? Join the RaspberryTips Community. Ask questions, share your projects, and learn from each other. Join now.

Going Further with Object Detection on Raspberry Pi

Now that you’ve got a basic handle on object detection with a Raspberry Pi, let me show you some tweaks to improve your code.

Using Other Object Detection Models on Raspberry Pi

Here is something you need to keep in mind: right now, your Raspberry Pi can detect people and common objects like chairs, TVs, and cups, but it can’t detect everything. This is because YOLOv8 was trained on the COCO dataset, which only includes about 88 categories of objects. While these categories cover many common things like “car” or “sportsball,” anything outside the training set, like glasses, won’t be recognized.

So far, you’ve been running YOLOv8 with the tiny “nano” model, which is super fast but not the most powerful. The cool thing about the Ultralytics package is that you can swap models with just one line of code:
model = YOLO("yolov8n.pt")

Change the n after v8 to try a bigger model like s (small), m (medium), l (large), or x (extra-large), and the script will automatically download it.

Bigger models are slower, so your FPS will drop, but they see better and recognize objects more accurately. That means a cup in your hand is less likely to be confused with a phone, and objects farther from the camera are easier to detect.

Convert the model to NCNN Format

NCNN is a special model format optimized for ARM processors like the one in your Raspberry Pi. By converting your YOLOv8 model to NCNN, you can get a big speed boost, up to 4x faster, without losing accuracy. Here’s how it works:

from ultralytics import YOLO
# Load a YOLOv8 model
model = YOLO("yolov8n.pt")
# Export the model to NCNN format
model.export(format="ncnn", imgsz=640) # creates 'yolov8n_ncnn_model'
  • Just replace “yolov8n.pt” with the model you want to convert.
  • Keep imgsz at 640 for now.
  • The first time you run this, it may download some extra files, but the conversion only takes a few seconds.

After conversion, you’ll see a new folder like yolov8n_ncnn_model. Update your main demo script to use this model:

# Load the converted NCNN model
model = YOLO("yolov8n_ncnn_model")

Run the script again, and everything works as before, but much faster.

Drop the Processing Resolution

Lowering the resolution means YOLO has fewer pixels to process, which makes it faster. For example, changing from 640 to 320 pixels can significantly increase FPS. To do this, just set the desired resolution when exporting the NCNN model:
model.export(format="ncnn", imgsz=320) # must be multiple of 32

Then, in your main script, make sure you:

  • Load the exported NCNN model.
  • Set the same resolution when running predictions:
    results = model.predict(frame, imgsz=320)

Lower resolutions trade a little accuracy for speed. Objects may be detected less reliably at longer distances, but resolutions between 160 and 320 usually give the best balance of speed and performance.

That’s it! We have successfully set up YOLOv8 on a Raspberry Pi, run it on a live camera feed, explored different model sizes, and even boosted performance using NCNN and resolution tweaks. You’re now ready to detect objects in real time and experiment with your own AI projects.

Whenever you’re ready, here are other ways I can help you:

Test Your Raspberry Pi Level (Free): Not sure why everything takes so long on your Raspberry Pi? Take this free 3-minute assessment and see what’s causing the problems.

The RaspberryTips Community: Need help or want to discuss your Raspberry Pi projects with others who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct help.

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts