Akson
Akson

Reputation: 61

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. For example a simple reading and showing of all frames works at about 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. Are there any ideas why Python version is so slow?

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).

I use OpenCV 2.3.1 and Python 2.7

Thanks for help!

Upvotes: 6

Views: 10680

Answers (3)

root
root

Reputation: 2448

The article "Lightning Fast Video Reading in Python" points out that OpenCV wasn't optimized for fast video reading, and compares other libraries.

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.

Upvotes: 0

Chamuka
Chamuka

Reputation: 11

Try changing the "1" in cv2.waitKey(1) into a higher value say cv2.waitKey(30)

Upvotes: 0

Peb
Peb

Reputation: 171

The code runs normally (fast) in my machine (opencv 2.3.0 & python 2.6.4 on win7-64, playing uncompressed avi file).

have you tried the performance using older python interface (cv instead of cv2)?

regarding .wmv video playback, it's kinda having problem with python interface (or specifically the ffmpeg). it can't play other than uncompressed .avi files.

Upvotes: 2

Related Questions