YOLO on Raspberry Pi LESSON 1: Run Your First Object Detection Program Using YOLO

Welcome back, folks! If you’ve been hanging out with me through the earlier computer vision and edge AI lessons, you know how we do things: we don’t just wave our hands and hope the magic smoke stays inside the silicon—we measure, we look at frame rates, and we understand what the code is actually doing.

In Lesson #1 of our YOLO series, we’re jumping straight into a working, real-time object detection loop on the Raspberry Pi using Picamera2, OpenCV, and Ultralytics YOLO (yolov8n.pt). Notice that this first script runs the .pt PyTorch weights directly as our baseline. That’s deliberate. You need a clean baseline measurement before you start optimizing, exporting, or chasing higher FPS later in the series.

Introduction to YOLO: Why “You Only Look Once” Matters at the Edge

Traditional or older multi-stage object detection pipelines often split the job up—proposing regions of interest first and then classifying them in a separate pass. That extra overhead adds latency fast when you’re running on an embedded processor instead of a massive data center GPU rack.

YOLO (You Only Look Once) takes a single neural network pass over the input image, predicting bounding boxes and class probabilities simultaneously in one forward shot. That architecture makes it a natural fit for real-time camera streams on edge hardware like the Raspberry Pi.

In this lesson, we load yolov8n.pt—the nano variant of YOLOv8. It’s lightweight and compact compared to larger variants (s, m, l, x), making it a practical starting point when you want to see boxes drawn on live video without waiting five seconds per frame. Even better, watching your live frame rate right on the display gives you immediate, honest feedback on how your resolution, confidence threshold, and model format affect real-world throughput.

Code Walkthrough: What Happens Inside the Loop

Let’s break down the script piece by piece so you aren’t just copying and pasting blindly.

1. Imports, Camera Setup, and Frame Rate Initialization

We bring in cv2 for drawing text and displaying the OpenCV window, time for measuring elapsed time between frames, Picamera2 to grab frames from the Pi camera pipeline, and YOLO from ultralytics.

  • Resolution & Format: We set W=1280 and H=720 (RES = (W,H)) and configure preview_configuration.main.format = "RGB888" with FrameRate=60 requested on the camera controls, followed by piCam.align(), piCam.configure("preview"), and piCam.start().

  • FPS Smoothing Variables: We initialize tStart = time.time() and fps = 0. Notice how we don’t just print raw instantaneous 1 / deltaT every single frame—raw instantaneous FPS jumps around like a flea on a hot skillet.

2. Loading the Model and Scaling the HUD

Right after starting the camera, we load the model weights:

model = YOLO("/home/pi/ai-lab-kit/yolo/yolov8n.pt")

Then we calculate our on-screen text parameters proportionally to W and H (textLowerLeft, fontThickness, fontScale, and fontColor = (0,0,255)). Scaling font thickness and size off resolution is good practice—if you change W and H later, your FPS overlay won’t suddenly turn into microscopic confetti or a giant red blob.

3. The Real-Time Inference & Display Loop

Inside while True:, every iteration does five clear jobs:

  1. Exponential Moving Average (EMA) FPS: We compute deltaT = time.time() - tStart, update tStart, and smooth fps using:fps = fps*.95 + (1/deltaT)*.05 That 0.95 / 0.05 blend gives you a steady, readable frame-rate estimate that still responds to changes over time.

  2. Capture the Frame:frame = piCam.capture_array() pulls the current image array from Picamera2. You’ll also see the commented-out line #frame=cv2.flip(frame,-1) ready if your camera is mounted upside down.

  3. Run YOLO Inference:results = model(frame, conf=.25, verbose=False) runs detection on frame. Setting conf=.25 keeps detections with at least 25% confidence, and verbose=False keeps your terminal clean instead of flooding the console every frame.

  4. Annotate & Overlay FPS:frame = results[0].plot() draws the predicted bounding boxes and labels onto the frame, and cv2.putText(...) stamps FPS: ... in the upper-left area of the image (y at 5% of H from the top edge in OpenCV image coordinates).

  5. Show Window & Clean Exit:cv2.imshow("Camera", frame) and cv2.moveWindow("Camera",0,60) position the preview window cleanly on screen, while if cv2.waitKey(1)==ord('q'): break lets you exit cleanly by pressing q. Once broken out of the loop, cv2.destroyAllWindows() cleans up the GUI windows and prints 'Program Terminated'.

Homework Assignment

Don’t just watch the video and move on—put your hands on the keyboard and gather some real data:

  1. Run the Baseline & Log Your FPS: Run the program as written and note your steady-state FPS on screen. Point the camera at a few everyday objects (a mug, a phone, a keyboard, yourself) and see what labels and confidence scores pop up.

  2. Test the Confidence Threshold (conf): Change conf=.25 to conf=.60 and then to conf=.10. Describe what happens to false positives, missed detections, and your displayed FPS.

  3. Experiment with Resolution (W and H): Drop the resolution from 1280x720 down to 640x480 (and try 960x540 if you want an intermediate step). Compare the FPS before and after—how much faster does it run when you feed fewer pixels into the pipeline?

  4. Verify Clean Shutdown: Test exiting with the q key and confirm the window closes cleanly and Program Terminated prints without hanging your terminal.

Drop your FPS numbers and what you observed in the comments on the video—I love seeing real measurements from your setups!