Skip to content

Commit 5467fa1

Browse files
committed
Refreshing
1 parent 47fca75 commit 5467fa1

File tree

158 files changed

+3882
-337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+3882
-337
lines changed

Analyzed_Images/tmp5e3tbmld.png

-608 KB
Binary file not shown.

Analyzed_Images/tmp7tf6y4tr.png

-584 KB
Binary file not shown.

Analyzed_Images/tmp8n73cvsr.png

-681 KB
Binary file not shown.

Analyzed_Images/tmplvr73ayy.png

-658 KB
Binary file not shown.

Image_metrics_20200508/Analyzed_Images/Sudoku_20200509.txt

+410
Large diffs are not rendered by default.

Image_metrics_20200508/Analyzed_Images/sudoku2.txt

+524
Large diffs are not rendered by default.

Image_metrics_20200508/Analyzed_Images/sudoku_training.txt

+1,459
Large diffs are not rendered by default.

Image_metrics_20200508/Analyzed_Images/sudokupuzzler.26.txt

+412
Large diffs are not rendered by default.
236 KB
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from PIL import Image
2+
import sys
3+
import os
4+
from os import listdir
5+
import util
6+
import cv2
7+
import numpy
8+
from matplotlib import pyplot
9+
10+
def main():
11+
if len(sys.argv) < 3:
12+
raise Exception("Input parameter 1: folder of input images.Input parameter 2: folder for output")
13+
input_folder = sys.argv[1]
14+
output_folder = sys.argv[2]
15+
for image_file in listdir(input_folder):
16+
print("Processing: %s..." % image_file)
17+
image_file_name, image_file_extension = os.path.splitext(image_file)
18+
if image_file_extension == '' or image_file_name[0] == '.':
19+
print("Skipping, not an image file.")
20+
else:
21+
image = cv2.imread("%s/%s" % (input_folder, image_file), cv2.IMREAD_GRAYSCALE)
22+
x_coords, y_coords = util.get_cell_boundaries(image)
23+
bw_threshold = 160
24+
row = 1
25+
for y_coord in y_coords:
26+
column = 1
27+
for x_coord in x_coords:
28+
cell_image = image[y_coord[0]:y_coord[1], x_coord[0]:x_coord[1]]
29+
# util.show_image(cell_image)
30+
# Make the image monochrome. If the original pixel value > threshold, 1, otherwise 0.
31+
# We use 1 and 0 since this will make it easy to sum the pixels in each direction
32+
(thresh, monochrome_image) = cv2.threshold(cell_image, bw_threshold,
33+
1, cv2.THRESH_BINARY_INV)
34+
util.show_image(monochrome_image)
35+
# Save the image of this cell
36+
new_file_name = "%s/%s/%s%s%s%s" % (output_folder, column,
37+
image_file_name, row, column, image_file_extension)
38+
write_succeeded = cv2.imwrite(new_file_name, monochrome_image)
39+
40+
41+
column += 1
42+
43+
row += 1
44+
45+
print("Finished: %s" % image_file)
46+
47+
if __name__ == '__main__':
48+
main()

