[英]Select specific frame fast OpenCV Python
I am trying to rapidly select and process different frames from a video using OpenCV Python. 我正在尝试使用OpenCV Python快速选择和处理视频中的不同帧。 To select a frame, I have used the 'CAP_PROP_POS_FRAMES' (or cap.set(2, frame_no)).
要选择一个框架,我使用了“ CAP_PROP_POS_FRAMES”(或cap.set(2,frame_no))。 However when using this I noticed a delay of about 200 ms to decode the selected frame.
但是,使用此方法时,我注意到解码所选帧的延迟约为200毫秒。 My script will be jumping in between frames a lot (not necessarily chronological) which means this will cause a big delay between each iteration.
我的脚本会在帧之间跳很多(不一定是时间顺序),这意味着这将导致每次迭代之间的较大延迟。 I suspected OpenCV is buffering the upcoming frames after I set the frame number.
我怀疑在设置帧号后,OpenCV正在缓冲即将到来的帧。 Therefore I tried pre-decoding of the video by basically putting the entire video as a list so it can be accessed from RAM.
因此,我基本上通过将整个视频作为列表来尝试对视频进行预解码,以便可以从RAM对其进行访问。 This worked fantastic except bigger videos completely eat up my memory.
除了较大的视频完全耗尽了我的记忆之外,这非常棒。 I was hoping someone knows a way to either set the frame number without this 200ms delay or to decode the video without using all of my memory space.
我希望有人知道一种方法来设置帧号而不延迟200ms,或者解码视频而不使用我所有的存储空间。 Any suggestions are also welcome!
任何建议也欢迎!
I don't know how to avoid that 200ms delay, but I have a suggestion on how you could decode the video first even if its size is greater than your RAM. 我不知道如何避免200ms的延迟,但是我对如何将视频解码(即使其大小大于您的RAM)提出了建议。 You could use numpy's
memmap
: 您可以使用numpy的
memmap
:
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.memmap.html . https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.memmap.html 。
In practice you could have a function that initializes this memory-mapped matrix and then iterate over each frame of the video using VideoCapture
and then store each frame in this matrix. 实际上,您可以使用一个函数来初始化此内存映射矩阵,然后使用
VideoCapture
遍历视频的每个帧,然后将每个帧存储在此矩阵中。 After that you will be able to jump between each frame just by accessing this memory-mapped matrix. 之后,您只需访问此内存映射矩阵就可以在每个帧之间跳转。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.