-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_prep.py
41 lines (30 loc) · 1.05 KB
/
data_prep.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
import os
import cv2
from glob import glob
root_folder = os. getcwd() +'/data/training'
import pickle
diction = {}
from random import shuffle
dir_images = []
dir_label = []
for subdir in os.listdir(root_folder):
print('subdir',os.path.join(root_folder, subdir))
for file_count, file_name in enumerate( sorted(glob(os.path.join(root_folder, subdir)+ '/*'),key=len) ):
img = cv2.imread(file_name)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dir_images.append(img)
dir_label.append(subdir)
dir_label_shuf = []
dir_images_shuf = []
index_shuf = list(range(len(dir_label)))
shuffle(index_shuf)
for i in index_shuf:
dir_label_shuf.append(dir_label[i])
dir_images_shuf.append(dir_images[i])
diction['X_tr'] = dir_images_shuf
diction['y_tr'] = dir_label_shuf
print(len(diction['X_tr']))
print(diction['y_tr'])
print(diction['X_tr'][0].shape)
with open('training.pickle', 'wb') as handle:
pickle.dump(diction, handle, protocol=pickle.HIGHEST_PROTOCOL)