VideoCapture is open, but unable to read frames (Raspberry pi)

  • Working on a Raspberry Pi 4B
  • Raspberry Pi OS
  • Raspberry Pi camera module 3
  • Camera works fine using commandline rpicam-vid

I Have not enabled legacy camera in raspi-config since that is no longer an option

Video capture works fine using files, but i cannot get a live acces to a camera.

I have tried index -1, 0 and 1 for the devices but only ‘0’ responds.

I have tried most of the APIs but only V4L and GStreamer results in anything.

#include <stdio.h>

#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>

#include "vision.h" 

int main(int argc, char** argv )
{
	cv::Mat frame;
	cv::VideoCapture vid;

	printf("CAP_V4L  : ");
	vid.open(0,cv::CAP_V4L);
	cv::waitKey(2000);
	if(!vid.isOpened()){
		printf("Cam not opened\n");
	}else{
		printf("Cam opened!\n");
		double dWidth = vid.get(cv::CAP_PROP_FRAME_WIDTH);
		double dHeight = vid.get(cv::CAP_PROP_FRAME_HEIGHT);
		printf("Frame size : %f x %f\n",dWidth ,  dHeight);
				
		if(vid.grab()){// used to be if(vid.read(frame)){ 
			//imshow("LIVE!", frame);   
			printf("grabbed\n");
			cv::waitKey(0);
		}else{
			vid.release();
			printf("no frame\n");
		}		
	}

	

	std::cout << "CAP_GSTREAMER : " << std::endl;
	vid.open(0,cv::CAP_GSTREAMER);
	std::cout << "DOES NOT REACH HERE : " << std::endl;
	if(!vid.isOpened()){
		printf("Cam not opened\n");
	}else{
		printf("Cam opened!\n");
		vid.read(frame); 
		imshow("LIVE!", frame);   
		cv::waitKey(0);
	}


    return 0;
}

This code response:

CAP_V4L  : Cam opened!
Frame size : 640.000000 x 480.000000
[ WARN:0@10.060] global ./modules/videoio/src/cap_v4l.cpp (1013) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
no frame
CAP_GSTREAMER : 

Then does nothing until I ctrl+C

you are not the first with this problem. what did your internet research show about this?

Well most of what i found says to turn on legacy camera for the Pi, but that is not an option any more. The other related problems that i have found are sometimes able to read frames, but mine never does. Can you point me in the direction of a solution?

the solution has been for years to not use OpenCV for accesing such cameras, but to use other libraries. this forum must have several threads asking about this. OpenCV does not yet have the ability to handle these cameras that use a non-standard interface. V4L/V4L2 would be standard. these cameras use libcamera/libcamera2.

1 Like