-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_pickler.py
164 lines (132 loc) · 4.22 KB
/
data_pickler.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
from skimage.feature import hog
from skimage import data, exposure
import PIL
import pickle
pstri = './'
fstri = '/home/placements2018/forgit/domain_adaptation_images/'
dir_lis = ['back_pack', 'bike', 'bike_helmet', 'bookcase', 'bottle']
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
def do_avg(dep_ar):
assert (dep_ar.shape[0] == 3)
return (dep_ar[0] + dep_ar[1] + dep_ar[2]) // 2
def do_weighted_avg(dep_ar):
assert (dep_ar.shape[0] == 3)
G = dep_ar[0] * 0.299 + dep_ar[1] * 0.587 + dep_ar[2] * 0.114
return G
def to_gray(image_ar):
assert (len(image_ar.shape) == 3)
grey_ar = np.zeros(image_ar.shape[:-1])
for rownum in range(image_ar.shape[0]):
for colnum in range(image_ar.shape[1]):
grey_ar[rownum][colnum] = do_weighted_avg(image_ar[rownum][colnum])
return grey_ar
def find_features_amazon(file_st):
image = np.asarray(PIL.Image.open(file_st))
image = to_gray(image)
# print(image.shape)
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(150, 150), block_norm='L1-sqrt',
cells_per_block=(1, 1), visualise=True)
return fd
def find_features_dslr(file_st):
image = np.asarray(PIL.Image.open(file_st))
image = to_gray(image)
# print(image.shape)
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(500, 500), block_norm='L1-sqrt',
cells_per_block=(1, 1), visualise=True)
return fd
def find_features_webcam(file_st):
image = np.asarray(PIL.Image.open(file_st))
image = to_gray(image)
# print(image.shape)
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(211, 211), block_norm='L1-sqrt',
cells_per_block=(1, 1), visualise=True)
return fd
def make_data_from_image_webcam(stri, dir_lis):
lislis = []
label_lis = []
pickle_dic = {}
sumi = -1
for dirnum, dir_st in enumerate(dir_lis):
new_dir_stri = stri + dir_st + '/'
file_lis = list(files(new_dir_stri))
pickle_dic[dirnum] = (sumi+1, sumi+len(file_lis))
sumi += len(file_lis)
lis = []
print(file_lis)
for file_st in file_lis:
fd_ar = find_features_webcam(new_dir_stri + file_st)
lis.append(list(fd_ar))
label_lis.append(dirnum)
lislis += lis
oned_ar = np.array(label_lis, dtype='float64')
twod_ar = np.array(lislis, dtype='float64')
assert (twod_ar.shape[0] == oned_ar.shape[0])
return twod_ar, oned_ar, pickle_dic
def make_data_from_image_amazon(stri, dir_lis):
lislis = []
label_lis = []
pickle_dic = {}
sumi = -1
for dirnum, dir_st in enumerate(dir_lis):
new_dir_stri = stri + dir_st + '/'
file_lis = list(files(new_dir_stri))
lis = []
print(file_lis)
pickle_dic[dirnum] = (sumi + 1, sumi + len(file_lis))
sumi += len(file_lis)
for file_st in file_lis:
fd_ar = find_features_amazon(new_dir_stri + file_st)
lis.append(list(fd_ar))
label_lis.append(dirnum)
lislis += lis
oned_ar = np.array(label_lis, dtype='float64')
twod_ar = np.array(lislis, dtype='float64')
assert (twod_ar.shape[0] == oned_ar.shape[0])
return twod_ar, oned_ar, pickle_dic
def make_data_from_image_dslr(stri, dir_lis):
lislis = []
label_lis = []
pickle_dic = {}
sumi = -1
for dirnum, dir_st in enumerate(dir_lis):
new_dir_stri = stri + dir_st + '/'
file_lis = list(files(new_dir_stri))
lis = []
print(file_lis)
pickle_dic[dirnum] = (sumi + 1, sumi + len(file_lis))
sumi += len(file_lis)
for file_st in file_lis:
fd_ar = find_features_dslr(new_dir_stri + file_st)
lis.append(list(fd_ar))
label_lis.append(dirnum)
lislis += lis
oned_ar = np.array(label_lis, dtype='float64')
twod_ar = np.array(lislis, dtype='float64')
assert (twod_ar.shape[0] == oned_ar.shape[0])
return twod_ar, oned_ar, pickle_dic
def make_source_data():
global fstri, dir_lis
stri = fstri + 'amazon/images/'
tup = make_data_from_image_amazon(stri, dir_lis)
fs = open(pstri + "pickle_jar/src_data_with_dic.pickle", "wb")
pickle.dump(tup, fs)
fs.close()
def make_target_data():
global fstri, dir_lis
stri = fstri + 'dslr/images/'
tup = make_data_from_image_dslr(stri, dir_lis)
fs = open(pstri + "pickle_jar/tar_data_with_dic.pickle", "wb")
pickle.dump(tup, fs)
fs.close()
def make_all_pickles():
pass
if __name__ == '__main__':
make_source_data()
make_target_data()