HIGHGUI ERROR: V4L/V4L2 while opening camera with OpenCV4Tegra, L4T r24

Hello,
I flashed my TX1 using JetPack 2.2 and installed r24. I am trying to access the camera to capture frames and getting:

HIGHGUI ERROR: V4L/V4L2: VIDIOC_CROPCAP
select timeout
select timeout
Segmentation fault

I ran the following C++ code:

//Build using g++ opencv.cpp `pkg-config --cflags --libs opencv`
#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())
        return -1;
    

    namedWindow("Preview",1);
    Mat frame;
    for(;;)
    {
        cap >> frame; // get a new frame from camera
        imshow("Preview", frame);
        if(waitKey(2) >= 0) break;
    }
    return 0;
}

I tried reading various other posts here but couldn’t get it to work.
What could be the problem here?

Regards,
Ankit

I’m not an OpenCV developer, but you could compile with debug symbols (-g) and run it in gdb…when it fails get a stack frame with backtrace command (bt). Then “l” to list the lines of code surrounding the seg fault. You may want to manually find out what the output is from the command “pkg-config --cflags --libs opencv” and substitute this directly for the backquoted “pkg-config --cflags --libs opencv”…if there is any optimizing (like -O#), remove that, add the “-g”.

Hi Ankit
I have exactly same error. Did you find a solution to run capture device?
Thanks,
-Sezai

Hi ankitdhall and Sezai,

With default camera, you will need to use “nvcamerasrc” pipeline instead of the default OpenCV cam value(-1 or 0).

see here: https://devtalk.nvidia.com/default/topic/925923/jetson-tx1/can-not-open-the-camera-on-tx1-via-opencv/post/4841289/#4841289

Thanks

Hello kayccc,
I tried that as well but the device is unable to open the camera.

char* gst = "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)I420, framerate=(fraction)24/1 ! nvvidconv flip-method=2 ! videoconvert ! appsink";
    VideoCapture cap(gst);

I am using OpenCV 2.4.13 on the TX1 after failed attempts to read on-board camera using the default OpenCV.

Can you give a working code snippet to access the camera?

Regards,
Ankit

Hi ankitdhall,

We did the test with OpenCV 3.1.0 with onboard camera, we can see it work successfully.
Here are some tips to open the default camera

  1. Please try openCV version be 3.0.0 or higher. we’re using 3.1.0
  2. GST 1.0 and related plugins must be installed.
  3. test program must be linked against built openCV 3.1.0 libs
  4. test code looks like:
VideoCapture cap("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)24/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"); //open the default camera

if(!cap.isOpened()) { // check if we succeeded
cerr << "Fail to open camera " << endl;
return -1;
}
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("original", frame);
waitKey(1);
}
// the camera will be deinitialized automatically in VideoCapture destructor
cap.release();

Regarding OpenCV4Tegra, it’s not well optimized for onboard camera due to pixel format issue, but USB webcam can work.

Thanks

@kayccc
hi kayccc,
as u said above, since there’s no articles about how to install opencv3.1 on TX1 board , could u please tell us how to install opencv3.1 (can use NVIDIA’s CPU optimizations )

Hi soga_zh,

To optimize OpenCV 3.x.x for Tegra is still under planning, no clear schedule yet.

If you still want to use OpenCV 3.x, you could try to compile and install OpenCV 3.x by referring the below forum thread:
https://devtalk.nvidia.com/default/topic/917386/jetson-tx1/usb-3-0-port-unstable-on-jetson-tx1-/post/4835793/#4835793

After built your own version of OpenCV (using the public open-source code), you will still have the full CUDA-optimized “gpu” module but not the CPU optimizations of OpenCV4Tegra.

Thanks

Hi kayccc,

Thank for your timely help!

Hi kayccc,

The code snippet works perfectly with OpenCV 3.1.0 on the TX1.

Regards,
Ankit

