-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_recognition.py
140 lines (117 loc) · 3.8 KB
/
stats_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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# stats_recognition.py
import PIL
import json
from pathlib import Path
import traceback
import time
import cv2 as cv
import recognition
import processing
from util import *
import os
# pildifailide asukoht
imageBase = Path("gen", "out")
# tuvastatavate märkide loetelu
samplePath = Path("gen", "singlechar.json")
# väljundkaust
outBase = Path("recognition")
# Konteksti tarbeks lisaruum märgi ümbert
padX = 10
padY = 10
os.makedirs(outBase, exist_ok=True)
with open(samplePath) as f:
rawSamples = json.load(f)
def cropToBox(img, box):
return img[box[1]:box[3], box[0]:box[2]]
samples = []
for si, rs in enumerate(rawSamples):
ssamples = []
for ci, ch, b in rs:
imagePath = imageBase / f"img{si}_{ci:03}.jpg"
metaPath = imageBase / f"meta{si}_{ci:03}.json"
with open(metaPath) as metaFile:
whitelist = json.load(metaFile)['whitelist']
ob = [int(round(x)) for x in b]
img = cv.imread(str(imagePath))
h, w, _ = img.shape
b = max(0, ob[0] - padX), max(0, ob[1] - padY), min(w, ob[2] + padX), min(h, ob[3] + padY)
img = img[b[1]:b[3], b[0]:b[2]]
box = [ob[0] - b[0], ob[1] - b[1], ob[0] - b[0] + ob[2] - ob[0], ob[1] - b[1] + ob[3] - ob[1]]
ssamples.append({"img": img, "box": box, "whitelist": whitelist})
samples.append(ssamples)
def runSingle(f, si, ci):
s = samples[si][ci]
img, box, whitelist = s["img"], s["box"], s["whitelist"]
ch = f(img, box, whitelist)
return ch
def run(f, outFile):
print(outFile, flush=True)
results = []
for si in range(len(samples)):
sresults = []
start = time.time()
for ci in range(len(samples[si])):
res = None
try:
res = runSingle(f, si, ci)
except:
print(f"Viga {outFile} {si} {ci}")
print(traceback.format_exc())
sresults.append(res)
end = time.time()
t = end - start
print(t, flush=True)
results.append({"time": t, "chars": sresults})
with open(outBase / outFile, "w") as f:
json.dump(results, f)
print(flush=True)
def tess1(img, box, whitelist):
ch = recognition.tessChar(img, box, whitelist)
return ch
def tess2(img, box, whitelist):
img = processing.denoise(img)
ch = recognition.tessChar(img, box, whitelist)
return ch
def tess3(img, box, whitelist):
img = processing.scale(processing.denoise(img), 2)
box = boxScale(box, 2)
ch = recognition.tessChar(img, box, whitelist)
return ch
def contour1(img, box, whitelist):
ch = recognition.contourChar(img, box, whitelist)
return ch
def contour2(img, box, whitelist):
img = processing.denoise(img)
ch = recognition.contourChar(img, box, whitelist)
return ch
def contour3(img, box, whitelist):
img = processing.scale(processing.denoise(img), 2)
box = boxScale(box, 2)
ch = recognition.contourChar(img, box, whitelist)
return ch
def pattern1(img, box, whitelist):
ch = recognition.patternChar(img, box, whitelist)
return ch
def pattern2(img, box, whitelist):
img = processing.denoise(img)
ch = recognition.patternChar(img, box, whitelist)
return ch
def pattern3(img, box, whitelist):
img = processing.scale(processing.denoise(img), 2)
box = boxScale(box, 2)
ch = recognition.patternChar(img, box, whitelist)
return ch
# genereeri eelnevalt kõik vajaminevad (loodetavasti)
# märkide võrdlusmaatriksid ja invariandid
for ch in allChars:
recognition.genCharImages(ch)
recognition.genCharContours(ch)
run(tess1, "tess1.json")
run(tess2, "tess2.json")
run(tess3, "tess3.json")
run(contour1, "contour1.json")
run(contour2, "contour2.json")
run(contour3, "contour3.json")
run(pattern1, "pattern1.json")
run(pattern2, "pattern2.json")
run(pattern3, "pattern3.json")