-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_pickles.py
73 lines (62 loc) · 2.46 KB
/
make_pickles.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import os
import cv2
from tqdm import tqdm
import random
import pickle
def main():
dir = "C:\\Users\\singe\\Documents\\Human Classifier"
imageSize = 100
make_pickles(dir, imageSize)
#takes each image in folder and makes an data array of every image
#returns an array of arrays of data of images
def create_data(dir, categories, imageSize, isValidate=False):
data = []
for category in categories:
path = dir
if (isValidate):
path = os.path.join(dir, "validate", category)
else:
path = os.path.join(dir, "train", category)
classNum = categories.index(category)
for image in tqdm(os.listdir(path)):
imageArray = cv2.imread(os.path.join(path, image), cv2.IMREAD_COLOR)
normalizedImageArray = cv2.resize(imageArray, (imageSize, imageSize))
data.append([normalizedImageArray, classNum])
return data
#makes pickle of images in folder
def make_pickles(dir, imageSize):
path = os.path.join(dir, "categories")
for category in os.listdir(path):
path = os.path.join(dir, "categories")
categories = os.listdir(os.path.join(path, category, "train"))
for isValidate in [False, True]:
path = os.path.join(dir, "categories")
data = create_data(os.path.join(path, category), categories, imageSize, isValidate)
random.shuffle(data)
xData, yData = process_data(data, imageSize)
path = os.path.join(path, category, "pickles")
if (not os.path.exists(path)):
os.mkdir(path)
if (isValidate):
pickleOutX = open(os.path.join(path, "x" + category + "Validate" + ".pickle"), "wb")
pickleOutY = open(os.path.join(path, "y" + category + "Validate" + ".pickle"), "wb")
else:
pickleOutX = open(os.path.join(path, "x" + category + ".pickle"), "wb")
pickleOutY = open(os.path.join(path, "y" + category + ".pickle"), "wb")
pickle.dump(xData, pickleOutX, protocol=4)
pickle.dump(yData, pickleOutY, protocol=4)
pickleOutX.close()
pickleOutY.close()
#normalize data of images
def process_data(data, imageSize):
X = []
y = []
for features, label in data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1, imageSize, imageSize, 3)
X = X / 255.0
return X, y
if __name__ == "__main__":
main()