Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Memory issues when loading videos into frames

I have folder with 1000 FLV videos, each having 120 frames of size 300, 400 with RGB colors (3 channels) that I would like to load into the numpy array frames. I do this with the code:

import numpy as np
import cv2
import os

directory = "data/"
# frames = []
frames = np.empty(shape=(1000 * 120,300, 400,3), dtype=np.float32)

for file in os.listdir(directory):
    if file.endswith(".flv"):
        file_path = os.path.join(directory, file)
        nr_file = nr_file + 1
        print('File '+str(nr_file)+' of '+str(nb_files_in_dir)+' files: '+file_path)

        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name
        cap = cv2.VideoCapture(file_path)

        # Check if camera opened successfully
        if (cap.isOpened() == False):
            print("Error opening video stream or file")

        # Read until video is completed
        while (cap.isOpened()):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if ret == True:
                # frames.append(frame.astype('float32') / 255.)
                frames[nr_frame, :, :, :] = frame.astype('float32') / 255.
                nr_frame = nr_frame + 1
                nb_frames_in_file = nb_frames_in_file + 1
            else:
                break

        # When everything done, release the video capture object
        cap.release()

# frames = np.array(frames)

Originally I tried to use a list frames (see the commented lines), instead of the prerallocated numpy array, but it seemed this took too much memory - no idea why though.

However, it seems this did not help much: Still the code is very memory hungry (many GB), even though my videos are just a few KB large. I think it is because the resources of the cap-objects (the cv2.VideoCapture-objects) might not freed despite me using cap.release() - is that correct? What can I do, to make my code memory-efficient?

Memory issues when loading videos into frames

I have folder with 1000 FLV videos, each having 120 frames of size 300, 400 with RGB colors (3 channels) that I would like to load into the numpy array frames. I do this with the code:

import numpy as np
import cv2
import os

directory = "data/"
# frames = []
frames = np.empty(shape=(1000 np.empty(shape=(160 * 120,300, 400,3), dtype=np.float32)

for file in os.listdir(directory):
    if file.endswith(".flv"):
        file_path = os.path.join(directory, file)
        nr_file = nr_file + 1
        print('File '+str(nr_file)+' of '+str(nb_files_in_dir)+' files: '+file_path)

        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name
        cap = cv2.VideoCapture(file_path)

        # Check if camera opened successfully
        if (cap.isOpened() == False):
            print("Error opening video stream or file")

        # Read until video is completed
        while (cap.isOpened()):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if ret == True:
                # frames.append(frame.astype('float32') / 255.)
                frames[nr_frame, :, :, :] = frame.astype('float32') / 255.
                nr_frame = nr_frame + 1
                nb_frames_in_file = nb_frames_in_file + 1
            else:
                break

        # When everything done, release the video capture object
        cap.release()

# frames = np.array(frames)

Originally I tried to use a list frames (see the commented lines), instead of the prerallocated numpy array, but it seemed this took too much memory - no idea why though.

However, it seems this did not help much: Still the code is very memory hungry (many GB), even though my videos are just a few KB large. I think it is because the resources of the cap-objects (the cv2.VideoCapture-objects) might not freed despite me using cap.release() - is that correct? What can I do, to make my code memory-efficient?

Memory issues when loading videos into frames

I have folder with 1000 FLV videos, each having 120 frames of size 300, 400 with RGB colors (3 channels) that I would like to load into the numpy array frames. I do this with the code:

import numpy as np
import cv2
import os

directory = "data/"
# frames = []
frames = np.empty(shape=(160 * 120,300, 400,3), 120, 152, 360, 3), dtype=np.float32)

for file in os.listdir(directory):
    if file.endswith(".flv"):
        file_path = os.path.join(directory, file)
        nr_file = nr_file + 1
        print('File '+str(nr_file)+' of '+str(nb_files_in_dir)+' files: '+file_path)

        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name
        cap = cv2.VideoCapture(file_path)

        # Check if camera opened successfully
        if (cap.isOpened() == False):
            print("Error opening video stream or file")

        # Read until video is completed
        while (cap.isOpened()):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if ret == True:
                # frames.append(frame.astype('float32') / 255.)
                frames[nr_frame, :, :, :] = frame.astype('float32') / 255.
                nr_frame = nr_frame + 1
                nb_frames_in_file = nb_frames_in_file + 1
            else:
                break

        # When everything done, release the video capture object
        cap.release()

# frames = np.array(frames)

Originally I tried to use a list frames (see the commented lines), instead of the prerallocated numpy array, but it seemed this took too much memory - no idea why though.

However, it seems this did not help much: Still the code is very memory hungry (many GB), even though my videos are just a few KB large. I think it is because the resources of the cap-objects (the cv2.VideoCapture-objects) might not freed despite me using cap.release() - is that correct? What can I do, to make my code memory-efficient?

Memory issues when loading videos into frames

I have folder with 1000 160 FLV videos, each having 120 frames of size 300, 400 152, 360 with RGB colors (3 channels) that I would like to load into the numpy array frames. I do this with the code:

import numpy as np
import cv2
import os

directory = "data/"
# frames = []
frames = np.empty(shape=(160 * 120, 152, 360, 3), dtype=np.float32)

for file in os.listdir(directory):
    if file.endswith(".flv"):
        file_path = os.path.join(directory, file)
        nr_file = nr_file + 1
        print('File '+str(nr_file)+' of '+str(nb_files_in_dir)+' files: '+file_path)

        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name
        cap = cv2.VideoCapture(file_path)

        # Check if camera opened successfully
        if (cap.isOpened() == False):
            print("Error opening video stream or file")

        # Read until video is completed
        while (cap.isOpened()):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if ret == True:
                # frames.append(frame.astype('float32') / 255.)
                frames[nr_frame, :, :, :] = frame.astype('float32') / 255.
                nr_frame = nr_frame + 1
                nb_frames_in_file = nb_frames_in_file + 1
            else:
                break

        # When everything done, release the video capture object
        cap.release()

# frames = np.array(frames)

Originally I tried to use a list frames (see the commented lines), instead of the prerallocated numpy array, but it seemed this took too much memory - no idea why though.

However, it seems this did not help much: Still the code is very memory hungry (many GB), even though my videos are just a few KB large. I think it is because the resources of the cap-objects (the cv2.VideoCapture-objects) might not freed despite me using cap.release() - is that correct? What can I do, to make my code memory-efficient?