简体   繁体   English

opencv 视频捕获和读取不会检索所有帧。 返回不正确的帧数

[英]opencv videocapture and read doesnt retrieve all frames. Returns incorrect number of frames

I have a mp4 video which is 29 minutes long with 1536x2304 resolution.我有一个 29 分钟长的 mp4 视频,分辨率为 1536x2304。 What I am trying do is to create a dataloader which ingest mp4 files and feeds to a CNN, but to do so I need to use opencv to retrieve the frames.我正在尝试做的是创建一个数据加载器,它摄取 mp4 文件并提供给 CNN,但为此我需要使用 opencv 来检索帧。 When I use cap = cv.VideoCapture("mp4 file") and ret, frame = cap.read(), I only receive about 40 frames.当我使用 cap = cv.VideoCapture("mp4 file") 和 ret, frame = cap.read() 时,我只收到大约 40 帧。

Since the Video is 29 min long, and it is 18fps, the number of frames I expect is around 31320 frames, but I only get 40. Really confused, any help is appreciated!由于视频长度为 29 分钟,并且为 18fps,因此我期望的帧数约为 31320 帧,但我只得到 40 帧。真的很困惑,感谢任何帮助!

As you specified that you used the line ret, frame = cap.read() to count the number of frames, I would assume that you are using a while loop with a condition of when to break .当您指定使用行ret, frame = cap.read()来计算帧数时,我会假设您正在使用带有 when to break条件的while循环。

Have you considered that the while loop may have ended due to an issue unrelated to the number of frames in the VideoCapture object?您是否考虑过由于与VideoCapture object 中的帧数无关的问题, while循环可能已经结束? Or you may have called the cap.read() method multiple times per increment to the variable you used to count the frames with.或者,您可能已经多次调用cap.read()方法,每次递增到用于计算帧数的变量。

The proper way to get the number of frames in a video using OpenCV would be:使用 OpenCV 获取视频中帧数的正确方法是:

import cv2 as cv

cap = cv.VideoCapture("file.mp4")
count = cap.get(cv2.CAP_PROP_FRAME_COUNT)

print(count)

(Note: If you get the output 0 , then the filename specified likely couldn't be found and would need to be corrected.) (注意:如果您得到 output 0 ,则可能找不到指定的文件名,需要更正。)

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

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