I can't get opencv to detect my USB camera. The system seem to detect and even take photos when I use other tools, but with opencv it is not working.
When i list the usb devices:
lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 006: ID 258a:002a SINO WEALTH Gaming KB
Bus 001 Device 005: ID 18f8:0f97 [Maxxter] Optical Gaming Mouse [Xtrem]
Bus 001 Device 004: ID 214b:7250 Huasheng Electronics USB2.0 HUB
Bus 001 Device 003: ID 214b:7250 Huasheng Electronics USB2.0 HUB
Bus 001 Device 010: ID 05a3:9230 ARC International Camera
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
When I try to capture an image with the camera using the fswebcam, it works perfectly.
fswebcam -r 1280x720 --no-banner test.jpg
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Disabling banner.
Writing JPEG image to 'test.jpg'.
But when i try to capture the image using opencv:
I tried with /dev/videox
>>> import cv2
>>>
>>> cam = cv2.VideoCapture('/dev/video0')
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1824) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module source reported: Could not read from resource.
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
And with the number
>>> import cv2
>>>
>>> cam = cv2.VideoCapture(0)
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (961) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
>>>
Im using:
- Raspberry Pi 4 Model B Rev 1.5
- Raspbian
- python 3.9.2
- pip 23.2.1
- python3-opencv==4.5.1 (with 'sudo apt install python3-opencv')
(obs: I couldn't install opencv with pip opencv-python)
Okay, I found a solution that worked for me. I had to specify the video api in the VideoCapture, using the V4L2 api, the final code looked like this:
I hope it helps someone who has the same problem :)