Capturing video stream from USB Cam using OpenCV

I’ve been trying to capture a live video stream from a USB Cam using the simplest cookbook example from OpenCV (C++), but I’m getting errors:

The code:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
 
int main() {
    VideoCapture stream1(0); 
    if (!stream1.isOpened()) { 
        cout << "cannot open camera";
    }
   
    while (true) {
        Mat cameraFrame;
        stream1.read(cameraFrame);
        imshow("cam", cameraFrame);
        if (waitKey(30) >= 0)
            break;
        }
        return 0;
    }

I’m getting an error:

Unable to stop the stream.: Device or resource busy
Unable to stop the stream.: Bad file descriptor
VIDIOC_STREAMON: Bad file descriptor
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /hdd/buildbot/slave_jetson_tx_2/35-O4T-L4T-Jetson-L/opencv/modules/highgui/src/window.cpp, line 261
terminate called after throwing an instance of 'cv::Exception'
  what():  /hdd/buildbot/slave_jetson_tx_2/35-O4T-L4T-Jetson-L/opencv/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

Aborted

I have tried this on two separate TX1 dev boards, with two different usb cams, and I’m getting the same results. Note that I can see the USB cam in /sys/class/video4linux/ and it is indeed device 0, and I can see a live feed in cheese, the video capture program that comes with ubuntu.

Any ideas?

I am having the same problem too. Tried running with some other codes, having the same error:
Unable to stop the stream.: Device or resource busy

Leow

Hi charlyn,

Thank you for reporting this issue, we’re investigating the error you mentioned above, and will update the status to you once we clarified it.

Thanks

Any update ?

Hi sandmen,

We’re going to have an update version of OpenCV4Tegra in coming release to resolve the USB video capturing (live streaming) issue.

Currently, you could try to compile and install OpenCV 3.1.0 by referring the below thread:
https://devtalk.nvidia.com/default/topic/917386/jetson-tx1/usb-3-0-port-unstable-on-jetson-tx1-/post/4835793/#4835793 as the temporary solution before it’s released.

Thanks

Hi~ Is it solved now?

Hi han_qiu,

The OpenCV4Tegra v2.4.13 is still the latest version for TX1, is there any specific requirement on your project?

Thanks

Hello

I actually have the same problem now and Tegra X2 is my platform. Tegra X1 also has some strange behavior in detection of the same camera but it is detecting now on Tegra X1.
For a simple code to get camera frames on Tegra X2 I get the following error :

libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT

and regardless of the fact that Tegra X2 has many installation problems (ex, Jetpack3 does not install GPU functions so you have to build it again) and it is solved now on another thread, it is my problem to get my camera to use now :(

I have installed all of the libraries and have no idea why Tegra X2 thinks my camera is BUSY all the time :/

Please help again ^_^

Hi Masoud_afra,

Please file TX2 issue into https://devtalk.nvidia.com/default/board/188/jetson-tx2/

Thanks

Hello

I have resolved this problem in another thread and you may delete this from here.

Thanks.