python cv2.Videocapture() does not work, cap.isOpened() returns false

前端 未结 4 1486

cv2.Videocapture() works fine while using webcam but while trying to read from hard drive it shows error cap.isOpened() returns false

import cv2
import nump         


        
相关标签:
4条回答
  • 2021-01-02 08:41

    You need the ffmpeg codec to be able to read the video.

    0 讨论(0)
  • 2021-01-02 08:50

    I am not sure that you are writing your file name correctly. I've never seen a file directory like 'car video.mp4'. When you are using the zero based index your webcam and cv2.VideoCapture works fine; however VideoCapture cannot read a file like 'car(space)video.mp4' A working code is something like this;

    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture('video.mp4')
    
    while(cap.isOpened()):
    
        ret, frame = cap.read()
    
        if ret==True:
    
            cv2.imshow('frame',frame)
    
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break
    
    # Release everything if job is finished
    cap.release()
    cv2.destroyAllWindows()
    
    0 讨论(0)
  • 2021-01-02 08:55

    try

    pip install opencv-contrib-python
    

    It worked for me

    0 讨论(0)
  • 2021-01-02 08:55

    I was getting the same error while using opencv in anaconda3 virtual environment. I checked the buildinformation for current opencv version and ffmpeg was marked "no".

    To resolve this

    1. I uninstalled opencv from my conda environment (conda uninstall opencv)
    2. Installed latest ffmpeg using conda-forge channel (conda install -c conda-forge ffmpeg)

      Name Version Build Channel

      ffmpeg 4.0.2 ha6a6e2b_0 conda-forge

    3. Then installed opencv again using conda-forge channel (conda install -c conda-forge opencv)

      Name Version Build Channel

    opencv 3.4.1 py36_blas_openblash829a850_201 [blas_openblas] conda-forge

    Restart the python console after doing this and import cv2.

    0 讨论(0)
提交回复
热议问题