Python/OpenCV exercises on Raspberry Pi - Capture images from Camera Module 3 using Picamera2, display using OpenCV.
It's a Python/OpenCV exercise run on Raspberry Pi OS (Bookworm), to capture
images using Picamera2 and show using OpenCV.
Test on:
Raspberry Pi 5B/Raspberry Pi OS 64-bit (Bookworm)
Python: 3.11.2
picamera2: 0.3.18
cv2: 4.6.0 (OpenCV was installed from apt)
Code:
picam2_cv2_capture_array.py
"""
Picamera2 + OpenCV exercise:
Run on Raspberry Pi 5 + Camera Module 3,
capture image using capture_array() and display using cv2.imshow().
Press:
S/s to save
Q/q to quit
My cameras:
Picamera2(0) - Camera Module 3 NoIR Wide
Picamera2(1) - Camera Module 3
"""
import cv2
import picamera2
from libcamera import controls
import platform
from importlib.metadata import version
import time
print("Python:", platform.python_version())
print(picamera2.__name__ + ":", version(picamera2.__name__))
print(cv2.__name__ + ":", cv2.__version__)
print()
time.sleep(1)
cv2.startWindowThread()
picam2 = picamera2.Picamera2(1)
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (800, 600)}))
# Enabe Auto-focus, currently for Camera Module 3 only.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
picam2.start()
while True:
im= picam2.capture_array()
cv2.imshow("Camera", im)
key = cv2.waitKey(1)
if key==ord('s'):
timeStamp = time.strftime("%Y%m%d-%H%M%S")
targetPath="/home/pi/Desktop/img" + "_"+timeStamp+".jpg"
cv2.imwrite(targetPath, im)
print("- Saved:", targetPath)
elif key==ord('q'):
print("Quit")
break
cv2.destroyAllWindows()
picam2_cv2_convert_color.py, convert to gray/switch color channel.
"""
Picamera2 + OpenCV exercise:
Run on Raspberry Pi 5 + Camera Module 3,
capture image using capture_array() and display using cv2.imshow().
- Convert to gray/ convert color channel.
Press:
S/s to save
Q/q to quit
My cameras:
Picamera2(0) - Camera Module 3 NoIR Wide
Picamera2(1) - Camera Module 3
"""
import cv2
import picamera2
from libcamera import controls
import platform
from importlib.metadata import version
import time
print("Python:", platform.python_version())
print(picamera2.__name__ + ":", version(picamera2.__name__))
print(cv2.__name__ + ":", cv2.__version__)
print()
time.sleep(1)
cv2.startWindowThread()
picam2 = picamera2.Picamera2(1)
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (800, 600)}))
# Enabe Auto-focus, currently for Camera Module 3 only.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
picam2.start()
while True:
im_org= picam2.capture_array()
#cv2.imshow("Camera", im)
#im = cv2.cvtColor(im_org, cv2.COLOR_RGB2GRAY) # convert to gray
im = cv2.cvtColor(im_org, cv2.COLOR_RGB2BGR) # RGB to BGR
cv2.imshow("Camera", im)
key = cv2.waitKey(1)
if key==ord('s'):
timeStamp = time.strftime("%Y%m%d-%H%M%S")
targetPath="/home/pi/Desktop/img" + "_"+timeStamp+".jpg"
cv2.imwrite(targetPath, im)
print("- Saved:", targetPath)
elif key==ord('q'):
print("Quit")
break
cv2.destroyAllWindows()
picam2_cv2_face_detect.py, with face-detection/eye-detection.
"""
Picamera2 + OpenCV exercise:
Run on Raspberry Pi 5 + Camera Module 3,
capture image using capture_array() and display using cv2.imshow().
- with face detection
Press:
S/s to save
Q/q to quit
My cameras:
Picamera2(0) - Camera Module 3 NoIR Wide
Picamera2(1) - Camera Module 3
"""
import cv2
import picamera2
from libcamera import controls
import platform
from importlib.metadata import version
import time
print("Python:", platform.python_version())
print(picamera2.__name__ + ":", version(picamera2.__name__))
print(cv2.__name__ + ":", cv2.__version__)
print()
time.sleep(1)
face_detector = cv2.CascadeClassifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml")
#face_detector = cv2.CascadeClassifier("/usr/share/opencv4/haarcascades/haarcascade_eye.xml")
cv2.startWindowThread()
picam2 = picamera2.Picamera2(1)
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (800, 600)}))
# Enabe Auto-focus, currently for Camera Module 3 only.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
picam2.start()
while True:
im= picam2.capture_array()
grey = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
faces = face_detector.detectMultiScale(grey, 1.1, 5)
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0))
cv2.imshow("Camera", im)
key = cv2.waitKey(1)
if key==ord('s'):
timeStamp = time.strftime("%Y%m%d-%H%M%S")
targetPath="/home/pi/Desktop/img" + "_"+timeStamp+".jpg"
cv2.imwrite(targetPath, im)
print("- Saved:", targetPath)
elif key==ord('q'):
print("Quit")
break
cv2.destroyAllWindows()
ref:
~ Github / raspberrypi / picamera2 (specially opencv_face_detect.py example)
Comments
Post a Comment