Reputation: 1533
I'm using OpenCV with Python to process a video stream. I'd like to implement my own algorithm, so I need to iterate over each frame.
What I have so far works, but way too slow to be real-time. I know that Python isn't the most efficient programming language, but I believe it can do much better than this, considering, that the built in image transformation functions are very fast. Numpy may be the way to go, but I'm not yet familiar with it.
import cv, numpy
vidFile = cv.CaptureFromFile( 'sample.avi' )
nFrames = int( cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FRAME_COUNT ) )
for f in xrange( nFrames ):
frameImg = cv.QueryFrame( vidFile )
frameMat=cv.GetMat(frameImg)
print "mat ", mat[3,1]
for x in xrange(frameMat.cols):
for y in xrange(frameMat.rows):
# just an example, multiply all 3 components by 0.5
frameMat[y, x] = tuple(c*0.5 for c in frameMat[y, x])
cv.ShowImage( "My Video Window", frameMat )
if cv.WaitKey( waitPerFrameInMillisec ) == 27:
break
How can I speed up the process? Thanks, b_m
Upvotes: 3
Views: 5044
Reputation: 35269
OpenCV has pretty good python documentation here. Basically you should always try to do operations on video frames using these builtin opencv functions, or numpy. For frame processing take a look at operations on arrays, using this you can replace your entire pixel by pixel processing loop, which is absurdly slow:
frameMat=cv.GetMat(frameImg)
print "mat ", mat[3,1]
for x in xrange(frameMat.cols):
for y in xrange(frameMat.rows):
# just an example, multiply all 3 components by 0.5
frameMat[y, x] = tuple(c*0.5 for c in frameMat[y, x])
cv.ShowImage( "My Video Window", frameMat )
with:
cv.ConvertScale(frameImg, frameImg, scale=0.5)
cv.ShowImage( "My Video Window", frameImg )
and easily play it in real time, there are loads of cool functions allowing you to merge videos etc.
Upvotes: 4
Reputation: 44128
Python for loops are just too slow. If you can express your algorithm using the built-in functions (or numpy or another extension module), do that. For example, your multiply-by-constant example is easy to implement with ConvertScale. If the algorithm is more complicated, you'll have to implement it at C level. Cython is one popular way to make that easier.
Upvotes: 2