Image_metrics_20200508/x.json

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
{
2+
"0": [
3+
0.0,
4+
0.0,
5+
0.0,
6+
0.0,
7+
0.0,
8+
0.0,
9+
0.0,
10+
0.0,
11+
0.0,
12+
0.0,
13+
0.0,
14+
0.0,
15+
0.0,
16+
0.0,
17+
0.0,
18+
0.0,
19+
0.0,
20+
0.0,
21+
0.0,
22+
0.0
23+
],
224
"1": [
325
0.016697049105616955,
426
0.017534465079184235,

Image_metrics_20200508/y.json

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
{
2+
"0": [
3+
0.0,
4+
0.0,
5+
0.0,
6+
0.0,
7+
0.0,
8+
0.0,
9+
0.0,
10+
0.0,
11+
0.0,
12+
0.0,
13+
0.0,
14+
0.0,
15+
0.0,
16+
0.0,
17+
0.0,
18+
0.0,
19+
0.0,
20+
0.0,
21+
0.0,
22+
0.0
23+
],
224
"1": [
325
0.030500170901219092,
426
0.04823971744331776,
200 KB
Binary file not shown.
236 KB
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
"""
2+
Routine to compute metrics for images that are training data for OCR for
3+
numbers. Basically, we extract a rectangle containing a digit, trimmed so there
4+
is no white space around the digit and then calculate the % of pixels that are non-zero at each
5+
point along the x and y axes. Then we compute the average image size. Then compute normalized versions
6+
of these arrays of pixel %s that match the average image width and height. Finally, we compute the
7+
overall average pixel %s for each digit based on the training images we have.
8+
9+
The final data structure looks like this:
10+
11+
{
12+
"size":
13+
{
14+
"x": "average_width",
15+
"y": "average_height"
16+
},
17+
"1":
18+
{
19+
"average":
20+
{
21+
"x": [0, 1, 2, "average_width"],
22+
"y": [0, 1 ,2, "average_height"]
23+
},
24+
"raw_data":
25+
[
26+
{
27+
"filename": filename,
28+
"x": [0, 1 ,2, "width"],
29+
"y": [0, 1, 2, "height"]
30+
},
31+
{
32+
"filename": filename,
33+
"x": [],
34+
"y": []
35+
}
36+
],
37+
"normalized_data":
38+
[
39+
{
40+
"x": [0, 1 ,2, "average_width"],
41+
"y": [0, 1, 2, "average_height"]
42+
},
43+
{
44+
"x": [],
45+
"y": []
46+
}
47+
]
48+
},
49+
"2":
50+
{
51+
"average": {},
52+
"raw_data": [],
53+
"normalized_data": []
54+
},
55+
"9":
56+
{
57+
"average": {},
58+
"raw_data": [],
59+
"normalized_data": []
60+
}
61+
}
62+
63+
"""
64+
import cv2
65+
import sys
66+
import os
67+
from os import listdir
68+
import numpy
69+
import util
70+
import json
71+
72+
import io
73+
74+
black = 0
75+
white = 255
76+
metrics_array_size = 20
77+
matrix_size = 9
78+
79+
def output_json(json_to_output, output_file_name):
80+
print(json.dumps(json_to_output, indent=4))
81+
output_file = open(output_file_name, 'w')
82+
output_file.write(json.dumps(json_to_output, indent=4))
83+
output_file.close()
84+
85+
def calculate_image_metrics(raw_image_data):
86+
image_metrics = {}
87+
88+
# First, calculate average image width and height
89+
# Also begin to set up the final output JSON including copying the raw image data
90+
number_of_items = 0
91+
width_total = 0
92+
height_total = 0
93+
for i in range(1, 10):
94+
index = str(i)
95+
image_metrics[index] = {}
96+
image_metrics[index]['raw_data'] = []
97+
for item in raw_image_data[index]:
98+
number_of_items += 1
99+
width_total += len(item['x'])
100+
height_total += len(item['y'])
101+
image_metrics[index]['raw_data'].append(item)
102+
103+
# Add the overall average image dimensions
104+
average_width = int(width_total/number_of_items)
105+
average_height = int(height_total/number_of_items)
106+
image_metrics['size'] = {}
107+
image_metrics['size']['x'] = average_width
108+
image_metrics['size']['y'] = average_height
109+
110+
# Now normalize each of the images to the average size. Save the individual normalized image data
111+
# and save an overall normalized average for each digit.
112+
for i in range(1, 10):
113+
index = str(i)
114+
image_metrics[index]['normalized_data'] = []
115+
x_total = numpy.zeros((average_width), dtype=float)
116+
y_total = numpy.zeros((average_height), dtype=float)
117+
for item in image_metrics[index]:
118+
new_entry = {}
119+
if len(item['x']) == average_width:
120+
new_entry['x'] = item['x']
121+
else:
122+
new_entry['x'] = util.morph_array_to_size(item['x'], average_width)
123+
if len(item['y']) == average_height:
124+
new_entry['y'] = item['y']
125+
else:
126+
new_entry['y'] = util.morph_array_to_size(item['y'], average_height)
127+
x_total += new_entry['x']
128+
y_total += new_entry['y']
129+
image_metrics[index]['normalized_data'].append(new_entry)
130+
x_average = x_total / len(image_metrics[index])
131+
y_average = y_total / len(image_metrics[index])
132+
image_metrics[index]['average'] = {}
133+
image_metrics[index]['average']['x'] = x_average
134+
image_metrics[index]['average']['y'] = y_average
135+
136+
return image_metrics
137+
138+
def main():
139+
if len(sys.argv) < 3:
140+
raise Exception("Input parameter 1: folder of folders of training data images. Input parameter 2: folder for JSON output representing image metrics.")
141+
# input fold of images organized into folders named by the number in in the image
142+
input_folder = sys.argv[1]
143+
# output folder for json metrics
144+
output_folder = sys.argv[2]
145+
146+
raw_image_data = {}
147+
for i in range(1,10):
148+
raw_image_data[str(i)] = []
149+
150+
for image_folder_name in listdir(input_folder):
151+
image_folder = "%s/%s" % (input_folder, image_folder_name)
152+
if os.path.isdir(image_folder):
153+
print("Processing: %s..." % image_folder_name)
154+
# current_number is the number in the images in this folder
155+
current_number = int(image_folder_name)
156+
for image_file in listdir(image_folder):
157+
image_file_name, image_file_extension = os.path.splitext(image_file)
158+
if image_file_extension == '' or image_file_name[0] == '.':
159+
print("Skipping, not an image file.")
160+
else:
161+
image = cv2.imread("%s/%s" % (image_folder, image_file), cv2.IMREAD_GRAYSCALE)
162+
trimmed_image = util.trim(image)
163+
# This is summing columns vertically
164+
x_counts = trimmed_image.sum(axis=0)
165+
# This is summing rows horizontally
166+
y_counts = trimmed_image.sum(axis=1)
167+
image_size = len(x_counts) * len(y_counts)
168+
x_percentage = x_counts / image_size * 100
169+
y_percentage = y_counts / image_size * 100
170+
171+
new_entry = {
172+
'filename': image_file,
173+
'x': x_percentage.tolist(),
174+
'y': y_percentage.tolist()
175+
}
176+
raw_image_data[image_folder_name].append(new_entry)
177+
178+
print("Finished folder: %s" % image_folder)
179+
output_json(raw_image_data, "%s/raw_image_data.json" % output_folder)
180+
# :todo start here checking the calcuate metrics routine
181+
image_metrics = calculate_image_metrics(raw_image_data)
182+
183+
output_json(image_metrics, "%s/json_to_output.json" % output_folder)
184+
185+
186+
if __name__ == '__main__':
187+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from PIL import Image
2+
import sys
3+
import os
4+
from os import listdir
5+
import util
6+
import cv2
7+
import numpy
8+
from matplotlib import pyplot
9+
10+
def main():
11+
if len(sys.argv) < 3:
12+
raise Exception("Input parameter 1: folder of input images.Input parameter 2: folder for output")
13+
input_folder = sys.argv[1]
14+
output_folder = sys.argv[2]
15+
for image_file in listdir(input_folder):
16+
print("Processing: %s..." % image_file)
17+
image_file_name, image_file_extension = os.path.splitext(image_file)
18+
if image_file_extension == '' or image_file_name[0] == '.':
19+
print("Skipping, not an image file.")
20+
else:
21+
image = cv2.imread("%s/%s" % (input_folder, image_file), cv2.IMREAD_GRAYSCALE)
22+
x_coords, y_coords = util.get_cell_boundaries(image)
23+
bw_threshold = 160
24+
row = 1
25+
for y_coord in y_coords:
26+
column = 1
27+
for x_coord in x_coords:
28+
cell_image = image[y_coord[0]:y_coord[1], x_coord[0]:x_coord[1]]
29+
# util.show_image(cell_image)
30+
# Make the image monochrome. If the original pixel value > threshold, 1, otherwise 0.
31+
# We use 1 and 0 since this will make it easy to sum the pixels in each direction
32+
(thresh, monochrome_image) = cv2.threshold(cell_image, bw_threshold,
33+
1, cv2.THRESH_BINARY_INV)
34+
util.show_image(monochrome_image)
35+
# Save the image of this cell
36+
new_file_name = "%s/%s/%s%s%s%s" % (output_folder, column,
37+
image_file_name, row, column, image_file_extension)
38+
write_succeeded = cv2.imwrite(new_file_name, monochrome_image)
39+
40+
41+
column += 1
42+
43+
row += 1
44+
45+
print("Finished: %s" % image_file)
46+
47+
if __name__ == '__main__':
48+
main()

Images/IMG_1629.jpg

-5.51 MB
Binary file not shown.

Images/pound.png

30 KB

0 commit comments

Comments
 (0)