Hi, I’m trying to get the above code to work (with the onboard camera), however running into a few problems. With OpenCV 2.4.9 I’m unable to open the camera stream as expected, and I’m having problems installing OpenCV 3.1.0. I followed the instructions in the forum thread linked above (https://devtalk.nvidia.com/default/topic/917386/jetson-tx1/usb-3-0-port-unstable-on-jetson-tx1-/post/4835793/#4835793) however I get several segfaults and eventually a crash.

For example:

[ 56%] Building CXX object modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/thresh.cpp.o

c++: internal compiler error: Segmentation fault (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions.
make[2]: *** [modules/imgproc/CMakeFiles/opencv_imgproc.dir/src/thresh.cpp.o] Error 4
make[1]: *** [modules/imgproc/CMakeFiles/opencv_imgproc.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

and finally

[ 56%] Built target opencv_cudaarithm
make: *** [all] Error 2

Any ideas why this may be failing? I’m able to access the camera through

gst-launch-1.0 nvcamerasrc fpsRange="30.0 30.0" ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! nvtee ! nvvidconv flip-method=2 ! 'video/x-raw(memory:NVMM), format=(string)I420' ! nvoverlaysink -e

at least, but idaelly need it in the openCV framework.

Thanks,
Brian

Hi Brian_I,

Regarding your OpenCV 3.1.0 install issue, that might be the known issue, OpenCV compiling frequently crash in R24.1 64-bit environment. Please repeat compiling till it’s done, or to wait for next release.

About the GST issue, please make sure that GST is installed correctly. Run a simple nvcamerasrc pipeline to make sure that on-board camera runs well.

For on-board camera, please paste the test code, and the crash log with GST debug information.

Thanks

@kayccc

Hi kayccc,

I have a camera pluged in TX1,how can I get the camera worked successfully with opencv3.1 (not the default camera)?could you please show me the codes then?
thank you very much!

Hi soga_zh,

What’s the camera you plugged in TX1?
If it’s USB camera, you could refer to below topic:
https://devtalk.nvidia.com/default/topic/948951/?comment=4923255

Thanks

Thanks kayccc, you were right, it was that same install issue. After about 100 times it finally made it through (the last one requiring a sudo). I wonder if it could have been caused by me already having the default OpenCV 2.4.9 in there?

I didn’t format my post well on the GST issue, that part was actually working fine and I’ve now gotten the code snippet to work with OpenCV.

Thanks!

@kayccc

Hi kayccc,

I’ve seen and tested that code, but due to different types of cam, the code there seems no help. My camera is Logitech, a USB camera, it’s not the camera (0-1) in opencv ,so I want to know how to make the camera work well , I’ll appreciate it if you show us the code about how to make the Logitech camera work successfully.
thx very much!

the error goes :from element /GstPipeline0/GstV4l2src0: Internal data flow error
error: pipeline doesn’t want to preroll

note : in the console , I use the follow orders:
gst-launch-1.0 -v v4l2src ! image/jpeg, width=3840, height=2160, framerate=30/1 ! nvjpegdec ! “video/x-raw, format=I420” ! nvoverlaysink

Hi soga_zh,

For USB camera, it should be a standard V4L2 device. you can test it with v4l2 interface.

More info could be found from below link:
http://elinux.org/Jetson/Cameras#Camera_access

Hope this can help on your case.

Thanks

USB webcam on Jetson TX1/TX2 could be accessed via OpenCV’s GStreamer-pipeline-based VideoCapture(). I shared my code in the blog post below. You’d have to build OpenCV 3.x with gstreamer supported enabled to get it to work though.

“How to Capture and Display Camera Video with Python on Jetson TX2”: https://jkjung-avt.github.io/tx2-camera-with-python/

def open_cam_usb(dev, width, height):
    # We want to set width and height here, otherwise we could just do:
    #     return cv2.VideoCapture(dev)
    gst_str = "v4l2src device=/dev/video{} ! video/x-raw, width=(int){}, height=(int){}, format=(string)RGB ! videoconvert ! appsink".format(dev, width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)