text_plain_output_1.png
from tqdm import tqdm

import cv2

import gif2numpy

import matplotlib.pyplot as plt

import numpy as np

import os

root = '/kaggle/input/retinal-vessel-segmentation/DRIVE/'
exts = ('jpg', 'JPG', 'png', 'PNG', 'tif', 'gif', 'ppm')

def Data_sorting(input_data, target_data, exts):
    images = sorted([os.path.join(input_data, fname) for fname in os.listdir(input_data) if fname.endswith(exts) and (not fname.startswith('.'))])
    masks = sorted([os.path.join(target_data, fname) for fname in os.listdir(target_data) if fname.endswith(exts) and (not fname.startswith('.'))])
    return (images, masks)

def Create_Dataset(folder_path, is_mask, img_height, img_width, img_channels):
    length = len(folder_path)
    X = np.zeros((length, img_height, img_width, img_channels), dtype=np.uint8)
    y = np.zeros((length, img_height, img_width, 1), dtype=np.bool)
    if not is_mask:
        for id, fname in tqdm(enumerate(folder_path)):
            if fname.endswith('gif'):
                img = gif2numpy.convert(fname)[0][0]
            else:
                img = cv2.imread(fname)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = cv2.resize(img, (img_height, img_width))
            X[id] = img
        return X
    if is_mask:
        for id, fname in tqdm(enumerate(folder_path)):
            if fname.endswith('gif'):
                img = gif2numpy.convert(fname)[0][0]
            else:
                img = cv2.imread(fname)
            img = cv2.resize(img, (img_height, img_width))
            img = img[:, :, 0]
            img = np.expand_dims(img, axis=-1)
            y[id] = img
        return y

IMG_HEIGHT = 512
IMG_WIDTH = 512
IMG_CHANNELS = 3
X_train = Create_Dataset(folder_path=images_drive_train, is_mask=False, img_height=IMG_HEIGHT, img_width=IMG_WIDTH, img_channels=IMG_CHANNELS)
X_test = Create_Dataset(folder_path=images_drive_test, is_mask=False, img_height=IMG_HEIGHT, img_width=IMG_WIDTH, img_channels=IMG_CHANNELS)
y_train = Create_Dataset(folder_path=masks_drive_train, is_mask=True, img_height=IMG_HEIGHT, img_width=IMG_WIDTH, img_channels=1)
y_test = Create_Dataset(folder_path=masks_drive_test, is_mask=True, img_height=IMG_HEIGHT, img_width=IMG_WIDTH, img_channels=1)

plt.imshow(X_test[5])