14

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 numpy as np
background=cv2.imread('background.png')
cap = cv2.VideoCapture('car video.mp4')
cap.open('car video.mp4')
print cap.isOpened()
while 1:
    ret,img=cap.read()
    cv2.imshow('a',img)
    print img.shape


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

It shows this error

    cv2.imshow('a',img)
    error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:266: error:       (-215)        size.width>0 && size.height>0 in function cv::imshow

my opencv version 3.0.0, python 2.7, windows10 32 bit

Joy Mazumder
  • 870
  • 1
  • 8
  • 14

4 Answers4

10

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

Casey Jones
  • 155
  • 9
Mohammed Awney
  • 1,292
  • 16
  • 21
  • yes it's works thanks. At first i didn't copy the ffmpeg codec file – Joy Mazumder Feb 21 '17 at 02:46
  • 5
    could you please tell me how to add ffmpeg codec file? – Mohammed Awney Feb 21 '17 at 03:21
  • 2
    Copy all the file from **OpenCV\3rdparty\ffmpeg\** to **C:\Python27\** or to a directory that is in the PATH. Renaming the opencv_ffmpeg.dll file may also be necessary. For OpenCV version X.Y.Z opencv_ffmpeg.dll ==> opencv_ffmpegXYZ.dll For 64-bit OpenCV version X.Y.Z opencv_ffmpeg.dll ==> opencv_ffmpegXYZ_64.dll – Joy Mazumder Feb 21 '17 at 04:51
  • i am using opencv 3.0.0 and win 32 so rename the file just like this opencv_ffmpeg300.dll – Joy Mazumder Feb 21 '17 at 04:56
6

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.

user7199416
  • 131
  • 1
  • 6
5

try

pip install opencv-contrib-python

It worked for me

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
L.xp
  • 89
  • 1
  • 2
2

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()