在 python 中从 VideoCapture opencv 获取特定帧
Getting specific frames from VideoCapture opencv in python
我有以下代码,它通过在 python 中使用 opencv 中的 VideoCapture 库从视频中连续获取所有帧:
import cv2
def frame_capture:
cap = cv2.VideoCapture("video.mp4")
while not cap.isOpened():
cap = cv2.VideoCapture("video.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
但我想抓取视频中特定时间戳中的特定帧。
我怎样才能做到这一点?
您可以使用 VideoCapture 的 set() 函数。
您可以计算总帧数:
cap = cv2.VideoCapture("video.mp4")
total_frames = cap.get(7)
这里的 7 是 prop-Id。你可以在这里找到更多 http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html
之后可以设置帧数,假设我要提取第100帧
cap.set(1, 100)
ret, frame = cap.read()
cv2.imwrite("path_where_to_save_image", frame)
这是我的第一个 post 所以如果我不完全遵守协议,请不要攻击我。我只是想回复 June Wang 以防她不知道如何设置要提取的帧数,或者万一其他人偶然发现这个问题的线程:
解决方案是很好的 for 循环:
vid = cv2.VideoCapture(video_path)
for i in range(start_frame, how_many_frames_you_want):
vid.set(1, i)
ret, still = vid.read()
cv2.imwrite(f'{video_path}_frame{i}.jpg', still)
我有以下代码,它通过在 python 中使用 opencv 中的 VideoCapture 库从视频中连续获取所有帧:
import cv2
def frame_capture:
cap = cv2.VideoCapture("video.mp4")
while not cap.isOpened():
cap = cv2.VideoCapture("video.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
但我想抓取视频中特定时间戳中的特定帧。
我怎样才能做到这一点?
您可以使用 VideoCapture 的 set() 函数。
您可以计算总帧数:
cap = cv2.VideoCapture("video.mp4")
total_frames = cap.get(7)
这里的 7 是 prop-Id。你可以在这里找到更多 http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html
之后可以设置帧数,假设我要提取第100帧
cap.set(1, 100)
ret, frame = cap.read()
cv2.imwrite("path_where_to_save_image", frame)
这是我的第一个 post 所以如果我不完全遵守协议,请不要攻击我。我只是想回复 June Wang 以防她不知道如何设置要提取的帧数,或者万一其他人偶然发现这个问题的线程:
解决方案是很好的 for 循环:
vid = cv2.VideoCapture(video_path)
for i in range(start_frame, how_many_frames_you_want):
vid.set(1, i)
ret, still = vid.read()
cv2.imwrite(f'{video_path}_frame{i}.jpg', still)