text_plain_output_1.png image_output_1.png
import cv2

import numpy as np

import os

image_size = 150
labels = ['PNEUMONIA', 'NORMAL']
def get_data(path):
    data = list()
    for label in labels:
        image_dir = os.path.join(path, label)
        class_num = labels.index(label)
        for img in os.listdir(image_dir):
            try:
                img_arr = cv2.imread(os.path.join(image_dir, img), cv2.IMREAD_GRAYSCALE)
                resized_array = cv2.resize(img_arr, (image_size, image_size))
                data.append([resized_array, class_num])
            except Exception as e:
    return np.array(data)

train = get_data('../input/chest-xray-pneumonia/chest_xray/chest_xray/train')
test = get_data('../input/chest-xray-pneumonia/chest_xray/chest_xray/test')
val = get_data('../input/chest-xray-pneumonia/chest_xray/chest_xray/val')

print('Total number of Train image', len(train))
print()
print('Total number of Test image', len(test))
print()
print('Total number of Validation image', len(val))