11
import cv2
import numpy as np

cap = cv2.VideoCapture('traffic.avi')

retval, frame = cap.read()

print retval

================ RESTART: J:\Python For DIP\traffic_video.py ================
False
>>> 

The Value of retval is always False, which means the video is not read by the command. It must be True to read frames. I don't know what to do. However when I use my default webcam it turns to be True. I tried many videos and the same problem appears. Note: I have installed the ffmpeg correctly.

Note: This is not the full code, in this step I am only validating cap.read() either True or False

Tes3awy
  • 2,166
  • 5
  • 29
  • 51

2 Answers2

14

This method is guaranteed 100%

first of all check your version of OpenCV, say for instance 2.4.11. you can check it by typing the following commands in your Python Shell:

>>> from cv2 import __version__
>>> __version__
'2.4.11'
>>> 

Then go to C:\opencv\build\x86\vc12\bin and copy opencv_ffmpeg2411.dll. Finally go to root directory of Python ex: C:\Python27 and paste opencv_ffmpeg2411.dll in it

check the name of the file opencv_ffmpeg2411.dll, whether the version of opencv is written or not, if not do the following opencv_ffmpeg(version of your opencv without dots).dll

After that create a new Python file and copy this code and paste it loading your own video

import numpy as np
import cv2

# Capture video from file
cap = cv2.VideoCapture('your video')

while True:

    ret, frame = cap.read()

    if ret == True:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('frame',gray)


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

    else:
        break

cap.release()
cv2.destroyAllWindows()

you will have an output video for example like this: Result

Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • This worked for me when everything else failed ! (I followed several advises on the net and installed ffmpeg, gstreamer and visual studio libraries, but none of them worked.) Thanks for finally solving my problem. – Raja Sep 08 '17 at 18:55
  • 9
    What about linux? Is there a similar attempt? None of the other advises i found have worked – WuerfelDev Oct 05 '17 at 22:17
  • Thank you. and do you have time to answer what WuerfelDev asked? – Tanishq Jaiswal Apr 01 '18 at 13:52
  • @WuerfelDev Unfortunately, I barely use Linux. So I have no idea. Sorry that I couldn't help. – Tes3awy Apr 02 '18 at 10:26
  • 1
    I tried a lot, but nothing worked for me. In the end I completely wiped Ubuntu 17.04 and installed Ubuntu 17.10. After installing on the new system it is working fine – WuerfelDev Apr 04 '18 at 16:52
  • 1
    It still didn't fix the issue using Python Notebooks. But I switched to PyCharm instead. – Ilan Aizelman WS May 27 '18 at 11:59
1

Finding the root directory of Python can be a little tricky. I am using an Enthought distribution and, at first, pasted the opencv_ffmpeg file into the wrong Python directory.

WRONG:

C:\Users\USERNAME\AppData\Local\Programs\Python\Python35-32

RIGHT:

C:\Users\USERNAME\AppData\Local\Enthought\Canopy\User

Long story short, make sure you find the right Python directory.

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
P__2
  • 66
  • 5