-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_image_recognition.py
64 lines (58 loc) · 2.45 KB
/
test_image_recognition.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
from PIL import Image
from PIL import ImageDraw
import sys
import json
import find_lines
def main():
if len(sys.argv) < 3:
raise Exception("Input parameter 1: Test image to use for processing, parameter 2: folder for image metrics json.")
image_name = sys.argv[1]
metrics_folder = sys.argv[2]
x_metrics_file = open(metrics_folder + "x.json", "r")
y_metrics_file = open(metrics_folder + "y.json", "r")
x_metrics = json.loads(x_metrics_file.read())
y_metrics = json.loads(y_metrics_file.read())
horizontal_lines, vertical_lines, image = find_lines.find_lines_cv2(image_name)
show_lines_on_image(image, horizontal_lines, vertical_lines)
cell_image_boundaries = find_lines.get_cell_image_boundaries(horizontal_lines, vertical_lines)
show_cell_images(image, cell_image_boundaries)
rows, columns = cell_image_boundaries.shape
find_lines.trim_cell_images(image, cell_image_boundaries)
matrix = []
for row in range(rows):
new_row = []
for column in range(columns):
if sum(cell_image_boundaries[row, column]) > 0:
number_image = image.crop((cell_image_boundaries[row, column]))
number_image.show()
number = find_lines.get_number_from_image(number_image, x_metrics, y_metrics)
new_row.append(number)
else:
new_row.append(0)
matrix.append(new_row)
print(matrix)
"""
Utility for putting horizontal and vertical lines back on the source image, for debugging
"""
def show_lines_on_image(image_mono, horizontal_lines, vertical_lines):
new_image = image_mono.copy()
new_image_width, new_image_height = new_image.size
d = ImageDraw.Draw(new_image)
teal = (66, 245, 239)
for h_line in horizontal_lines:
d.rectangle((0, h_line[0], new_image_width, h_line[1]), outline="#888888", fill="#888888")
for v_line in vertical_lines:
d.rectangle((v_line[0], 0, v_line[1], new_image_height), outline="#888888", fill="#888888")
new_image.show()
"""
Utility routine for debugging purposes only
"""
def show_cell_images(image_mono, cell_image_boundaries):
rows, columns = cell_image_boundaries.shape
for row in range(rows):
for column in range(columns):
if sum(cell_image_boundaries[row, column]) > 0:
number_image = image_mono.crop((cell_image_boundaries[row, column]))
number_image.show()
if __name__ == '__main__':
main()