简体   繁体   English

有没有办法在 opencv-python 中显示最后一帧?

[英]Is there a way to show last frame in opencv-python?

I need to show the last frame of a video.我需要显示视频的最后一帧。 I can calculate last frame number manually and run below code.我可以手动计算最后一帧编号并在代码下运行。

last_frame_num = duration_in_seconds * video_fps #manual entry

vs = cv2.VideoCapture('test.mp4')
vs.set(cv2.CAP_PROP_POS_FRAMES, last_frame_num)

while True:
    ret, frame = vs.read()
    if ret:
        cv2.imshow('last_frame', frame)
    if cv2.waitKey(0) == 27:
        break
vs.release()
cv2.destroyAllWindows()

The first line of code is manual entry.第一行代码是手动输入。

Is there a way I can get the last frame number directly for any video?有没有办法可以直接获取任何视频的最后一帧编号?

Since you want the last frame of a video, you can use VideoCapture::get with the property CAP_PROP_FRAME_COUNT .由于您想要视频的最后一帧,您可以使用VideoCapture::get和属性CAP_PROP_FRAME_COUNT This will give you the amount of frames in the video.这将为您提供视频中的帧数。 You can try to assign it to your variable like this:您可以尝试将其分配给您的变量,如下所示:

last_frame_num = vs.get(cv2.CAP_PROP_FRAME_COUNT)

In my opinion it should have a -1 as well, since CAP_PROP_POS_FRAMES takes a "0-based index of the frame to be decoded/captured next".在我看来,它也应该有一个-1 ,因为CAP_PROP_POS_FRAMES需要“接下来要解码/捕获的帧的基于 0 的索引”。 However, good to see that it worked for you even without the -1.但是,很高兴看到即使没有 -1 它也对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM