[英]Unable to read MP4 and avi files in OpenCV Python
我只想使用 OpenCV 读取和显示 MP4 视频,我为此编写了以下基本代码:
import cv2
input_video_path = './Input Video/Input_video1.mp4'
cap = cv2.VideoCapture(input_video_path)
while(cap.isOpened()):
ret, frame = cap.read()
print(frame, ret)
cv2.imshow("frame", frame)
cap.release()
cv2.destroyAllWindows()
当我运行它时,它会读取第一几帧,然后所有其他帧都是None
:
[[[ 7 14 27]
[ 7 14 27]
[ 7 14 27]
...
...
[ 60 57 64]
[ 70 62 64]
[ 72 64 66]]] True
None False
Traceback (most recent call last):
File "D:/Project/ML IP and Coding/Cynapto_Task/exploring_face_detection_methods.py", line 10, in <module>
cv2.imshow("frame", frame)
cv2.error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
我还打印了确认相同行为的ret
变量。 我尝试了 2-3 个不同的视频,甚至使用.avi
格式,但得到了相同的结果。
我在网上搜索了解决方案,但只能找到有关编解码器和在我的虚拟环境中安装编解码器的信息。 我对视频文件格式和编解码器知之甚少。
有人可以帮我弄这个吗?
我在用:
Python 3.7,操作系统:Windows,环境:康达,OpenCV v4.5.0
如果您收到ret
为False
,则表示视频到达结束帧。 如果视频未完成但您收到False
,则它可能已损坏。
试试这个代码:
import cv2
input_video_path = './Input Video/Input_video1.mp4'
cap = cv2.VideoCapture(input_video_path)
while(cap.isOpened()):
ret, frame = cap.read()
print(frame, ret)
if ret:
cv2.imshow("frame", frame)
cv2.waitKey(1)
else:
break
cap.release()
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.