-
Notifications
You must be signed in to change notification settings - Fork 0
/
subprocesses.py
50 lines (39 loc) · 1.44 KB
/
subprocesses.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
import cv2
import os
import numpy as np
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
# Param1: Path of Image dataset
# Return: data : NumPy array uint8
# labels: NumPy array float64
def preProcess(imgPath):
img_path = imgPath #"Dataset"
dataset = []
width = 256
height = 256
for directory in os.listdir(img_path):
path = os.path.join(img_path, directory)
if not os.path.isdir(path):
continue
for item in os.listdir(path):
if item.startswith("."):
continue
img = cv2.imread(os.path.join(path, item))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (width, height))
dataset.append([img, directory])
# labeling
data, labels = zip(*dataset)
data = np.array(data, dtype="float32")
data = data.reshape(data.shape[0], width, height, 1) # 1 is channel
#data = np.expand_dims(data, axis=0)
# covid : 0
# non-covid : 1
labels = onehotLabels(labels)
return data, labels, dataset
def onehotLabels(values):
labelEncoder = LabelEncoder()
integerEncoded = labelEncoder.fit_transform(values)
onehotEncoder = OneHotEncoder(sparse=False)
integerEncoded = integerEncoded.reshape(len(integerEncoded), 1)
onehot_encoded = onehotEncoder.fit_transform(integerEncoded)
return onehot_encoded