[英]OpenCV don't get last frame of the video
我尝试从视频中获取第一帧、中间帧和最后一帧,但它只能获取第一帧和中间帧。
这是我的代码:
import cv2
import math
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
# Get number of frame of the video
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print(length)
# Define a tuple of frame need to get
frame = (0, math.ceil(length/2), length - 1)
# Get the frame
for f in frame:
vidcap.set(1, f)
_, image = vidcap.read()
cv2.imwrite("/home/data/output/frame%d.jpg" % f, image)
它确实保存了最后一帧图像,但没有任何数据。
我的代码有什么问题吗?
更新:
根据这篇文章,似乎cv2.CAP_PROP_FRAME_COUNT
可以计算错误的帧数。
那么有没有更好的方法来获取帧数而不读取整个帧?
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
count = 1
while 1:
ret , frame = vidcap.read()
count += 1
if ret == False:
break
print(count)
可能会出现打字错误,但您可以理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.