-
Notifications
You must be signed in to change notification settings - Fork 35
/
test_ocr.php
123 lines (107 loc) · 4.15 KB
/
test_ocr.php
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
<style>
.blue{color:blue;}
.green{color:green;}
.red{color:red;}
</style>
<h1 class='blue'>OCR Test:</h1>
<?php
function OCR($img, $expected, $input, $lookup_array, $ann) {
echo "Image: <img src='images/$img'><br>" . PHP_EOL;
$calc_out = fann_run($ann, $input);
echo 'Raw: ' . $calc_out[0] . '<br>' . PHP_EOL;
echo 'Trimmed: ' . floor($calc_out[0]*100)/100 . '<br>' . PHP_EOL;
echo 'Decoded Symbol: ';
for($i = 0; $i < count($lookup_array); $i++) {
if( floor($lookup_array[$i][0]*100)/100 == floor($calc_out[0]*100)/100) {
echo $lookup_array[$i][1] . '<br>' . PHP_EOL;
echo "Expected: $expected <br>" . PHP_EOL;
echo 'Result: ';
if($expected == $lookup_array[$i][1]){
echo '<span class="green">Correct!</span>';
}else{
echo '<span class="red">Incorrect!</span> <a href="train_ocr.php">Retrain OCR</a>';
}
}
}
echo '<br><br>' . PHP_EOL;
}
$train_file = (dirname(__FILE__) . '/ocr_float.net');
if (!is_file($train_file))
die('<span class="red">The file ocr_float.net has not been created!</span><a href="train_ocr.php">Train OCR</a>' . PHP_EOL);
$ocr_ann = fann_create_from_file($train_file);
if ($ocr_ann) {
$result_lookup_array = array();
$curr = 0.00;
for($i = 33; $i <= 126; $i++) {
array_push($result_lookup_array, array($curr, chr($i)));
$curr+= 0.01;
}
// Pixel data for F images/32.png
/*
0000000000
0000000000
0000000000
0111111110
0110000000
0110000000
0110000000
0111111000
0110000000
0110000000
0110000000
0110000000
0110000000
0000000000
0000000000
0000000000
*/
$test_F = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// Pixel data for A images/37.png
/*
0000000000
0000000000
0000000000
0000110000
0001111000
0011001100
0110000110
0110000110
0110000110
0111111110
0110000110
0110000110
0110000110
0000000000
0000000000
0000000000
*/
$test_A = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// Pixel data for N images/45.png
/*
0000000000
0000000000
0000000000
0110000110
0111000110
0111100110
0111100110
0110110110
0110110110
0110011110
0110001110
0110001110
0110000110
0000000000
0000000000
0000000000
*/
$test_N = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
OCR('37.png', 'F', $test_F, $result_lookup_array, $ocr_ann);
OCR('32.png', 'A', $test_A, $result_lookup_array, $ocr_ann);
OCR('45.png', 'N', $test_N, $result_lookup_array, $ocr_ann);
OCR('45.png', 'N', $test_N, $result_lookup_array, $ocr_ann);
fann_destroy($ocr_ann);
} else {
die("<span class='red'>Invalid file format.</span>" . PHP_EOL);
}
?>