Can not Read or Play a Video in OpenCV+Python using VideoCapture

泄露秘密 提交于 2019-11-28 11:16:30

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

P__2

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!