I need to access frames from video by the frame index. So far I used code like this:
video = cv2.VideoCapture(video_path)
status, frame = video.read()
The code reads the first frame. If I use the code repeatedly I will get next frames. But how I can access directly any frame by its index?
In other words, if I want second frame, how can I access directly the second frame without calling read()
two times?
Use VideoCapture::set()
with CAP_PROP_POS_FRAMES
property id to set the position of the frame to be read.
myFrameNumber = 50
cap = cv2.VideoCapture("video.mp4")
# get total number of frames
totalFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
# check for valid frame number
if myFrameNumber >= 0 & myFrameNumber <= totalFrames:
# set frame position
cap.set(cv2.CAP_PROP_POS_FRAMES,myFrameNumber)
while True:
ret, frame = cap.read()
cv2.imshow("Video", frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
import os
folder= 'frames'
os.mkdir(folder)
import cv2
vidcap = cv2.VideoCapture('four.mp4')
def getFrame(sec):
vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = vidcap.read()
if hasFrames:
cv2.imwrite("image"+str(count)+".jpg", image) # save frame as JPG file
return hasFrames
sec = 0
frameRate = 0.5 #//it will capture image in each 0.5 second
count=1
success = getFrame(sec)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = getFrame(sec)