I was in the same boat you were about 4 days ago. I found that using picamera 2 was the only way I could get it to work.
from picamera2 import Picamera2
from ultralytics import YOLO
import cv2
import numpy as np
# Load the YOLO model
# Initialize the Raspberry Pi camera
picam2 = Picamera2()
config = picam2.create_preview_configuration(main={"size": (640, 480)})
picam2.configure(config)
picam2.start()
# Function to process frames with YOLO
def process_frame(frame):
# Display video feed
try:
print("Press 'q' to quit.")
while True:
# Capture frame from the Raspberry Pi camera
frame = picam2.capture_array()
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Convert to OpenCV's BGR format
# Process frame with YOLO
processed_frame = process_frame(frame)
# Show the video feed
cv2.imshow("Video Feed", processed_frame)
# Exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
picam2.stop()
cv2.destroyAllWindows()
The problem I am trying to fix is to display the video feed on the display so that I can see the bounding boxes on the objects YOLO detected. But cv2.imshow does not seem to work for some reason. The other parts of the code work tho.