Got an error when tried to capture from Usb Camera

asked 2018-06-19 20:49:13 -0500

nvnnghia gravatar image

updated 2018-06-21 20:01:19 -0500

My set up:

Python 3.6
Opencv 3.4.1

My test code:

cap = cv2.VideoCapture(0)
while True:
    ret, image =
    if not ret:
    cv2.imshow("test", image)

When I run this code, It can only capture the first frame from the camera, but the frame is black. It can not capture from the second frame. This code worked perfectly if I use a video file instead.

print(cv2.getBuildInformation()) give this result:

    General configuration for OpenCV 3.4.1 =====================================
  Version control:               3.4.1

    Timestamp:                   2018-05-19T15:09:33Z
    Host:                        Windows 6.3.9600 AMD64
    CMake:                       3.11.1
    CMake generator:             Visual Studio 14 2015 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
    MSVC:                        1900

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (3 files):          + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (9 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

    Built as dynamic libs?:      NO
    C++11:                       YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24215.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi      /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP2   /MT /O2 /Ob2 /DNDEBUG 
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi      /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP2   /MTd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi        /MP2    /MT /O2 /Ob2 /DNDEBUG 
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi        /MP2  /MTd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
    Linker flags (Debug):        /machine:x64  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 vfw32
    3rdparty dependencies:       ittnotify libprotobuf zlib libjpeg libwebp libpng libtiff libjasper IlmImf ippiw ippicv

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python3 python_bindings_generator shape stitching superres video videoio videostab
    Disabled:                    js world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 ts viz
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.34)
    TIFF:                        build (ver 42 - 4.0.9)
    JPEG 2000 ...
edit retag flag offensive close merge delete



how did you install this ? (opencv does not have version numbers like that)

try a : print(cv2.getBuildInformation()) there is a "VideoIO" section, please report back.

are you able to use your webcam with other programs (like vlc, mediaplayer, etc) ?

berak gravatar imageberak ( 2018-06-20 01:03:10 -0500 )edit

Hi berak, I edited the question, I updated the result of print(cv2.getBuildInformation()) above. I tried the above code many times and It rarely works. I tried it for about 10 times and it only works 1 time. I don't know what is the problem I use this site to test my webcam: and it worked correctly

nvnnghia gravatar imagenvnnghia ( 2018-06-20 01:16:02 -0500 )edit

change a webcam to see if the problem still happen.

jsxyhelu gravatar imagejsxyhelu ( 2018-06-22 07:13:56 -0500 )edit

But, Why sometime it works, sometime it not works. I tried 3 webcams and it gave the same result.

nvnnghia gravatar imagenvnnghia ( 2018-06-22 07:55:45 -0500 )edit

strange,how many cameras did you use at a time?

jsxyhelu gravatar imagejsxyhelu ( 2018-06-23 02:54:26 -0500 )edit

I used 1 or 2 cameras at a time, But I changed the ID of camera in code. Now, After I restarted my computer, 1 camera works, and the others are not.

nvnnghia gravatar imagenvnnghia ( 2018-06-24 20:11:24 -0500 )edit