skip to Main Content

My goal is to be able to capture an image using a Logitech hd C270 webcam on Ubuntu 20.04 using OpenCV but I’m encountering a problem that I don’t understand.

Here is my program:

import cv2

def capture_image(device_path, ouput) : 
    cap = cv2.VideoCapture(device_path)

    if not cap.isOpened():
        print("Error open the device")
        return 
    
    ret, frame = cap.read()
    print(ret)
    print(frame)
    if not ret:
        print("Error capture image")
        return
    
    cv2.imwrite(ouput, frame)

    cap.release

    print("imahe capturée")

if __name__ == "__main__":
    device_path = "/dev/video0"
    ouput = "image.jpg"   
    capture_image(device_path, ouput) `

When I launch my program, ret is False and frame is None.

This is my OS:

Linux nahel-VirtualBox 5.15.0-105-generic 
#115~20.04.1-Ubuntu SMP Mon Apr 15 17:33:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
nahel@nahel-VirtualBox:/$ v4l2-ctl --list-device
C270 HD WEBCAM (usb-0000:00:06.0-2):
/dev/video0
/dev/video1
nahel@nahel-VirtualBox:/$ v4l2-ctl -V
Format Video Capture:
Width/Height      : 640/480
Pixel Format      : 'YUYV' (YUYV 4:2:2)
Field             : None
Bytes per Line    : 1280
Size Image        : 614400
Colorspace        : sRGB
Transfer Function : Rec. 709
YCbCr/HSV Encoding: ITU-R 601
Quantization      : Default (maps to Limited Range)
Flags             :
nahel@nahel-VirtualBox:/$ v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

[0]: 'YUYV' (YUYV 4:2:2)
[1]: 'MJPG' (Motion-JPEG, compressed)

I tried my code on Windows and it worked.

2

Answers


  1. Chosen as BEST ANSWER

    and here is the information obtained with V4L2

    nahel@nahel-VirtualBox:/$ v4l2-compliance
    v4l2-compliance SHA: not available, 64 bits
    
    Compliance test for uvcvideo device /dev/video0:
    
    Driver Info:
    Driver name      : uvcvideo
    Card type        : C270 HD WEBCAM
    Bus info         : usb-0000:00:06.0-2
    Driver version   : 5.15.148
    Capabilities     : 0x84a00001
    Video Capture
    Metadata Capture
    Streaming
    Extended Pix Format
    Device Capabilities
    Device Caps      : 0x04200001
    Video Capture
    Streaming
    Extended Pix Format
    
    Required ioctls:
    test VIDIOC_QUERYCAP: OK
    
    Allow for multiple opens:
    test second /dev/video0 open: OK
    test VIDIOC_QUERYCAP: OK
    test VIDIOC_G/S_PRIORITY: OK
    test for unlimited opens: OK
    
    Debug ioctls:
    test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported)
    test VIDIOC_LOG_STATUS: OK (Not Supported)
    
    Input ioctls:
    test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported)
    test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
    test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
    test VIDIOC_ENUMAUDIO: OK (Not Supported)
    test VIDIOC_G/S/ENUMINPUT: OK
    test VIDIOC_G/S_AUDIO: OK (Not Supported)
    Inputs: 1 Audio Inputs: 0 Tuners: 0
    
    Output ioctls:
    test VIDIOC_G/S_MODULATOR: OK (Not Supported)
    test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
    test VIDIOC_ENUMAUDOUT: OK (Not Supported)
    test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
    test VIDIOC_G/S_AUDOUT: OK (Not Supported)
    Outputs: 0 Audio Outputs: 0 Modulators: 0
    
    Input/Output configuration ioctls:
    test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
    test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
    test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
    test VIDIOC_G/S_EDID: OK (Not Supported)
    
    Control ioctls (Input 0):
    test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
    test VIDIOC_QUERYCTRL: OK
    fail: v4l2-test-controls.cpp(445): s_ctrl returned an error (13)
    test VIDIOC_G/S_CTRL: FAIL
    fail: v4l2-test-controls.cpp(695): s_ext_ctrls returned an error (13)
    test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL
    test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK
    test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
    Standard Controls: 14 Private Controls: 0
    
    Format ioctls (Input 0):
    test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
    test VIDIOC_G/S_PARM: OK
    test VIDIOC_G_FBUF: OK (Not Supported)
    test VIDIOC_G_FMT: OK
    test VIDIOC_TRY_FMT: OK
    warn: v4l2-test-formats.cpp(976): Could not set fmt2
    test VIDIOC_S_FMT: OK
    test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
    test Cropping: OK (Not Supported)
    test Composing: OK (Not Supported)
    test Scaling: OK (Not Supported)
    
    Codec ioctls (Input 0):
    test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported)
    test VIDIOC_G_ENC_INDEX: OK (Not Supported)
    test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)
    
    Buffer ioctls (Input 0):
    test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK
    test VIDIOC_EXPBUF: OK
    test Requests: OK (Not Supported)
    
    Total for uvcvideo device /dev/video0: 44, Succeeded: 42, Failed: 2, Warnings: 1
    

  2. Yes, I work with VirtualBox and Visual Studio code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search