How to process images of a video, frame by frame, in video streaming using OpenCV and Python
After reading the documentation of VideoCapture
. I figured out that you can tell VideoCapture
, which frame to process next time we call VideoCapture.read()
(or VideoCapture.grab()
).
The problem is that when you want to read()
a frame which is not ready, the VideoCapture
object stuck on that frame and never proceed. So you have to force it to start again from the previous frame.
Here is the code
import cv2cap = cv2.VideoCapture("./out.mp4")while not cap.isOpened(): cap = cv2.VideoCapture("./out.mp4") cv2.waitKey(1000) print "Wait for the header"pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)while True: flag, frame = cap.read() if flag: # The frame is ready and already captured cv2.imshow('video', frame) pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) print str(pos_frame)+" frames" else: # The next frame is not ready, so we try to read it again cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1) print "frame is not ready" # It is better to wait for a while for the next frame to be ready cv2.waitKey(1000) if cv2.waitKey(10) == 27: break if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): # If the number of captured frames is equal to the total number of frames, # we stop break
Use this:
import cv2cap = cv2.VideoCapture('path to video file')count = 0while cap.isOpened(): ret,frame = cap.read() cv2.imshow('window-name', frame) cv2.imwrite("frame%d.jpg" % count, frame) count = count + 1 if cv2.waitKey(10) & 0xFF == ord('q'): breakcap.release()cv2.destroyAllWindows() # destroy all opened windows
According to the latest updates for OpenCV 3.0 and higher, you need to change the Property Identifiers as follows in the code by Mehran:
cv2.cv.CV_CAP_PROP_POS_FRAMES
to
cv2.CAP_PROP_POS_FRAMES
and same applies to cv2.CAP_PROP_POS_FRAME_COUNT
.
Hope it helps.