-
Notifications
You must be signed in to change notification settings - Fork 53
/
test_processing.py
113 lines (86 loc) · 3.65 KB
/
test_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
import os
import time
from DisplayUtils.Colors import bcolors
from ImageProcessing import FrameProcessor, ProcessingVariables
std_height = 90
# thresh = 73 # 1-50 mod 2 25
# erode = 3 # 3-4 2
# adjust = 15 # 10-40 30
# blur = 9 # 5-15 mod 2 7
erode = ProcessingVariables.erode
threshold = ProcessingVariables.threshold
adjustment = ProcessingVariables.adjustment
iterations = ProcessingVariables.iterations
blur = ProcessingVariables.blur
version = '_2_0'
test_folder = 'tests/single_line'
frameProcessor = FrameProcessor(std_height, version, False, write_digits=False)
def test_img(path, expected, show_result=True):
frameProcessor.set_image(path)
(debug_images, calculated) = frameProcessor.process_image(blur, threshold, adjustment, erode, iterations)
if expected == calculated:
if show_result:
print(bcolors.OKBLUE + 'Testing: ' + path + ', Expected ' + expected + ' and got ' + calculated + bcolors.ENDC)
return True
else:
if show_result:
print(bcolors.FAIL + 'Testing: ' + path + ', Expected ' + expected + ' and got ' + calculated + bcolors.ENDC)
return False
def get_expected_from_filename(filename):
expected = filename.split('.')[0]
expected = expected.replace('A', '.')
expected = expected.replace('Z', '')
return expected
def run_tests(show_result=True):
count = 0
correct = 0
start_time = time.time()
for file_name in os.listdir(test_folder):
# Skip hidden files
if not file_name.startswith('.'):
count += 1
expected = get_expected_from_filename(file_name)
is_correct = test_img(test_folder + '/' + file_name, expected, show_result)
if is_correct:
correct += 1
print("\nFiles tested: " + str(count))
print("Files correct: " + str(correct))
acc = round(float(correct) / count * 100, 2)
print("Test Params - erode: " + str(erode) + ", blur: " + str(blur) + ", adjust: " + str(
adjustment) + ", thres: " + str(threshold) + ", iterations: " + str(iterations))
print("Test Accuracy: " + bcolors.BOLD + str(acc) + '%' + bcolors.ENDC)
return acc
def bulk_run():
start_time = time.time()
max_acc = 0
best_erode = 0
global erode, blur, adjustment, threshold, iterations
for test_erode in range(3, 5):
erode = test_erode
for test_blur in range(5, 11, 2):
blur = test_blur
for test_adjust in range(10, 40, 2):
adjustment = test_adjust
for test_thres in range(11, 201, 2):
threshold = test_thres
for test_iterations in range(2, 5, 1):
iterations = test_iterations
acc = run_tests(show_result=False)
if acc > max_acc:
best_thres = threshold
best_erode = erode
best_blur = blur
best_adjust = adjustment
max_acc = acc
best_iterations = iterations
print("Best Params - erode: " + str(best_erode) + ", blur: " + str(best_blur) + ", adjust: " + str(
best_adjust) + ", thres: " + str(best_thres) + ", iterations: " + str(best_iterations))
print("Best Accuracy: " + bcolors.BOLD + str(max_acc)) + bcolors.ENDC
print("--- %s seconds ---" % (time.time() - start_time))
def main():
start_time = time.time()
acc = run_tests()
print("--- %s seconds ---" % (time.time() - start_time))
# bulk_run()
if __name__ == "__main__":
main()