dimanche 10 juillet 2016

How to append reshaped / flattened images to a python list without consuming too much memory?

I'm trying to read around 2,000 grayscale images (640 x 480) from a directory using opencv, scale down the intensity values between 0 and 1, flatten the images as one-dimensional numpy arrays and append them to a list for feeding them to a Neural Network later. This is my code :

import os
import cv2 as cv

path = "images/"
img_names = os.listdir(path)

x = []

for name in img_names:
    fullpath = path+name
    img = cv.imread(fullpath, 0).astype(float)
    img /= 255.0
    x.append(img.ravel())

When I try to execute this code, my memory usage goes up rapidly and then everything starts to lag and freeze. So, what really is going wrong here? Any suggestions on improving the process? It works fast and smooth for 100 images but not with over 1000 images.

I am running Ubuntu 16.04 on a Core i5 2.6 GHz laptop with 4 GB RAM.

Aucun commentaire:

Enregistrer un commentaire