I am learning Python and I can’t figure out how to download a 5 MB video and transfer it to a list of images, I have 2 GB of RAM.

Here is my code:

# преобразуем видео в список изображений def video_to_array(path): cap = cv2.VideoCapture(path) im_data = [] while(cap.isOpened()): ret, frame = cap.read() if ret == True: im_data.append(frame) else: break cap.release() cv2.destroyAllWindows() return im_data # вызываем функцию Arr=video_to_array('test.mp4') 

I suppose it's about im_data.append(frame) . I tried to set the size of the array before the start of the cycle [[width,height,3]]*frames , but the result was the same. Also tried using the numpy np.array(frames,width,height,3)

What am I doing wrong? Maybe he should take such a volume? Or is it still possible to form an array of images I do not use such a decent amount of OP?

Binding to opencv is optional. Perhaps someone will tell the library that solves my problem. Thank!

  • Give the characteristics of your video (resolution, duration, frame rate), calculate how much it should take - andreymal

2 answers 2

Just count.
For example, video format 1920x1080.
One second of video at 25 frames per second in uncompressed form will take up a volume of 155 megabytes.
13 seconds - that's your 2 gig

    In order not to occupy such a place in RAM, you can store frames on your disk, but it is worth remembering that for maximum performance, the frames with which you want to work must be in RAM. I changed your program a bit, and now every frame is immediately poured onto the disk.

     import cv2 import numpy as np # преобразуем видео в список изображений def video_to_array(path): cap = cv2.VideoCapture(path) while(cap.isOpened()): ret, frame = cap.read() if ret == True: f.write(frame) else: break cap.release() cv2.destroyAllWindows() # вызываем функцию f = open("arr.txt","wb") video_to_array('123.mp4') f.close()