Got a bit of problem trying to use VideoCapture capability of OpenCV.
After the VideoCapture.read() I get the "VIDIOC_STREAMON: Invalid Argument" error.
I am using Raspberry Pi 3 with Camera Module installed (this is working fine, as I am able to run my application to detect Aruco Markers)
I am trying to improve aforementioned application, which currently captures images, detects markers on them, and displays them using imshow. I want to be able to detect markers in real-time video capture, just like on this video: https://www.youtube.com/watch?v=IsXWrcB_Hvs
Here is the code I'm trying to run:
def continuuosDetection(self):
cap = cv2.VideoCapture(-1)
if not cap.isOpened():
print("The camera didn\'t open correctly")
cap.open(-1)
print(cap.isOpened())
ret, frame = cap.read()
cap.release()
if not cap.isOpened():
print("Camera shut properly")
I'm using Python 3.5, and as mentioned camera module is working fine with different function, although I am using PiCamera.capture() method to take images. For the VideoCapture() I used both -1 and 0 as camera IDs.
print(cap.isOpened())
This returns 'True' every single time. So it seems like the camera is opening correctly(?)
print(ret)
print(frame)
These return 'False' and 'None', hence I can't display the capture using cv2.imshow.
I used:
sudo mobprobe bcm2835-v4l2
As I found on one forum, but this doesn't solve my issue.
And suggestions or advices would be highly appreciated.
I am still trying to sort this problem, so if I find the answer, I will include it here. Although so far I've been scrolling google for past few hours trying to find a solution and I'm still in the woods.
Regards,
The problem here is that you're mixing the "legacy" camera stack and the new libcamera-based stack. If your camera is working with PiCamera.capture(), then your Raspberry Pi is configured to use the new stack, but that means that cv2.VideoCapture() won't work anymore, at least not with the defaults.
There are two possible solutions:
Switch back to the legacy camera stack. Run sudo raspi-config, go to "Interface Options" and enable "Legacy Camera Stack", then reboot. However, this will then cause PiCamera.capture() to fail (because it only works with the new stack).
Use GStreamer as a video source in OpenCV. One example of how to do this is here: https://github.com/Qengineering/Libcamera-OpenCV-RPi-Bullseye-32OS/blob/main/main.cpp#L26-L35 (note this is in C++, but the solution in Python should be very similar).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With