-
Notifications
You must be signed in to change notification settings - Fork 0
/
accuracytest.py
62 lines (47 loc) · 1.48 KB
/
accuracytest.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
""""coding: UTF-8"""
import cv2
import numpy as np
from PIL import Image
from tesserocr import PyTessBaseAPI
test_file = "Digits.png" # 500 of each digit, in order. Each image is 20x20. Get from google drive
img = cv2.imread(test_file)
gray = img
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # needed or not?
cells = [np.hsplit(row, 100) for row in np.vsplit(gray, 50)]
x = np.array(cells)
test = x[:, :100].reshape(-1, 400).astype(np.float32)
k = np.arange(10)
test_labels = np.repeat(k, 500)[:, np.newaxis]
index = 0
correct = 0
accs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
predictions = []
for lk in range(0, 10):
predictions.append("")
with PyTessBaseAPI(psm = 10) as api:
api.SetVariable("tessedit_char_whitelist", "0987654321")
for l in x:
for t in l:
t = Image.fromarray(t)
api.SetImage(t)
text = api.GetUTF8Text().encode("utf-8")
text = text.strip("\n")
real = str(test_labels[index])
a = int(test_labels[index])
if text in real:
correct += 1
accs[int(test_labels[index])] += 1
else:
print text
predictions[a] += text
index += 1
# print text
print correct
print index
accuracy = 100.0 * correct / 5000
print "Accuracy in %: " + str(accuracy)
print accs
for p in range(0, len(predictions)):
print "The predictions for " + str(p) + " were: "
a = predictions[p]
print ''.join(sorted(a))