Problem with gstreamer on Raspberry Pi 4

Hello, I’m working on a project for my mechatronics engineering coursewhere I need to detect circles in real time using a Raspberry Pi 4 Model B with a Raspberry Pi Rev 1.3 camera. I have configured the entire environment and ensured that the Raspberry correctly detects the camera. Furthermore, I have also reinstalled OpenCV and made the necessary updates.

The problem arises when trying to capture video with OpenCV. I’m getting several warning messages related to GStreamer and a fatal error when trying to convert the captured frame to grayscale, telling me that the frame is probably empty or not being captured correctly. Here is the code I am using and the specific errors I am getting:
Code:

import cv2
import numpy as np

# Video capture
cap = cv2.VideoCapture(0)

# Adjust image resolution (optional)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)

# Defines a color range to detect red circles
lower_red = np.array([160, 50, 50])
upper_red = np.array([180, 255, 255])

while True:
     # Get the current frame of the camera
     ret, frame = cap.read()

     # Convert the frame to grayscale
     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

     # Apply Gaussian smoothing filter
     blurred = cv2.GaussianBlur(gray, (5, 5), 0)

     # Detect edges in blurred image
     edges = cv2.Canny(blurred, 50, 150)

     # Search for circles in the border image
     circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp=1, minDist=100, param1=100, param2=30)

     # Draw the circles detected in the original frame
     if circles is not None:
         for (x, y, r) in circles[0]:
             cv2.circle(frame, (int(x), int(y)), int(r), (0, 255, 0), 2)

     # Shows the frame with the detected circles
     cv2.imshow('Circle detection', frame)

     # If the 'q' key is pressed, the window is closed
     if cv2.waitKey(1) & 0xFF == ord('q'):
         break

# Release the camera and close all the windows
cap.release()
cv2.destroyAllWindows()

Errors:

[ WARN:0@0.339] 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:0@0.340] global ./modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
cv2.error: OpenCV(4.6.0) ./modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

I have verified that the camera works correctly outside of OpenCV and that updates are up-to-date. Has anyone faced a similar issue or have any suggestions on how to resolve these GStreamer errors and ensure video capture works properly?

I appreciate any help or suggestions you can offer.

that’s a problem specific to raspberry pi. you need to ask on their forum about this.

maybe you can use the V4L backend directly (w/o gstreamer issues):

VideoCapture(0, cv2.CAP_V4L)

I already asked them, but they sent me here

replicate the issue with plain gstreamer on the command line. then you will know that it’s not an opencv issue.

or do what berak suggested, bypass gstreamer.

when in doubt, always go to the raspberry pi forum, and stick with the raspberry pi forum, because your situation involves raspberry pi.