-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_helper.py
53 lines (40 loc) · 1.29 KB
/
data_helper.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
from tqdm import tqdm
import cv2
import numpy as np
from sklearn.preprocessing import LabelEncoder
from keras.utils import to_categorical
import os
def load_data(IMG_SIZE):
DAISY_DIR = 'flowers/daisy'
SUNFLOWER_DIR = 'flowers/sunflower'
TULIP_DIR = 'flowers/tulip'
DANDI_DIR = 'flowers/dandelion'
ROSE_DIR = 'flowers/rose'
X = []
Z = []
"Load the data set into X array and labels in Z array"
def make_train_data(flower_type, DIR):
for img in tqdm(os.listdir(DIR)):
label = flower_type
path = os.path.join(DIR, img)
img = cv2.imread(path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
X.append(np.array(img))
Z.append(str(label))
make_train_data('Daisy', DAISY_DIR)
print(len(X))
make_train_data('Sunflower', SUNFLOWER_DIR)
print(len(X))
make_train_data('Tulip', TULIP_DIR)
print(len(X))
make_train_data('Dandelion', DANDI_DIR)
print(len(X))
make_train_data('Rose', ROSE_DIR)
print(len(X))
""" Encodes the label. Encoding is categorical, each label is represented as binary array for length 5 """
le = LabelEncoder()
Y = le.fit_transform(Z)
Y = to_categorical(Y, 5)
X = np.array(X)
X = X / 255
return X, Y