opencv-python icon indicating copy to clipboard operation
opencv-python copied to clipboard

[bug] camera.read() not returning any data

Open dhaval-khatri1996 opened this issue 1 year ago • 8 comments

openCv python version 4.5.5. Camera module waveshare OV9281-110 I am working with raspberry-pi model 4-B with latest bullseye OS.

I am able to capture image using libcamera-still -o test.png command but when I try to read an image with opencv it returns None. The output for cam.isOpened() is true but cam.grab() and cam.read() return false and (false,None) respectively.

Code:

import cv2
cam_port = 0
cam = cv2.VideoCapture(cam_port)
print(cam.isOpened())
print(cam.grab())
print(cam.read())
cam.release()

Output:

True
False
(False, None)

camera info


~ $ v4l2-ctl --device /dev/video0 --all

Driver Info:
	Driver name      : unicam
	Card type        : unicam
	Bus info         : platform:fe801000.csi
	Driver version   : 5.15.56
	Capabilities     : 0xa5a00001
		Video Capture
		Metadata Capture
		Read/Write
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x25200001
		Video Capture
		Read/Write
		Streaming
		Extended Pix Format
Media Driver Info:
	Driver name      : unicam
	Model            : unicam
	Serial           : 
	Bus info         : platform:fe801000.csi
	Media version    : 5.15.56
	Hardware revision: 0x00000000 (0)
	Driver version   : 5.15.56
Interface Info:
	ID               : 0x03000005
	Type             : V4L Video
Entity Info:
	ID               : 0x00000003 (3)
	Name             : unicam-image
	Function         : V4L2 I/O
	Flags         : default
	Pad 0x01000004   : 0: Sink
	  Link 0x02000007: from remote pad 0x1000002 of entity 'ov9281 10-0060': Data, Enabled, Immutable
Priority: 2
Video input : 0 (unicam-image: ok)
Format Video Capture:
	Width/Height      : 640/480
	Pixel Format      : 'BGR3' (24-bit BGR 8-8-8)
	Field             : None
	Bytes per Line    : 1920
	Size Image        : 921600
	Colorspace        : sRGB
	Transfer Function : sRGB
	YCbCr/HSV Encoding: ITU-R 601
	Quantization      : Full Range
	Flags  

dhaval-khatri1996 avatar Sep 01 '22 16:09 dhaval-khatri1996

Could you run cheese or some other web cam app to ensure that v4l2 interface works well. Also, OpenCV prints warnings, if something goes wrong in v4l2 back-end. Please add them to logs, if any.

asmorkalov avatar Sep 05 '22 07:09 asmorkalov

@asmorkalov thank you for your time, I just checked the camera with cheese. There is a weird behavior here the camera feed looks clear, as in when viewing camera using cheese it looks fine but when I save an image it shows a picture with random colored pixels(static colored noise) and when I try to record a video cheese gets stuck. The video feed without clicking image or taking video is as it should be, but as soon as I click an image the image, cheese stores just pixel noise with a green patch and when trying to capture video cheese gets stuck.

Note: libcamera-still gives a perfect image no issues.

and as for the opencv warnings yes I did get some warnings after I built opencv in debug mode, find the complete output of the above code below:

[ INFO:[email protected]] global /home/work/opencv-4.5.5/modules/videoio/src/videoio_registry.cpp (223) VideoBackendRegistry VIDEOIO: Enabled backends(7, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); UEYE(940)
[ INFO:[email protected]] global /home/work/opencv-4.5.5/modules/videoio/src/backend_plugin.cpp (369) getPluginCandidates VideoIO plugin (GSTREAMER): glob is 'libopencv_videoio_gstreamer*.so', 1 location(s)
[ INFO:[email protected]] global /home/work/opencv-4.5.5/modules/videoio/src/backend_plugin.cpp (379) getPluginCandidates     - /usr/local/lib: 0
[ INFO:[email protected]] global /home/work/opencv-4.5.5/modules/videoio/src/backend_plugin.cpp (383) getPluginCandidates Found 0 plugin(s) for GSTREAMER
[ INFO:[email protected]] global /home/work/opencv-4.5.5/modules/videoio/src/cap_v4l.cpp (602) setFps VIDEOIO(V4L2:/dev/video0): can't set FPS: 30
4.5.5
True
(False, None)


dhaval-khatri1996 avatar Sep 05 '22 11:09 dhaval-khatri1996

I try to record a video cheese gets stuck - it's expected behavior. Cheese uses CPU codec to encode video. It's not capable to encode video on pi in real time. I just tried couple of my boards:

  • Raspberry 1b + Raspberry Pi Camera Module v2 (IMX219), OpenCV 4.6.0.66 (pip install)
  • Raspberry 3 + Raspberry Pi Camera Module v1 (OV5647), OpenCV 4.6.0.66 (pip install) And found out that the issue correlates with camera mode in raspi-config. Legacy v4l2 mode works well. In case I disable legacy camera mode I get the same issue like you. So it could be some issue on OpenCV side in C++ code. Let me check and I'll return back.

asmorkalov avatar Sep 07 '22 10:09 asmorkalov

@asmorkalov any updates my friend?

dhaval-khatri1996 avatar Sep 14 '22 02:09 dhaval-khatri1996

Not yet for now. Please switch to legacy camera mode as work around till the issue is not fixed on our side.

asmorkalov avatar Sep 14 '22 06:09 asmorkalov

@asmorkalov please some kind of update would be good, thank you for your time

dhaval-khatri1996 avatar Oct 11 '22 07:10 dhaval-khatri1996

@asmorkalov I think I found the issue. So as I was unable to use opencv, I tried to use libcamera code directly. On using that code I realized that it is picking up wrong image format. For example in my case the image format is supposed YUYV but for some reason the libcamera code was taking NV12 as the format and hence when the code tried to convert the captured buffer frame to actual image and failed with None as return value. So I assigned the format manually and it worked like a charm.

So I think same is happening with OpenCV as well. Let me know what you think.

dhaval-khatri1996 avatar Oct 16 '22 20:10 dhaval-khatri1996

I found the same behaviour. Trying to fix the issue to have cv::VideoCapture usable without tricks.

asmorkalov avatar Oct 17 '22 06:10 asmorkalov

@asmorkalov just googled every possible angle of internet and expect moving back to legacy camera no other solution seems to exist

VideoCapture(0), VideoCapture(-1), VideoCapture('/dev/video0', cv2.CAP_V4L) etc simply lead to the same error related to the empty frame/image.

Stefanotuv avatar Oct 26 '22 11:10 Stefanotuv

Please ensure that you completely switched back to legacy solution and reboot the system. Legacy version worked for me with several devices.

asmorkalov avatar Oct 26 '22 11:10 asmorkalov

@asmorkalov the issue is that the rest of code works with picamera2... so it would stop working, thats why i am stuck on try to see what a solution or work around can be

Stefanotuv avatar Oct 26 '22 12:10 Stefanotuv