-
Notifications
You must be signed in to change notification settings - Fork 21
/
preprocess.py
48 lines (44 loc) · 1.49 KB
/
preprocess.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
import numpy as np
from PIL import Image
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.preprocessing import image
def binarylab(labels, size, nb_class):
y = np.zeros((nb_class,size,size))
for i in range(size):
for j in range(size):
y[labels[i][j],i, j] = 1
return y
def load_data(path, size=224, mode=None):
img = Image.open(path)
w,h = img.size
if w < h:
if w < size:
img = img.resize((size, size*h//w))
w, h = img.size
else:
if h < size:
img = img.resize((size*w//h, size))
w, h = img.size
img = img.crop((int((w-size)*0.5), int((h-size)*0.5), int((w+size)*0.5), int((h+size)*0.5)))
if mode=="original":
return img
if mode=="label":
y = np.array(img, dtype=np.int32)
mask = y == 255
y[mask] = 0
y = binarylab(y, size, 21)
y = np.expand_dims(y, axis=0)
return y
if mode=="data":
X = image.img_to_array(img)
X = np.expand_dims(X, axis=0)
X = preprocess_input(X)
return X
def generate_arrays_from_file(names, path_to_train, path_to_target, img_size, nb_class):
while True:
for name in names:
Xpath = path_to_train + "{}.jpg".format(name)
ypath = path_to_target + "{}.png".format(name)
X = load_data(Xpath, img_size, mode="data")
y = load_data(ypath, img_size, mode="label")
yield (X, y)