application_vnd.jupyter.stderr_output_2.png text_plain_output_3.png text_plain_output_1.png
import cv2

import matplotlib.pyplot as plt

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')

train_pneumonia = '/kaggle/input/chest-xray-pneumonia/chest_xray/train/PNEUMONIA'
train_nornal = '/kaggle/input/chest-xray-pneumonia/chest_xray/train/NORMAL'
x = (len(os.listdir(train_pneumonia)), len(os.listdir(train_nornal)))
labels = ['PNEUMONIA', 'NORMAL']
color = ['yellow', 'green']

#  visualize
figure = plt.figure(figsize=(12, 12))
img1 = figure.add_subplot(1, 2, 1)
img_plot = plt.imshow(train[3][0], cmap = 'gray')
img1.set_title(labels[train[3][1]])
plt.axis("off")    

#  Visualize
img2 = figure.add_subplot(1, 2, 2)
img2_plot = plt.imshow(train[4][0], cmap = 'gray')
img2.set_title(labels[train[4][1]])
plt.axis('off')

sample = train[2][0]
rgb = cv2.cvtColor(sample, cv2.COLOR_BGR2RGB)
plt.imshow(rgb)