# Capturing video stream from USB Cam using OpenCV

**URL:** <https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348>\
**Category:** Jetson TX1\
**Tags:** opencv\
**Created:** [February 11, 2016, 3:54pm UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348 "2016-02-11T15:54:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SagiS](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sagis/32/17483_2.png) [@SagiS](https://forums.developer.nvidia.com/u/SagiS)\
**Post date:** [February 11, 2016, 3:54pm UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/1 "2016-02-11T15:54:32Z")

</div>

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:

```auto
#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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Leow](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Leow](https://forums.developer.nvidia.com/u/Leow)\
**Post date:** [February 15, 2016, 7:24am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/2 "2016-02-15T07:24:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kayccc](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kayccc/32/14043_2.png) [@kayccc](https://forums.developer.nvidia.com/u/kayccc)\
**Post date:** [March 14, 2016, 7:28am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/3 "2016-03-14T07:28:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sandmen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sandmen](https://forums.developer.nvidia.com/u/sandmen)\
**Post date:** [April 11, 2016, 6:38am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/4 "2016-04-11T06:38:40Z")

</div>

> [@](#):
>
> 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 ?

---

<div class="post-metadata">

**Author:** ![kayccc](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kayccc/32/14043_2.png) [@kayccc](https://forums.developer.nvidia.com/u/kayccc)\
**Post date:** [April 11, 2016, 7:34am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/5 "2016-04-11T07:34:46Z")

</div>

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:  
[url][https://devtalk.nvidia.com/default/topic/917386/jetson-tx1/usb-3-0-port-unstable-on-jetson-tx1-/post/4835793/#4835793[/url]](https://devtalk.nvidia.com/default/topic/917386/jetson-tx1/usb-3-0-port-unstable-on-jetson-tx1-/post/4835793/#4835793%5B/url%5D) as the temporary solution before it’s released.

Thanks

---

<div class="post-metadata">

**Author:** ![han\_qiu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@han\_qiu](https://forums.developer.nvidia.com/u/han_qiu)\
**Post date:** [April 18, 2017, 1:52am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/6 "2017-04-18T01:52:31Z")

</div>

Hi~ Is it solved now?

---

<div class="post-metadata">

**Author:** ![kayccc](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kayccc/32/14043_2.png) [@kayccc](https://forums.developer.nvidia.com/u/kayccc)\
**Post date:** [April 26, 2017, 8:57am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/7 "2017-04-26T08:57:34Z")

</div>

Hi han\_qiu,

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

Thanks

---

<div class="post-metadata">

**Author:** ![Masoud\_afra](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/masoud_afra/32/11158_2.png) [@Masoud\_afra](https://forums.developer.nvidia.com/u/Masoud_afra)\
**Post date:** [June 22, 2017, 2:56am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/8 "2017-06-22T02:56:31Z")

</div>

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 ^\_^

---

<div class="post-metadata">

**Author:** ![kayccc](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kayccc/32/14043_2.png) [@kayccc](https://forums.developer.nvidia.com/u/kayccc)\
**Post date:** [June 29, 2017, 5:51am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/9 "2017-06-29T05:51:30Z")

</div>

> [@](#):
>
> 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/](https://devtalk.nvidia.com/default/board/188/jetson-tx2/)

Thanks

---

<div class="post-metadata">

**Author:** ![Masoud\_afra](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/masoud_afra/32/11158_2.png) [@Masoud\_afra](https://forums.developer.nvidia.com/u/Masoud_afra)\
**Post date:** [June 29, 2017, 7:38am UTC](https://forums.developer.nvidia.com/t/capturing-video-stream-from-usb-cam-using-opencv/41348/10 "2017-06-29T07:38:36Z")

</div>

Hello

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

Thanks.
