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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import cv2 import time from picamera2 import Picamera2 from ultralytics import YOLO piCam = Picamera2(0) W=1280 H=720 tStart = time.time() fps = 0 RES = (W,H) piCam.preview_configuration.main.size = RES piCam.preview_configuration.main.format = "RGB888" piCam.preview_configuration.controls.FrameRate=60 piCam.preview_configuration.align() piCam.configure("preview") piCam.start() model = YOLO("/home/pi/ai-lab-kit/yolo/yolov8n.pt") textLowerLeft = (int(W*.01),int(H*.05)) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontThickness = int(W/425) fontScale = H*.0015 fontColor = (0,0,255) while True: deltaT = time.time() - tStart tStart=time.time() fps = fps*.95 + (1/deltaT)*.05 frame= piCam.capture_array() #Uncomment Next Line if Your image is Upside Down #frame=cv2.flip(frame,-1) results=model(frame,conf=.25, verbose=False) frame = results[0].plot() myText = "FPS: "+str(round(fps,1)) cv2.putText(frame,myText,textLowerLeft,fontFace,fontScale,fontColor,fontThickness) cv2.imshow("Camera", frame) cv2.moveWindow("Camera",0,60) if cv2.waitKey(1)==ord('q'): break cv2.destroyAllWindows() print('Program Terminated') |
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=1280andH=720(RES = (W,H)) and configurepreview_configuration.main.format = "RGB888"withFrameRate=60requested on the camera controls, followed bypiCam.align(),piCam.configure("preview"), andpiCam.start(). -
FPS Smoothing Variables: We initialize
tStart = time.time()andfps = 0. Notice how we don’t just print raw instantaneous1 / deltaTevery 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:
-
Exponential Moving Average (EMA) FPS: We compute
deltaT = time.time() - tStart, updatetStart, and smoothfpsusing:fps = fps*.95 + (1/deltaT)*.05That0.95/0.05blend gives you a steady, readable frame-rate estimate that still responds to changes over time. -
Capture the Frame:
frame = piCam.capture_array()pulls the current image array fromPicamera2. You’ll also see the commented-out line#frame=cv2.flip(frame,-1)ready if your camera is mounted upside down. -
Run YOLO Inference:
results = model(frame, conf=.25, verbose=False)runs detection onframe. Settingconf=.25keeps detections with at least 25% confidence, andverbose=Falsekeeps your terminal clean instead of flooding the console every frame. -
Annotate & Overlay FPS:
frame = results[0].plot()draws the predicted bounding boxes and labels onto the frame, andcv2.putText(...)stampsFPS: ...in the upper-left area of the image (yat5%ofHfrom the top edge in OpenCV image coordinates). -
Show Window & Clean Exit:
cv2.imshow("Camera", frame)andcv2.moveWindow("Camera",0,60)position the preview window cleanly on screen, whileif cv2.waitKey(1)==ord('q'): breaklets you exit cleanly by pressingq. 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:
-
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.
-
Test the Confidence Threshold (
conf): Changeconf=.25toconf=.60and then toconf=.10. Describe what happens to false positives, missed detections, and your displayed FPS. -
Experiment with Resolution (
WandH): Drop the resolution from1280x720down to640x480(and try960x540if 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? -
Verify Clean Shutdown: Test exiting with the
qkey and confirm the window closes cleanly andProgram Terminatedprints 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!