问题描述
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
>>>
retval的值始终为False,这意味着该命令无法读取视频.读取帧必须为True.我不知道该怎么办.但是,当我使用默认的网络摄像头时,它会变为True.我尝试了许多视频,并出现了相同的问题.注意:我已经正确安装了ffmpeg.
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.
注意:这不是完整的代码,在这一步中,我仅验证cap.read()是True还是False
Note: This is not the full code, in this step I am only validating cap.read() either True or False
推荐答案
此方法保证100%
首先检查您的OpenCV版本,例如2.4.11.您可以通过在Python Shell中键入以下命令来进行检查:
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'
>>>
然后转到C:\opencv\build\x86\vc12\bin
并复制opencv_ffmpeg2411.dll
.最后,转到Python ex的根目录:C:\Python27
并将opencv_ffmpeg2411.dll
粘贴到其中
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
之后,创建一个新的Python文件并复制此代码并将其粘贴,以加载您自己的视频
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
这篇关于无法使用VideoCapture在OpenCV + Python中读取或播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!