简体   繁体   English

为什么我的笔记本电脑在使用Python列表时会卡住?

[英]Why my laptop gets stuck when working with Python list?

I have a video file and all I want for now is put all the video's frames into a Python list. 我有一个视频文件,我现在想要的是将所有视频的帧放入Python列表中。 I am using Python's OpenCV library to do it. 我正在使用Python的OpenCV库来完成它。 But my laptop could never do it. 但我的笔记本电脑永远不会这样做。 it just gets stuck and I have to cut the power to restart it. 它只是卡住了我必须切断电源重新启动它。 my guess is that python list is unable to handle all the frames due to memory deficiency. 我的猜测是由于内存不足,python列表无法处理所有帧。 Here is the code and i believe it is the right way to do what I want(syntax). 这是代码,我相信它是做我想要的正确方法(语法)。 now I need why the laptop is getting stuck and any solution other than using list. 现在我需要为什么笔记本电脑卡住以及使用列表以外的任何解决方案。

import cv2
video = cv2.VideoCapture("myvideo.mp4")
all_frames = []
while 1:
    ret, frame = video.read()
    if ret:
        all_frames.append(frame)
        continue
    break

below is some data about the video that might help you 以下是有关可能对您有所帮助的视频的一些数据
the video contains 7000 frames. 该视频包含7000帧。
every frame has (1080, 1920) dimension 每帧具有(1080,1920)维度

You can't afford to do that this way. 你不能这样做。

When reading, the frames are uncompressed from the .mp4 to raw output like 3 bytes per pixel or such. 在读取时,帧从.mp4原始输出未压缩 ,如每像素3个字节等。

So you want to store 7000*3*1080*1920 bytes total which is roughly 43 Gb !! 所以你想存储总共7000 * 3 * 1080 * 1920字节,大约是43 Gb !!

Not to mention that the constant resizing of the list owing to append creates even more copies, so even if you had the memory available, this would be very long. 更何况,由于名单的不断调整大小append创造更加副本,所以即使你有可用内存,这将是很长的。

The idea behind this program is probably to analyse the frames. 该计划背后的想法可能是分析框架。 So basically you don't need all the frames in memory at the same time. 所以基本上你不需要同时在内存中的所有帧。

In that case, read a small number of them (in a revolving buffer), perform your shape detection analysis, whatever, store the analysed data (much smaller) and drop the raw data, repeat (programs performing real-time analysis cannot store all the data, because they're running forever) 在这种情况下,读取少数(在旋转缓冲区中),执行形状检测分析,无论如何,存储分析的数据(小得多)并丢弃原始数据,重复(执行实时分析的程序不能存储所有数据,因为它们永远在运行)

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

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