VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV Unable to stop the stream: Device or resource busy

Running a private code that reads the data from the Webcam, I get the following error:

Loading weights from handyolo/weights/000010.weights... Done!
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:Creating instance of the ImageHeatmapsPose2dZrelZAll_FPN model.
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:n_iter: 1
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:improvement_per_itr: 0.01
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:beta: 5.0
INFO:exp11_fm64_iter1_l2_rmsprop_feaadd_zrel3_transformer_Test:Creating instance of L1 Loss.
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy
Error getting frame from device 0

Here is the camera info:

$ v4l2-ctl -d /dev/video0 --all
Driver Info (not using libv4l2):
    Driver name   : uvcvideo
    Card type     : HD Pro Webcam C920
    Bus info      : usb-0000:00:14.0-9
    Driver version: 4.13.13
    Capabilities  : 0x84200001
        Video Capture
        Streaming
        Extended Pix Format
        Device Capabilities
    Device Caps   : 0x04200001
        Video Capture
        Streaming
        Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
    Width/Height      : 320/180
    Pixel Format      : 'YUYV'
    Field             : None
    Bytes per Line    : 640
    Size Image        : 115200
    Colorspace        : sRGB
    Transfer Function : Default
    YCbCr Encoding    : Default
    Quantization      : Default
    Flags             : 
Crop Capability Video Capture:
    Bounds      : Left 0, Top 0, Width 320, Height 180
    Default     : Left 0, Top 0, Width 320, Height 180
    Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 320, Height 180
Selection: crop_bounds, Left 0, Top 0, Width 320, Height 180
Streaming Parameters Video Capture:
    Capabilities     : timeperframe
    Frames per second: 30.000 (30/1)
    Read buffers     : 0
                     brightness (int)    : min=0 max=255 step=1 default=128 value=128
                       contrast (int)    : min=0 max=255 step=1 default=128 value=128
                     saturation (int)    : min=0 max=255 step=1 default=128 value=128
 white_balance_temperature_auto (bool)   : default=1 value=1
                           gain (int)    : min=0 max=255 step=1 default=0 value=132
           power_line_frequency (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature (int)    : min=2000 max=6500 step=1 default=4000 value=3255 flags=inactive
                      sharpness (int)    : min=0 max=255 step=1 default=128 value=128
         backlight_compensation (int)    : min=0 max=1 step=1 default=0 value=0
                  exposure_auto (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute (int)    : min=3 max=2047 step=1 default=250 value=333 flags=inactive
         exposure_auto_priority (bool)   : default=0 value=1
                   pan_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                  tilt_absolute (int)    : min=-36000 max=36000 step=3600 default=0 value=0
                 focus_absolute (int)    : min=0 max=250 step=5 default=0 value=0 flags=inactive
                     focus_auto (bool)   : default=1 value=1
                  zoom_absolute (int)    : min=100 max=500 step=1 default=100 value=100
                      led1_mode (menu)   : min=0 max=3 default=0 value=3
                 led1_frequency (int)    : min=0 max=255 step=1 default=0 value=0



$ v4l2-ctl -d /dev/video0 --list-formats
ioctl: VIDIOC_ENUM_FMT
    Index       : 0
    Type        : Video Capture
    Pixel Format: 'YUYV'
    Name        : YUYV 4:2:2

    Index       : 1
    Type        : Video Capture
    Pixel Format: 'H264' (compressed)
    Name        : H.264

    Index       : 2
    Type        : Video Capture
    Pixel Format: 'MJPG' (compressed)
    Name        : Motion-JPEG

How can I fix this?

Also, trying to get the video from guvcview I get the following error:

$ guvcview
GUVCVIEW: version 2.0.2
GUVCVIEW: couldn't open /home/mona/.config/guvcview2/video0 for read: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
V4L2_CORE: (UVCIOC_CTRL_MAP) Error: No such file or directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
libv4l2: error setting pixformat: Device or resource busy
V4L2_CORE: (VIDIOC_S_FORMAT) Unable to set format: Device or resource busy
GUCVIEW: could not set the defined stream format
GUCVIEW: trying first listed stream format
libv4l2: error setting pixformat: Device or resource busy
V4L2_CORE: (VIDIOC_S_FORMAT) Unable to set format: Device or resource busy
GUCVIEW: also could not set the first listed stream format
GUVCVIEW: Video capture failed
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
V4L2_CORE: (VIDIOC_S_PARM) error: Device or resource busy
V4L2_CORE: Unable to set 1/30 fps
GUVCVIEW: (status) saving video to /home/mona/my_video-1.mkv
ENCODER: add stream 0 to stream list
ENCODER: add stream 1 to stream list
ENCODER: (matroska) add seekhead entry 0 (max 10)
ENCODER: (matroska) add seekhead entry 1 (max 10)
AUDIO: (portaudio) Aborting audio stream
AUDIO: Closing audio stream...
ENCODER: (matroska) closing context
ENCODER: (matroska) closing cluster
ENCODER: (matroska)write seekhead
ENCODER: (matroska) end duration = 6922 (6922.000000) 
^CGUVCVIEW Caught signal 2
^CGUVCVIEW Caught signal 2

so far unplugged and plugged back the Webcam, used cheese and now I get video:

$ cheese &

But now, running the code I get this new error:

VIDEOIO ERROR: V4L: index 0 is not correct!
Error getting frame from device 0

Before this, the camera light was blue and now it has no light turned on.

so my Webcam is on device 014 and I use it for video capture in OpenCV:

$ sudo lsusb  | grep -i cam
Bus 003 Device 014: ID 046d:082d Logitech, Inc. HD Pro Webcam C920

Here is the code:

if USE_CAMERA:
    cap = cv.VideoCapture(args.device)

And I pass 014 as argument to it. I get this error:

VIDEOIO ERROR: V4L: index 14 is not correct!
Error getting frame from device 14

Solution 1:

First unplugged and plugged the Webcam USB and then used the following command to find which device number is the Webcam and used the appropriate port number:

$ ls /dev/video*
/dev/video1

Solution 2:

In my case it was OpenCV version issue. I downgraded its version from 4.0 to 3.4 and it worked for me. Since I installed my OpenCv with pip before, I downgraded it in the following way:

pip uninstall opencv-python
pip install opencv-python==3.4.0.12