-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
425 lines (341 loc) · 14.1 KB
/
utilities.py
File metadata and controls
425 lines (341 loc) · 14.1 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# # # This acts as a library for essential methods
import numpy as np
from PIL import Image
# # # Neural Network Utilities
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoidInverse(x):
return np.log(x / (1 - x))
def sigmoidDerivative(x):
return sigmoid(x) * (1 - sigmoid(x))
def outputCostDerivative(layer, trueValue):
# This calculates the error of output layer
# 1. Loop through the last layer
# 2. Calculate error of each node (error being derivative of cost)
# Initialisation
value = []
# Stage One
for i in range(len(trueValue)):
# Stage Two
newCostDerivative = 2 * (trueValue[i] - layer[i].output) * sigmoidDerivative(layer[i].z)
value.append(newCostDerivative)
return value
def evaluateCost(answer, trueValue):
# Gets the cost of an answer
# 1. Gets the cost of one node and sum up the total cost
# Initialisation
value = 0
for i in range(len(answer)):
# Stage One
value += (answer[i] - trueValue[i]) ** 2
return value
# # # Image manipulation
def loadImageFromPath(path):
size = 28, 28
im = Image.open(path).convert('LA')
im.thumbnail(size)
return im
def loadImageFromPIL(img):
size = 28, 28
img = img.convert('LA')
img.thumbnail(size)
return img
def centreImage(img):
# This draws a box around the number and then crops out the number and puts it in the middle of another image
# 1. Loop through image
# 2. Get the leftmost, rightmost, bottom and topmost pixel of the image
# 3. Remove possibility for errors with a simple check
# 4. Crop out image and paste the result into a new images centre
# Initialisation
width, height = img.size
left, right, top, bottom = 100000000000, 0, 0, 0
tgtImg = Image.new('LA', (width, height))
# Stage One
for y in range(height):
for x in range(width):
tgtImg.putpixel((x, y), (255, 255))
# Stage Two
white, black = img.getpixel((x, y))
if white != 255 or black != 255:
if x < left:
left = x
if x > right:
right = x
if top == 0:
top = y
bottom = y
# Stage Three
if left == 100000000000:
return img
# Stage Four
img = img.crop((left, top, right + 1, bottom + 1))
tgtImg.paste(img, (int((width / 2) - (right - left) / 2), int((height / 2) - (bottom - top) / 2)))
return tgtImg
def cleanImage(image):
# Changes the image into an array
# 1. Loop though the image
# 2. Changes the representation of the colour into a single number between 0 and 1
# 3. Change array into numpy array
# Initialisation
width, height = image.size
array = []
adjust = 0.99 / 255
# Stage One
for y in range(height):
for x in range(width):
white, black = image.getpixel((x, y))
# Stage Two
if white == 255 and black == 255:
array.append(0)
continue
elif white == 0 and black == 255:
array.append(1)
continue
elif white > black:
array.append((white - black) * adjust)
continue
else:
array.append((black - white) * adjust)
continue
# 3.
array = np.array(array)
return array
def cropOutNumber(startX, startY, img):
# This function crops out an image from its furthest left and highest pixel
# 1. Outline Image
# 2. Use the outline to determine the boundaries of the image and thus crop it out of the original. This preserves
# internal shapes
# Initialisation
width, height = img.size
numberFound = False
numberPixels = []
direction = 1
tgtImg = Image.new('LA', (width, height))
outImg = Image.new('LA', (width, height))
for x in range(width):
for y in range(height):
tgtImg.putpixel((x, y), (255, 255))
outImg.putpixel((x, y), (255, 255))
x = startX
y = startY
# Stage One
while not numberFound:
numberPixels.append([x, y])
tgtImg.putpixel((x, y), (img.getpixel((x, y))))
if x == 0:
x += 1
if y == 0:
y += 1
if x == width - 1:
x = width - 2
if y == height - 1:
y = height - 2
if direction == 1:
# Direction 6 ----------------------------------------------------------------------------------------------
if [x - 1, y - 1] not in numberPixels and img.getpixel((x - 1, y - 1)) != (255, 255):
x -= 1
y -= 1
direction = 2
# Direction 7 ----------------------------------------------------------------------------------------------
elif [x - 1, y] not in numberPixels and img.getpixel((x - 1, y)) != (255, 255):
x -= 1
direction = 1
# Direction 8 ----------------------------------------------------------------------------------------------
elif [x - 1, y + 1] not in numberPixels and img.getpixel((x - 1, y + 1)) != (255, 255):
x -= 1
y += 1
direction = 1
# Direction 1 ----------------------------------------------------------------------------------------------
elif [x, y + 1] not in numberPixels and img.getpixel((x, y + 1)) != (255, 255):
y += 1
direction = 1
# Direction 2 ----------------------------------------------------------------------------------------------
elif [x + 1, y + 1] not in numberPixels and img.getpixel((x + 1, y + 1)) != (255, 255):
x += 1
y += 1
direction = 1
# Direction 3 ----------------------------------------------------------------------------------------------
elif [x + 1, y] not in numberPixels and img.getpixel((x + 1, y)) != (255, 255):
x += 1
direction = 1
# Direction 4 ----------------------------------------------------------------------------------------------
elif [x + 1, y - 1] not in numberPixels and img.getpixel((x + 1, y - 1)) != (255, 255):
x += 1
y -= 1
direction = 2
# Direction 5 ----------------------------------------------------------------------------------------------
elif [x, y - 1] not in numberPixels and img.getpixel((x, y - 1)) != (255, 255):
y -= 1
direction = 2
else:
numberFound = True
elif direction == 2:
# Direction 2 ----------------------------------------------------------------------------------------------
if [x + 1, y + 1] not in numberPixels and img.getpixel((x + 1, y + 1)) != (255, 255):
x += 1
y += 1
direction = 1
# Direction 3 ----------------------------------------------------------------------------------------------
elif [x + 1, y] not in numberPixels and img.getpixel((x + 1, y)) != (255, 255):
x += 1
direction = 2
# Direction 4 ----------------------------------------------------------------------------------------------
elif [x + 1, y - 1] not in numberPixels and img.getpixel((x + 1, y - 1)) != (255, 255):
x += 1
y -= 1
direction = 2
# Direction 5 ----------------------------------------------------------------------------------------------
elif [x, y - 1] not in numberPixels and img.getpixel((x, y - 1)) != (255, 255):
y -= 1
direction = 2
# Direction 6 ----------------------------------------------------------------------------------------------
elif [x - 1, y - 1] not in numberPixels and img.getpixel((x - 1, y - 1)) != (255, 255):
x -= 1
y -= 1
direction = 2
# Direction 7 ----------------------------------------------------------------------------------------------
elif [x - 1, y] not in numberPixels and img.getpixel((x - 1, y)) != (255, 255):
x -= 1
direction = 2
# Direction 8 ----------------------------------------------------------------------------------------------
elif [x - 1, y + 1] not in numberPixels and img.getpixel((x - 1, y + 1)) != (255, 255):
x -= 1
y += 1
direction = 1
# Direction 1 ----------------------------------------------------------------------------------------------
elif [x, y + 1] not in numberPixels and img.getpixel((x, y + 1)) != (255, 255):
y += 1
direction = 1
else:
numberFound = True
tgtImg.putpixel((x, y), img.getpixel((x, y)))
# Stage 2
left, top, right, bottom = findBoundaryBox(tgtImg)
tgtImg = img.crop((left, top, right + 1, bottom + 1))
outImg.paste(tgtImg, (left, top))
tgtImg = outImg
return tgtImg
def cropOutNumbers(img):
# This isolates each number/shape from an image
# 1. Checks whether image is blank
# 2. Finds the furthest left and highest up pixel of a number
# 3. Crops out the image based on the pixel found
# 4. Removes the original number from the image
# 5. Checks for finish and if not repeat
# Initialisation
width, height = img.size
numbers = []
startX, startY = 0, 0
index = -1
allNumbersFound = False
# Stage 1
countOne = 0
countTwo = 0
for x in range(width):
for y in range(height):
countOne += 1
if img.getpixel((x, y)) == (255, 255):
countTwo += 1
if countOne == countTwo:
allNumbersFound = True
while not allNumbersFound:
# Stage 2
pixelFound = False
for x in range(width):
for y in range(height):
if img.getpixel((x, y)) != (255, 255) and not pixelFound:
startX = x
startY = y
pixelFound = True
# Stage 3
numbers.append(cropOutNumber(startX, startY, img))
index += 1
# Stage 4
for y in range(height):
for x in range(width):
if numbers[index].getpixel((x, y)) != (255, 255):
img.putpixel((x, y), (255, 255))
# Stage 5
countOne = 0
countTwo = 0
for x in range(width):
for y in range(height):
countOne += 1
if img.getpixel((x, y)) == (255, 255):
countTwo += 1
if countOne == countTwo:
allNumbersFound = True
return numbers
# This became redundant because of the better alternative of cropping, but I'm keeping it here for sentimentality sake.
def fillHolesInImages(img):
# This function fills up holes in images
# 1. Invert image colours
# 2. Flood fill outside of image with white
# 3. Combine images
width, height = img.size
invImg = Image.new('LA', (width, height))
# Inverts the colours of the input Image
for x in range(width):
for y in range(height):
if img.getpixel((x, y)) != (255, 255):
invImg.putpixel((x, y), (255, 255))
else:
invImg.putpixel((x, y), (0, 255))
# Removes the outer layer of black from the image
outImg = floodFill(0, 0, (255, 255), invImg)
# Combines the two images
for x in range(width):
for y in range(width):
if img.getpixel((x, y)) == (255, 255):
img.putpixel((x, y), outImg.getpixel((x, y)))
return img
def floodFill(x, y, colour, img):
# This is a recursive function that replaces all adjacent pixels of the same colour with a different colour
# 1. Check whether the pixel given is the right colour
# 2. Replace it with a pixel of the right colour
# 3. Choose the next pixel to change colour
width, height = img.size
# Stage One
if img.getpixel((x, y)) == (0, 255):
# Stage Two
img.putpixel((x, y), colour)
# Stage Three
if x > 0:
floodFill(x - 1, y, colour, img)
if x < width - 1:
floodFill(x + 1, y, colour, img)
if y > 0:
floodFill(x, y - 1, colour, img)
if y < height - 1:
floodFill(x, y + 1, colour, img)
return img
def findBoundaryBox(img):
# This function finds the boundaries of a shape i.e the leftmost and rightmost x values as well as the top and
# bottom y values
# 1. Loop through the image and check if the pixel is coloured.
# 2. If it is the first pixel, then it is the top one, and the last pixel is the bottom most pixel.
# 3. Then check whether the pixel is further to the right/left than the last right/left value, if so change the
# right/left value to match
# Initialisation
width, height = img.size
left = 0
right = 0
top = 0
bottom = 0
topFound = False
# Stage One
for y in range(height):
for x in range(width):
white, black = img.getpixel((x, y))
if (white, black) != (255, 255):
# Stage Two
if not topFound:
top = y
topFound = True
bottom = y
# Stage Three
if x <= left:
left = x
if x >= right:
right = x
return left, top, right, bottom