Moh*_*ham 2 python opencv out-of-memory google-colaboratory
I have several images in a folder and i am trying to turn each one into grayscale and save them into another folder
Google Colab session keeps crashing due to running out of ram and i have tried using del on every variable
here is my code
img_array = []
for filename in FileArray:
img = cv2.imread('train/train/Img-'+filename)
height, width, layers = img.shape
size = (width, height)
img_array.append(img)
image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imwrite('train/gray/Img-'+filename, image)
del img
del height
del width
del layers
del size
del image
Run Code Online (Sandbox Code Playgroud)
Even though you are deleting img, the image is still held in memory in the img_array list. If you have a lot of images in FileArray, you can very quickly chew through your RAM by keep them all in memory.
Try removing the line:
img_array.append(img)
Run Code Online (Sandbox Code Playgroud)