Computer Vision with OpenCV and Picamera2
Capture frames from the Camera Module 3 and run motion detection in real time.
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

Video by DIY Engineers, embedded from YouTube. The written steps below are our own and match the parts list.
Mission steps
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.
Test it
Run rpicam-hello to see a 5-second preview (on the desktop) or rpicam-still -o test.jpg to take a photo.
Install OpenCV
Install python3-opencv and python3-picamera2 from apt — they are optimized for the Pi.
Run motion detection
The script compares each frame with the previous one and reports when enough pixels change.
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.jpgimport 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 = grayTip: 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.