-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
105 lines (86 loc) · 3.18 KB
/
test.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
import glob
import os
import pytesseract
import cv2, numpy as np
import csv
from AccuracyMeter import AccuracyMeter
from AlignmentExtractor import AlignmentExtractor
from Preprocessor import Preprocessor
from ResultDiffer import ResultDiffer
from StringReader import StringReader, headers, headers_list
from TicketExtractor import TicketExtractor
def get_new(old):
new = np.ones(old.shape, np.uint8)
cv2.bitwise_not(new,new)
return new
def filterTicket(contours):
for cont in contours:
peri = cv2.arcLength(cont, True)
rect = cv2.approxPolyDP(cont, 0.04 * peri, True).copy().reshape(-1, 2)
if len(rect) >= 4:
# rects.append(rect)
# get rotated rectangle from outer contour
rotrect = cv2.minAreaRect(rect)
# get angle from rotated rectangle
(x, y), (width, height), angle = rotrect
if (height < width):
angle = 90 + angle
mapMatrix = cv2.getRotationMatrix2D((x, y), angle, 1.0)
print(angle, "deg")
return rect, mapMatrix, rotrect
def crop_minAreaRect(img, rect):
# rotate img
(x, y), (width, height), angle = rect
rows,cols = img.shape[0], img.shape[1]
if (height < width):
angle = 90 + angle
M = cv2.getRotationMatrix2D((x, y),angle,1)
img_rot = cv2.warpAffine(img,M,(cols,rows))
# rotate bounding box
rect0 = ((x, y), (height, width), angle)
box = cv2.boxPoints(rect0)
pts = np.int0(cv2.transform(np.array([box]), M))[0]
pts[pts < 0] = 0
# crop
img_crop = img_rot[pts[1][1]:pts[0][1],
pts[1][0]:pts[2][0]]
return img_crop
if __name__ == '__main__':
fotos = os.getcwd()+'/input'
output = os.getcwd()+'/output'
processed = os.getcwd()+'/processed'
os.chdir(fotos)
files = glob.glob("*.jpg")
os.chdir(fotos)
template = cv2.imread("template.jpg")
results = []
for file in files:
print(file)
os.chdir(fotos)
orig = cv2.imread(file)
# ticketExtractor = TicketExtractor(orig.copy(), file)
# ticket = ticketExtractor.extract()
preprocessor = Preprocessor(orig)
image = preprocessor.preprocess(upper= np.array([130 / 2, 10, 10]), lower=np.array([270 / 2, 255, 255]))
custom_config = r'--oem 3 --psm 6'
string = pytesseract.image_to_string(image, lang="custom", config=custom_config)
image_align = preprocessor.correctSizeBRG(orig)
extractor = AlignmentExtractor(image_align, template)
result_align = extractor.extract(debug=True)
reader = StringReader(string)
result_parse = reader.parse()
final_result = ResultDiffer(result_align, result_parse).reconcile(debug=False)
print(final_result.csv())
results.append((file, final_result))
os.chdir(processed)
cv2.imwrite(file, image)
# print(string)
# print(accuracyMeter.measure())
os.chdir(output)
with open('results.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headers_list())
for r in results:
res = r[1].asList()
res.append(r[0])
writer.writerow(res)