-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_preprocessing.py
51 lines (35 loc) · 1.13 KB
/
data_preprocessing.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 cv2
import os
dataset_dir = "dataset"
categories = os.listdir(dataset_dir)
labels = []
for i in range(len(categories)):
labels.append(i)
label_dict = dict(zip(categories, labels))
# print(label_dict)
# print(categories)
# print(labels)
image_size = 100
data = []
target = []
for category in categories:
folder_path = os.path.join(dataset_dir, category)
image_names = os.listdir(folder_path)
for image_name in image_names:
image_path = os.path.join(folder_path, image_name)
try:
image = cv2.imread(image_path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
resized = cv2.resize(gray_image, (image_size, image_size))
data.append(resized)
target.append(label_dict[category])
except Exception as error:
print("Error: ", error)
import numpy as np
data = np.array(data)/255.0
data = np.reshape(data, (data.shape[0], image_size, image_size, 1))
target = np.array(target)
from keras.utils import np_utils
new_target = np_utils.to_categorical(target)
np.save("data_save", data)
np.save("target_save", target)