Week 03 ‐ Interfacing Raspberry Pi Camera Module and Servos Motors - AkinduID/EyeRiz GitHub Wiki
- achieve basic functionality of raspberry pi camera and servo motor
- using opecv with raspberry pi camera
- Raspberry pi 4 model B - 2GB RAM with raspberrry pi(bookworm)
- Raspberry Pi Cooling Fan
- Raspberry Pi Noir Camera v2
- SG90 Servo Motor (Mounted to the Bracket)
I created a Python script to control the SG90 servo motor using PWM on the Raspberry Pi. The script worked as expected and successfully controlled the servo.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(17, GPIO.OUT)
servo = GPIO.PWM(11, 50)
servo.start(0)
try:
servo.ChangeDutyCycle(2)
time.sleep(1)
servo.ChangeDutyCycle(4)
time.sleep(1)
servo.ChangeDutyCycle(6)
time.sleep(1)
servo.ChangeDutyCycle(8)
time.sleep(1)
servo.ChangeDutyCycle(10)
time.sleep(1)
servo.ChangeDutyCycle(12)
time.sleep(1)
servo.ChangeDutyCycle(2)
time.sleep(1)
finally:
servo.stop()
GPIO.cleanup()
The servo motor responded correctly to the script, verifying successful integration with the Raspberry Pi.
I connected the Raspberry Pi NoIR Camera v2 to the Pi and wrote a simple script to test the camera functionality.
from picamera2 import Picamera2, Preview
from time import sleep
picam2 = Picamera2()
picam2.start_preview(Preview.QTGL)
picam2.start()
sleep(30)
picam2.close()
The camera worked, but the frame rate was slow. I then attempted to use OpenCV to capture the video feed, hoping to improve performance.
import cv2
# Initialize the video capture object
cap = cv2.VideoCapture(0)
try:
# Set video resolution (optional)
if cap.isOpened():
desired_width = 240
desired_height = 320
cap.set(cv2.CAP_PROP_FRAME_WIDTH, desired_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, desired_height)
# Check if the camera opened successfully
if not cap.isOpened():
raise Exception("Error opening video stream")
while True:
# Read a frame from the camera
ret, frame = cap.read()
if not ret:
# Handle the case where no frame is received
break
# Display the frame
cv2.imshow('frame', frame)
# Press 'q' to quit
if cv2.waitKey(1) == ord('q'):
break
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Release the video capture object and close windows
cap.release()
cv2.destroyAllWindows()
print("Successfully closed video stream and windows.")
Unfortunately, this code encountered a memory allocation issue. Most likely due to the row memory of the Raspberry Pi Board.
pi@raspberrypi:~/cam $ python3 opencvCam.py
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (2401) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Can't receive frame (stream end?). Exiting.
Successfully closed video stream and windows.
I attempted to fix this issue by lowering the frame rate and resolution, but this conflics with the project’s goal of creating a face-tracking webcam, which requires a video stream of average quality and good framerate.
- Raspberry Pi does not have sufficient power to achieve the required functionality of the project.
- Pause following Option 4 and get started with Option 1
- Buy a cheap low quality webcam
- Study OpenCV Cascade Classifiers
- Reducing frame rate and resolution would negatively affect the camera’s suitability for facial tracking and webcam functionality.
- Resolving this issue would require significant time investment, which could be better spent on achieving the basic project goals using a standard USB webcam.(Option 1)