Ask Your Question
5

cv2.VideoCapture: Cannot read from file

asked Aug 31 '12

slychief gravatar image

Hi!

I try to process videos using the Python interface. I downloaded OpenCV 2.4.2 and copied the cv2.py file to the Python27\Lib\site-packages folder on Windows 7.

Using the following code, I'm not able to read video files. capture.isOpened() always returns false. Reading images from Webcam using cv2.VideoCapture(0) works fine, but not from files.

Did I miss to copy some importtant files to the Python directory?

import cv2

if __name__ == '__main__':


    capture = cv2.VideoCapture("test.mpg")

    if capture.isOpened():
            print("Device Opened\n")
    else:
            print("Failed to open Device\n")

    (ret, imageMat) = capture.read()

Cheers, Sly

Preview: (hide)

Comments

How you run your app? Try absolute path for test.

Victor1234 gravatar imageVictor1234 (Sep 2 '12)edit

is your test.mpg in the same folder of your code?

Mahdi gravatar imageMahdi (Nov 26 '12)edit
1

I'm having the same issue: VideoCapture works well with webcam, but no success with files - absolute path and otherwise.

I tried the solutions from here: http://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows, but it still doesn't work. I added the folder to the PATH, and copied the dlls around.

errollw gravatar imageerrollw (Jan 10 '13)edit

Did you ever find a solution to this?

loughnane gravatar imageloughnane (Aug 13 '13)edit

There might be a problem with the ffmpeg dll that is being used. Which build are you following? x86 or x86_64?

prakharmohan gravatar imageprakharmohan (Feb 18 '14)edit

DId anyone get the solution of this problem? I am facing the same problem for so many days and still couldn't fix it

AnjaliChanglani gravatar imageAnjaliChanglani (Feb 18 '15)edit

Did anyone solved it? I am having the same problem. After added path and copied dll still not working. I am on win 8; 64x.

Vashaman gravatar imageVashaman (Feb 23 '17)edit

This post is too old you should ask a new question

LBerger gravatar imageLBerger (Feb 23 '17)edit

2 answers

Sort by » oldest newest most voted
1

answered Jul 17 '15

wunjo gravatar image

Try including the full file path in cv2.VideoCapture and be sure to change the "\" to "/". OpenCV doesn't like the forward slashes for some reason. Example: cv2.VideoCapture('C:/Project/Videos/test.mpg')Also, see my answer here for more clarification on proper codecs to use: (http://answers.opencv.org/question/66...). Hope this helps!

Preview: (hide)

Comments

Changing "\" to "/" fixed the problem for me!

Fredericco gravatar imageFredericco (Apr 24 '19)edit
-1

answered Oct 24 '18

I was getting the same error while using opencv in anaconda3 virtual environment. I checked the buildinformation (cv2.getBuildInformation()) for current opencv version and ffmpeg wasn't marked "yes". To resolve this

  1. I uninstalled opencv from my conda environment ( conda uninstall opencv)

  2. Installed latest ffmpeg using conda-forge channel

  3. Then installed opencv again using conda-forge channel ( conda install -c conda-forge opencv) . Don't install using menpo channel

Doing this resolved the issue for me.

Preview: (hide)

Question Tools

3 followers

Stats

Asked: Aug 31 '12

Seen: 40,976 times

Last updated: Jul 17 '15