-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageprocessing.py
95 lines (87 loc) · 3.49 KB
/
imageprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
from random import shuffle
import numpy as np
import os
from tensorflow import keras
from keras import utils
from Dataset import *
from Config import *
class ImageProcessing:
# load config dict
config = SystemConfig()
# Class Variables
Dataset = None # Variable that will hold whole dataset images and its label
root_folder = "Data/Dataset/" # Directory path for folders that contains images
IMAGE_SIZE = config.get("IMAGE_SIZE") # Image size height and width
def GenerateDataSet(self):
dsM = Dataset()
data = None
if os.path.exists(
"Data/DatasetGray.npy"
): # If you have already created the dataset:
data = dsM.loadDataset()
else: # If dataset is not created:
folders = [
os.path.join(self.root_folder, x)
for x in (
"COVID/train/",
"Fibrosis/train/",
"Normal/train/",
"Tuberculosis/train/",
"PNEUMONIA/train/",
)
] # create a list contains all sub folders in our root
data = [
img
for folder in folders
for img in self.load_images_from_folder(folder)
] # get images in each subfolder
shuffle(data) # shuffle arrangement of dataset
dsM.saveDataset(
"Data/DatasetGray.npy", data
) # Save dataset to be easily used later
images = np.array(
[i[0] for i in data]
) # holds the images from dataset into this variable
labels = [
i[1] for i in data
] # holds the labels for each image into this variable
labels = keras.utils.to_categorical(
labels, self.config.get("ClassLabels")
) # Converts a class vector (integers) to binary class matrix for example COVID = 1 => [0. 1. 0. 0. 0. 0.]
return images, labels
def load_images_from_folder(self, folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder, filename), 0)
if img is not None:
img = cv2.resize(img, (self.IMAGE_SIZE, self.IMAGE_SIZE))
images.append([np.array(img), self.create_label(folder, filename)])
return images
def create_label(self, filename, imagename):
# Normal = 0
# COVID = 1
# Bacterial PNEUMONIA = 2
# Viral PNEUMONIA = 3
# Fibrosis = 4
# Tuberculosis = 5
# set label for each disease to labels in previous lines
label = "None"
if "PNEUMONIA" in filename: # check if folder name contains PNEUMONIA
if "bacteria" in imagename: # check if image name contains bacteria
label = 2
elif "virus" in imagename: # check if image name contains virus
label = 3
elif "COVID" in filename: # check if folder name contains COVID
label = 1
elif "Fibrosis" in filename: # check if folder name contains Fibrosis
label = 4
elif "Normal" in filename: # check if folder name contains Normal
label = 0
elif "Tuberculosis" in filename: # check if folder name contains Tuberculosis
label = 5
return label
def load_image(self, filepath):
img = cv2.imread(filepath, 0)
img = cv2.resize(img, (self.IMAGE_SIZE, self.IMAGE_SIZE))
return [np.array(img)]