/
processing.py
233 lines (192 loc) · 7.12 KB
/
processing.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import numpy as np
from skimage.io import imread
from skimage.transform import resize
from skimage import exposure
from skimage.filter import threshold_otsu, gabor_filter
from sklearn.cross_validation import train_test_split
from utils import Utils
HAND_DRAWN_DIR = os.path.join(os.path.abspath('.'), 'smarterboard-images/')
RAND_ECOMPS_DIR = os.path.join(os.path.abspath('.'), 'rand-ecomps-images/')
class Preprocessing:
@staticmethod
def binary_from_thresh(img):
""" Base function for converting to binary using a threshold """
thresh = threshold_otsu(img)
binary = img < thresh
return binary
@staticmethod
def angle_pass_filter(img, frequency, theta, bandwidth):
""" returns the magnitude of a gabor filter response for a certain
angle """
real, imag = gabor_filter(img, frequency, theta, bandwidth)
mag = np.sqrt(np.square(real) + np.square(imag))
return mag
@staticmethod
def frontslash_filter(img, denom, freq, bandwidth):
""" intensifies edges that look like a frontslash '/' """
theta = np.pi*(1.0/denom)
return Preprocessing.angle_pass_filter(img, freq, theta, bandwidth)
@staticmethod
def backslash_filter(img, denom, freq, bandwidth):
""" intensifies edges that look like a backslash '\' """
theta = np.pi*(-1.0/denom)
return Preprocessing.angle_pass_filter(img, freq, theta, bandwidth)
class FeatureExtraction:
denom = 4.0
freq = 0.50
bw = 0.80
@staticmethod
def mean_exposure_hist(nbins, *images):
""" calculates mean histogram of many exposure histograms
args:
nbins: number of bins
*args: must be images (ndarrays) i.e r1, r2
returns:
histogram capturing pixel intensities """
hists = []
for img in images:
hist, _ = exposure.histogram(img, nbins)
hists.append(hist)
return np.sum(hists, axis=0) / len(images)
@staticmethod
def mean_exposure_hist_from_gabor(img, nbins):
frontslash = Preprocessing.frontslash_filter(
img,
FeatureExtraction.denom,
FeatureExtraction.freq,
FeatureExtraction.bw
)
backslash = Preprocessing.backslash_filter(
img,
FeatureExtraction.denom,
FeatureExtraction.freq,
FeatureExtraction.bw
)
return np.array(
FeatureExtraction.mean_exposure_hist(nbins, frontslash, backslash)
)
@staticmethod
def rawpix_nbins(image, nbins):
"""
extracts raw pixel features and a histogram of nbins
args:
image: a m x n standardized shape ndarray representing an image
nbins: nbins for histogram
"""
gabor_hist = FeatureExtraction.mean_exposure_hist_from_gabor(
image, nbins
)
image = image.flatten()
return Utils.hStackMatrices(image, gabor_hist)
class Data:
@staticmethod
def getTrainFilenames(n, dir_path=HAND_DRAWN_DIR):
filenames = os.listdir(dir_path)
np.random.shuffle(filenames)
filenames = filenames[:n]
return filenames
@staticmethod
def isResistorFromFilename(filenames):
is_resistor = [fn[0] == "r" for fn in filenames]
return is_resistor
@staticmethod
def isComponentFromFilename(filenames):
""" Parses directory of resistor and capacitor images and determines what
the expected output should be.
Arguments
---------
filenames: a str
the path to the training image directory.
Returns
-------
Y: array-like, shape (n_samples, 3)
labels or teaching examples
"""
is_resistor = [fn[0] == "r" for fn in filenames]
is_capacitor = [fn[0] == "c" for fn in filenames]
is_inductor = [fn[0] == "i" for fn in filenames]
Y = np.column_stack((is_resistor,is_capacitor,is_inductor))
return Y
@staticmethod
def loadImageFeatures(filename, nbins):
image = Data.loadImage(filename)
return FeatureExtraction.rawpix_nbins(image, nbins)
@staticmethod
def loadImage(filename, square=True):
if filename[-3:] == 'jpg':
image = imread(filename, as_grey=True)
elif filename[-3:] == 'npy':
image = np.load(filename)
if square:
sqr_image = resize(image, (100, 100))
return Preprocessing.binary_from_thresh(sqr_image)
else:
return Preprocessing.binary_from_thresh(image)
@staticmethod
def loadTrain(dir_path=HAND_DRAWN_DIR, oneHot=True):
""" loads training data (trX, trY) for the nnet theano implementation.
See dinopants174/SmarterBoard for implementation including loading histograms of
Gabor Filtered Images
Arguments
---------
dir_path: a str or None
the path to the training image directory. If None, uses the HAND_DRAWN_DIR path
specified in processing.py.
oneHot: boolean for resistor vs. resistor, capacitor, inductor testing
Returns
-------
X: array-like, shape (n_samples, n_features)
data inputs
Y: array-like, shape (n_samples, 1)
labels or teaching examples
"""
fns = Data.getTrainFilenames(-1, dir_path)
images = [np.ravel(Data.loadImage(dir_path + fn)) for fn in fns]
X = np.vstack(images)
if not oneHot:
# y has shape (y.size,)
y = np.array(Data.isResistorFromFilename(fns))
# Y has shape (y.size, 1)
Y = y.reshape(y.size, 1)
else:
Y = np.array(Data.isComponentFromFilename(fns))
return X, Y
@staticmethod
def loadTrainTest(train_size, dir_path=HAND_DRAWN_DIR):
""" loads training data, and holds out a percentage of this data for
test validation """
X, Y = Data.loadTrain(dir_path)
trX, teX, trY, teY = train_test_split(X, Y, train_size=train_size)
return trX, teX, trY, teY
def test_loadImage():
import matplotlib.pyplot as plt
resistor_path = HAND_DRAWN_DIR + 'resistor1.jpg'
img = Data.loadImage(resistor_path, square=True)
print img
plt.imshow(img, cmap='gray')
plt.title("Should be Square")
plt.show()
hist, bins = exposure.histogram(img)
plt.plot(bins, hist)
plt.show()
def test_loadTrainTest():
import matplotlib.pyplot as plt
trX, teX, trY, teY = Data.loadTrainTest(0.8, RAND_ECOMPS_DIR)
img = teX[0].reshape((100, 100))
print img
img_label = "resistor" if teY[0, 0] == 1 else "capacitor"
plt.imshow(img, cmap='gray')
plt.title("Should be %s" % img_label)
plt.show()
hist, bins = exposure.histogram(img)
plt.plot(bins, hist)
plt.show()
def test_isComponentFromFilename():
data = Data.getTrainFilenames(-1,HAND_DRAWN_DIR)
result = Data.isComponentFromFilename(data)
print result
if __name__ == '__main__':
test_loadImage()
# test_loadTrainTest()
# test_isComponentFromFilename()