简体   繁体   English

OpenCV读取视频文件在Python很慢

[英]Video file reading by OpenCV is very slow in Python

I'm trying to use OpenCV from Python for some video processing and it works extremely slow for me.我正在尝试使用 Python 中的 OpenCV 进行一些视频处理,但它对我来说非常慢。 For example a simple reading and showing of all frames works at about 1 fps:例如,以大约 1 fps 的速度简单读取和显示所有帧:

import cv2
cap = cv2.VideoCapture("out1.avi")
cv2.namedWindow("input")
while(True):
    f, img = cap.read()
    cv2.imshow("input", img)
    cv2.waitKey(1)

The same video file in C++ is rendered without any problems at about 30 fps. C++ 中的相同视频文件以大约 30 fps 的速度呈现而没有任何问题。 Are there any ideas why Python version is so slow?有什么想法为什么 Python 版本这么慢?

And there is another interesting thing about Python version: it doesn't show.wmv files which C++ version can process (for my Python can open raw video only).关于 Python 版本还有另一个有趣的事情:它不显示 C++ 版本可以处理的.wmv 文件(因为我的 Python 只能打开原始视频)。

I use OpenCV 2.3.1 and Python 2.7我使用 OpenCV 2.3.1 和 Python 2.7

Thanks for help!感谢帮助!

The code runs normally (fast) in my machine (opencv 2.3.0 & python 2.6.4 on win7-64, playing uncompressed avi file). 代码在我的机器上正常(快速)运行(win7-64上的opencv 2.3.0和python 2.6.4,播放未压缩的avi文件)。

have you tried the performance using older python interface (cv instead of cv2)? 你有没有尝试使用旧的python接口(cv而不是cv2)?

regarding .wmv video playback, it's kinda having problem with python interface (or specifically the ffmpeg). 关于.wmv视频播放,它有点python接口(或特别是ffmpeg)的问题。 it can't play other than uncompressed .avi files. 除了未压缩的.avi文件之外,它无法播放。

The article "Lightning Fast Video Reading in Python" points out that OpenCV wasn't optimized for fast video reading, and compares other libraries.文章“Lightning Fast Video Reading in Python”指出 OpenCV 没有针对快速视频阅读进行优化,并与其他库进行了比较。

The benchmark code is here .基准代码在这里 You can run it on your video.你可以在你的视频上运行它。 Results may depend on the video resolution, codec, your hardware, etc.结果可能取决于视频分辨率、编解码器、您的硬件等。

The Decord library seems to win in most cases.在大多数情况下, Decord库似乎胜出。

尝试将cv2.waitKey(1)中的“1”更改为更高的值,例如cv2.waitKey(30)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM