KevsRobots Learning Platform
By Kevin McAleer, 4 Minutes

In this lesson you’ll set up your Raspberry Pi for computer vision: a Python virtual environment with OpenCV, MediaPipe and CVZone, plus a quick camera test.
cv2) - reads frames from the camera, draws on them and shows them in a windowUpdate your Pi:
sudo apt update && sudo apt full-upgrade -y
Create a virtual environment:
Recent versions of Raspberry Pi OS don’t let pip install packages system-wide, so we create a virtual environment for the course. The --system-site-packages option lets it see the camera library that comes with Raspberry Pi OS.
python3 -m venv --system-site-packages ~/cvzone-env
source ~/cvzone-env/bin/activate
Your prompt now starts with (cvzone-env). Run the source line again each time you open a new terminal.
Install CVZone and MediaPipe:
pip install cvzone mediapipe
This also installs OpenCV (opencv-python) and NumPy. It can take a few minutes.
Save this as camera_test.py and run it with python camera_test.py:
import cv2
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
if not success:
print("No camera image")
break
cv2.imshow("Camera test", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
cv2.VideoCapture(0) opens the first cameracap.read() grabs one frame. success is False if it failedcv2.imshow() shows the frame in a window, and cv2.waitKey(1) gives the window time to draw and checks for a key pressq to quitUsing a Raspberry Pi camera module
cv2.VideoCapture(0)works with USB webcams. Camera modules on the ribbon cable use thepicamera2library instead. Swap the capture lines for:from picamera2 import Picamera2 picam2 = Picamera2() picam2.configure(picam2.create_preview_configuration(main={"format": "RGB888", "size": (640, 480)})) picam2.start() img = picam2.capture_array() # use this in place of cap.read()The
RGB888format gives you frames in the same colour order OpenCV expects. The rest of the code in this course works the same way.
Problem: pip install says error: externally-managed-environment.
Solution: Activate the virtual environment first with source ~/cvzone-env/bin/activate.
Why: Raspberry Pi OS protects the system Python, so packages go in a virtual environment.
Problem: pip can’t find a version of mediapipe.
Solution: Check you’re on 64-bit Raspberry Pi OS with uname -m, which should print aarch64. If it does, check MediaPipe on PyPI supports your Python version (python --version).
Why: MediaPipe only publishes packages for 64-bit Arm and particular Python versions.
Problem: The test program prints “No camera image”.
Solution: Check the webcam is plugged in and try cv2.VideoCapture(1), or use the picamera2 code for a camera module.
Why: OpenCV couldn’t open a camera at that index.
Quick quiz 1 of 2
What does MediaPipe do in this course?
Quick quiz 2 of 2
Why do we use a virtual environment?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments