|
1 | | -import Extractor |
2 | | -import os |
3 | | -import numpy as np |
4 | | -from PIL import Image |
5 | | - |
6 | | - |
7 | | -def loop_images(): |
8 | | - root = os.getcwd() |
9 | | - for x in range(10): |
10 | | - os.chdir(root +"/train_images_sorted/" + str(x)) |
11 | | - i = 0 |
12 | | - total = [] |
13 | | - for filename in os.listdir(os.getcwd()): |
14 | | - i += 1 |
15 | | - image = Extractor.getImage(filename) |
16 | | - matrix = Extractor.ImageToMatrix(image) |
17 | | - data = black_percentage(matrix) |
18 | | - if len(total) == len(data): |
19 | | - total = add_grayscale(data, total) |
20 | | - else: |
21 | | - total = data |
22 | | - array = average_percentage(total ,i) |
23 | | - print("Digit " + str(x) + " is complete") |
24 | | - img = Image.fromarray(array.astype(np.uint8)) |
25 | | - img.save("average.tif") |
26 | | - # img.show() |
27 | | - |
28 | | -def black_percentage(grayscale): |
29 | | - """ |
30 | | - NumPy 2D array -> same dimension NumPy 2D array |
31 | | - |
32 | | - returns black % for each pixel, 1 is completely black, 0 is white |
33 | | - """ |
34 | | - |
35 | | - r = np.zeros((grayscale.shape[0], grayscale.shape[1]), dtype=int) |
36 | | - for row in range(len(grayscale)): |
37 | | - for col in range(len(grayscale[row])): |
38 | | - r[row][col] = grayscale[row][col] |
39 | | - return r |
40 | | - |
41 | | -def average_percentage(sum_grayscale, num): |
42 | | - r = np.zeros((sum_grayscale.shape[0], sum_grayscale.shape[1]), dtype=int) |
43 | | - for row in range(len(sum_grayscale)): |
44 | | - for col in range(len(sum_grayscale[row])): |
45 | | - r[row][col] = (sum_grayscale[row][col])//num |
46 | | - return r |
47 | | - |
48 | | -def add_grayscale(g1, g2): |
49 | | - r = np.zeros((g1.shape[0], g1.shape[1]), dtype=int) |
50 | | - for row in range(len(g1)): |
51 | | - for col in range(len(g1[row])): |
52 | | - r[row][col] = g1[row][col] + g2[row][col] |
53 | | - return r |
54 | | - |
55 | | - |
56 | | -if __name__ == "__main__": |
57 | | - loop_images(); |
| 1 | +import Extractor |
| 2 | +import os |
| 3 | +import numpy as np |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | + |
| 7 | +def loop_images(): |
| 8 | + FOLDER_NAME = "/-Averaged Approach-" |
| 9 | + |
| 10 | + os.chdir('..') |
| 11 | + root = os.getcwd() |
| 12 | + try: |
| 13 | + new_dir = root+FOLDER_NAME+"/trained_digits/" |
| 14 | + os.makedirs(new_dir) |
| 15 | + for x in range(10): |
| 16 | + os.chdir(root +"/train_images_sorted/" + str(x)) |
| 17 | + i = 0 |
| 18 | + total = [] |
| 19 | + for filename in os.listdir(os.getcwd()): |
| 20 | + i += 1 |
| 21 | + image = Extractor.getImage(filename) |
| 22 | + matrix = Extractor.ImageToMatrix(image) |
| 23 | + data = black_percentage(matrix) |
| 24 | + if len(total) == len(data): |
| 25 | + total = add_grayscale(data, total) |
| 26 | + else: |
| 27 | + total = data |
| 28 | + array = average_percentage(total ,i) |
| 29 | + print("Digit " + str(x) + " is complete") |
| 30 | + |
| 31 | + os.chdir(new_dir) |
| 32 | + img = Image.fromarray(array.astype(np.uint8)) |
| 33 | + img.save(str(x)+".tif") |
| 34 | + except Exception as e: |
| 35 | + print("trained_digits directory already exists") |
| 36 | + |
| 37 | +def black_percentage(grayscale): |
| 38 | + """ |
| 39 | + NumPy 2D array -> same dimension NumPy 2D array |
| 40 | + |
| 41 | + returns black % for each pixel, 1 is completely black, 0 is white |
| 42 | + """ |
| 43 | + |
| 44 | + r = np.zeros((grayscale.shape[0], grayscale.shape[1]), dtype=int) |
| 45 | + for row in range(len(grayscale)): |
| 46 | + for col in range(len(grayscale[row])): |
| 47 | + r[row][col] = grayscale[row][col] |
| 48 | + return r |
| 49 | + |
| 50 | +def average_percentage(sum_grayscale, num): |
| 51 | + r = np.zeros((sum_grayscale.shape[0], sum_grayscale.shape[1]), dtype=int) |
| 52 | + for row in range(len(sum_grayscale)): |
| 53 | + for col in range(len(sum_grayscale[row])): |
| 54 | + r[row][col] = (sum_grayscale[row][col])//num |
| 55 | + return r |
| 56 | + |
| 57 | +def add_grayscale(g1, g2): |
| 58 | + r = np.zeros((g1.shape[0], g1.shape[1]), dtype=int) |
| 59 | + for row in range(len(g1)): |
| 60 | + for col in range(len(g1[row])): |
| 61 | + r[row][col] = g1[row][col] + g2[row][col] |
| 62 | + return r |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + loop_images(); |
0 commit comments