AcademyCommander

Computer Vision with OpenCV and Picamera2

Capture frames from the Camera Module 3 and run motion detection in real time.

LevelCommander
Time45 min
Est. cost~$30
Steps5

Picamera2 is the official camera library; OpenCV is the world's most popular vision toolkit. Together they power security cams, line-following rovers and object trackers.

Video tutorial

▶ DIY EngineersWatch on YouTube ↗

Video by DIY Engineers, embedded from YouTube. The written steps below are our own and match the parts list.

Mission steps

  1. Connect the camera

    Power off. On a Pi 5 use the 22-pin to 15-pin cable; lift the connector latch, insert the ribbon contacts facing the right way, close the latch.

  2. Test it

    Run rpicam-hello to see a 5-second preview (on the desktop) or rpicam-still -o test.jpg to take a photo.

  3. Install OpenCV

    Install python3-opencv and python3-picamera2 from apt — they are optimized for the Pi.

  4. Run motion detection

    The script compares each frame with the previous one and reports when enough pixels change.

  5. Level up

    Next, add a Hailo AI HAT+ for real object detection, or feed the frames to a robot's steering logic.

Code

sudo apt install -y python3-opencv python3-picamera2
rpicam-still -o test.jpg
import cv2, time
from picamera2 import Picamera2

cam = Picamera2()
cam.configure(cam.create_video_configuration(main={"format": "RGB888", "size": (640, 480)}))
cam.start(); time.sleep(1)

prev = None
while True:
    gray = cv2.GaussianBlur(cv2.cvtColor(cam.capture_array(), cv2.COLOR_BGR2GRAY), (21, 21), 0)
    if prev is not None:
        diff = cv2.threshold(cv2.absdiff(prev, gray), 25, 255, cv2.THRESH_BINARY)[1]
        if cv2.countNonZero(diff) > 5000:
            print(time.strftime("%H:%M:%S"), "motion!")
    prev = gray

Tip: test every script on the bench before installing it in a moving machine.

Questions

USB webcam instead?

Yes. Use cv2.VideoCapture(0) instead of Picamera2. The Camera Module gives better image quality and lower CPU use.