简体   繁体   中英

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. I am using Python's OpenCV library to do it. 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. 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.
every frame has (1080, 1920) dimension

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.

So you want to store 7000*3*1080*1920 bytes total which is roughly 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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