text_plain_output_1.png
from keras.preprocessing import image

from os import listdir

from sklearn.preprocessing import LabelEncoder

import cv2

import numpy as np

INIT_LR = 0.001
BS = 32
default_image_size = tuple((72, 72))
image_size = 72
directory_root = '../input/brain-tumor-classification-mri/Training'
width = 256
height = 256
depth = 3

def convert_image_to_array(image_dir):
    try:
        image = cv2.imread(image_dir)
        if image is not None:
            image = cv2.resize(image, default_image_size)
            image = cv2.convertScaleAbs(image, alpha=1.2, beta=0)
            return image.astype(np.uint8)
        else:
            return np.array([])
    except Exception as e:
        return None

image_list, label_list = ([], [])
try:
    root_dir = listdir(directory_root)
    brain_folder_list = listdir(f'{directory_root}/')
    for brain_folder in brain_folder_list:
        brain_image_list = listdir(f'{directory_root}/{brain_folder}/')
        for image in brain_image_list[:500]:
            image_directory = f'{directory_root}/{brain_folder}/{image}'
            if image_directory.endswith('.jpg') == True or image_directory.endswith('.JPG') == True:
                image_list.append(convert_image_to_array(image_directory))
                label_list.append(brain_folder)
                exit
except Exception as e:

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(label_list)
label_list = y.reshape(len(image_list), 1)

label_list.shape