-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_preprocessing.py
96 lines (69 loc) · 2.63 KB
/
image_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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from PIL import Image
import os
from scipy import misc
import numpy as np
import pickle
# collect all the images and reduce their resoultion to a constant size
desired_size = 64
size = 64, 64
example_data = []
def reduce_resolution(directory, save_dir):
for img in os.listdir(directory):
im = Image.open(directory + img)
old_size = im.size # old_size[0] is in (width, height) format
ratio = float(desired_size) / max(old_size)
new_size = tuple([int(x * ratio) for x in old_size])
im.thumbnail(size)
new_im = Image.new("RGB", (desired_size, desired_size))
new_im.paste(im, ((desired_size-new_size[0])//2,
(desired_size-new_size[1])//2))
new_im.save(save_dir + img, "JPEG")
def average_pixel(pixel):
return np.average(pixel)
def read_image(directory, label):
for img in os.listdir(directory):
ls = {"features": [], "label": []}
image = misc.imread(directory + img)
w, h = Image.open(directory + img).size
for x in range(0, w):
for y in range(0, h):
ls["features"].append(image[x][y][0])
ls["features"].append(image[x][y][1])
ls["features"].append(image[x][y][2])
#print(ls["features"])
ls["label"] = label
#print(len(ls["features"]))
example_data.append(ls)
#for example in example_data:
#print(example)
def get_training_data():
pickle_file = open("training_obj.pickle", "rb")
data = pickle.load(pickle_file)
return data
def get_testing_data():
pickle_file = open("test_obj.pickle", "rb")
data = pickle.load(pickle_file)
return data
#train preprocessing
#reduce_resolution("./dataset/train/hot_dog/", "./dataset/train_red/hot_dog/")
#reduce_resolution("./dataset/train/not_hot_dog/", "./dataset/train_red/not_hot_dog/")
#test preprocessing
#reduce_resolution("./dataset/test/hot_dog/", "./dataset/test_red/hot_dog/")
#reduce_resolution("./dataset/test/not_hot_dog/", "./dataset/test_red/not_hot_dog/")
#train prepare data set
#read_image("./dataset/train_red/hot_dog/", [1, 0])
#read_image("./dataset/train_red/not_hot_dog/", [0, 1])
#test prepare data set
#read_image("./dataset/test_red/hot_dog/", 0)
#read_image("./dataset/test_red/not_hot_dog/", 1)
#save training data set
#shuffle everything
#np.random.shuffle(example_data)
#pickle_file = open("training_obj.pickle", "wb")
#pickle.dump(example_data, pickle_file)
#pickle_file.close()
#save test data set
#np.random.shuffle(example_data)
#pickle_file = open("test_obj.pickle", "wb")
#pickle.dump(example_data, pickle_file)
#pickle_file.close()