forked from tesseract-ocr/tesstrain
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
_recognition_preview.ahk
291 lines (244 loc) · 8.76 KB
/
_recognition_preview.ahk
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
; (C) Copyright 2021, Bartlomiej Uliasz
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
; http://www.apache.org/licenses/LICENSE-2.0
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
; Allows preview of OCR recognition after training is done
PreviewRecognition(btnCtrl, *) {
closeRequest := false
ocrInProgress := false
searchEnabled := false
col1Width := 120
col2Width := 680
tessdataDirForOcr := DATA_DIR
modelNameForOcr := MODEL_NAME
ignoreSpaces := false
preserveSpaces := false
recognizedValue := expectedValue := ""
previewPsm := PSM
if (!_CheckGeneratedModelExistence()) {
message := "There is no newly generated model of name '" MODEL_NAME "' inside the '" DATA_DIR "' folder.`n"
if (START_MODEL) {
if (YesNoConfirmation(message "Do you want to use Start Model instead for this preview?")) {
tessdataDirForOcr := TESSDATA
modelNameForOcr := START_MODEL
} else {
return
}
} else {
NotAllowedBox(message "Please execute training first or select a Start Model.")
return
}
}
imageList := []
for fileExtension in SUPPORTED_IMAGE_FILES {
ArrayPushAll(FindAllFilesExtended(GROUND_TRUTH_DIR "\*" fileExtension), imageList)
}
if (imageList.Length == 0) {
NotAllowedBox("No supported images found in your Ground Truth directory")
return
}
imageList := ArraySort(imageList, (a,b)=>b.modified-a.modified) ; Sort by modificaton date descending
imageListPosition := 1
; Create GUI
previewGui := Gui("-Resize -DPIScale +Owner" btnCtrl.Gui.Hwnd, "Trained Image")
imageFullPath := imageList[imageListPosition].path
previewGui.Add("Text", "section xm w" col1Width, "Processed image:")
imagePathCtrl := previewGui.Add("Edit", "ys w" col2Width " r1 +ReadOnly vImagePath", imageFullPath)
ocrResultLabelCtrl := previewGui.Add("Text", "section xm w" col1Width, "Recongized value:")
previewGui.SetFont("s15 w678 cGreen")
ocrResultCtrl := previewGui.Add("Edit", "ys w" col2Width " r2 +ReadOnly vOcrResult", "")
previewGui.SetFont()
gtTxtLabelCtrl := previewGui.Add("Text", "section xm h35 w" col1Width, "Value from .gt.txt file:")
previewGui.SetFont("cBlue s15 w678")
gtTxtCtrl := previewGui.Add("Edit", "ys w" col2Width " r2 +ReadOnly vGtTxtValue", "")
previewGui.SetFont()
previousButton := previewGui.Add("Button", "xm section default Disabled" (imageListPosition == 1), "&Previous")
previousButton.OnEvent("Click", _ShowPreviousImage)
nextButton := previewGui.Add("Button", "ys Disabled" (imageListPosition == imageList.Length), "&Next")
nextButton.OnEvent("Click", _ShowNextImage)
searchWrongButton := previewGui.Add("Button", "ys", "Find next &Wrong OCR")
searchWrongButton.OnEvent("Click", _FindNextWrongOcr)
stopSearchButton := previewGui.Add("Button", "xp yp Hidden", "&Stop searching")
stopSearchButton.OnEvent("Click", _StopSearchCallback)
deleteButton := previewGui.Add("Button", "ys", "&Delete")
deleteButton.OnEvent("Click", _DeleteFilesCallback)
gotoButton := previewGui.Add("Button", "ys", "&Go to")
gotoButton.OnEvent("Click", _GotoImagePosition)
gotoPageNumber := previewGui.Add("Edit", "ys w60 Number")
gotoPageNumber.OnEvent("Change", _GotoChange)
previewGui.Add("UpDown", "Range1-" imageList.Length, 1)
previewGui.Add("Text", "ys hp +0x200", "PSM")
psmInputCtrl := previewGui.Add("Edit", "ys w50 Number")
psmInputCtrl.OnEvent("Change", _PsmChange)
previewGui.Add("UpDown", "Range1-13", previewPsm)
preserveSpacesCtrl := previewGui.Add("Checkbox", "ys Checked" preserveSpaces, "Preserve inter-word spaces")
preserveSpacesCtrl.OnEvent("Click", (ctrlObj, *)=>(preserveSpaces:=ctrlObj.Value, _RefreshGui()))
ignoreSpacesCtrl := previewGui.Add("Checkbox", "xp Checked" ignoreSpaces, "Ignore spaces")
ignoreSpacesCtrl.OnEvent("Click", (ctrlObj,*)=>(ignoreSpaces:=ctrlObj.Value, preserveSpacesCtrl.Enabled:=!ignoreSpaces, _RefreshGui()))
picCtrl := previewGui.Add("Picture", "xm w" (col1Width + col2Width) " h-1", imageFullPath)
progressStatus := previewGui.Add("StatusBar",, "Preview OCR for image 1 / " imageList.Length)
previewGui.Title := PROGRAM_TITLE " - Ground Truth Preview"
previewGui.Show("AutoSize") ; Resize the window to match the picture size.
previewGui.OnEvent("Close", _CloseCb)
previewGui.OnEvent("Escape", _CloseCb)
btnCtrl.Gui.Opt("+Disabled")
_RefreshGui()
GetGtTxtContent() {
gtTxtPath := RemoveImageExtension(imageFullPath) ".gt.txt"
if (FileExist(gtTxtPath)) {
return FileGetFirstLine(gtTxtPath)
} else {
return "<no .gt.txt file>"
}
}
_RefreshGui() {
if (closeRequest) {
return
}
ocrInProgress := true
; prepare values
imageFullPath := imageList[imageListPosition].path
recognizedValue := OcrImageFile(imageFullPath, modelNameForOcr, tessdataDirForOcr, preserveSpaces, previewPsm)
expectedValue := GetGtTxtContent()
; update displayed values
imagePathCtrl.Text := imageFullPath
gtTxtCtrl.Text := expectedValue
ocrResultCtrl.Text := recognizedValue
ocrResultCtrl.SetFont("bold " (_CompareResults(recognizedValue, expectedValue) ? "cGreen" : "cRed"))
progressStatus.SetText("Preview OCR for image " imageListPosition " / " imageList.Length)
picCtrl.Value := "*w" (col1Width + col2Width) " *h-1 " imageFullPath ; Load the image.
if (!searchEnabled) {
nextButton.Enabled := (imageListPosition < imageList.Length)
previousButton.Enabled := (imageListPosition > 1)
}
previewGui.Show("AutoSize") ; Resize the window to match the picture size.
imagePathCtrl.Focus()
ocrInProgress := false
if (closeRequest) {
_CloseCb()
}
}
_DeleteFilesCallback(*) {
if (!YesNoConfirmation("Do you want to delete current image with corresponding .gt.txt, .lstmf and .box files?")) {
return
}
imageFullPath := imageList[imageListPosition].path
FileDelete(imageFullPath)
gtTxtPath := RemoveImageExtension(imageFullPath) ".gt.txt"
if (FileExist(gtTxtPath)) {
FileDelete(gtTxtPath)
}
lstmfPath := RemoveImageExtension(imageFullPath) ".lstmf"
if (FileExist(lstmfPath)) {
FileDelete(lstmfPath)
}
boxPath := RemoveImageExtension(imageFullPath) ".box"
if (FileExist(boxPath)) {
FileDelete(boxPath)
}
imageList.RemoveAt(imageListPosition)
if (imageListPosition > imageList.Length) {
imageListPosition := imageList.Length
}
_RefreshGui()
}
_FindNextWrongOcr(searchCtrl, *) {
_ReplaceControl(searchWrongButton, stopSearchButton)
previousButton.Enabled := false
nextButton.Enabled := false
searchEnabled := true
loop {
_ShowNextImage()
} until !searchEnabled || closeRequest || imageListPosition == imageList.Length || !_CompareResults(recognizedValue, expectedValue)
SoundBeep 1800, 100
if (imageListPosition == imageList.Length) {
AotBox("No more images")
}
searchEnabled := false
if (closeRequest) {
_CloseCb()
} else {
_ReplaceControl(stopSearchButton, searchWrongButton)
previousButton.Enabled := true
nextButton.Enabled := true
}
}
_ReplaceControl(controlToReplace, newControl) {
if (closeRequest) {
return
}
controlToReplace.GetPos(&x, &y, &w, &h)
controlToReplace.Visible := false
newControl.Visible := true
newControl.Move(x, y, w, h)
}
_StopSearchCallback(stopCtrl, *) {
searchEnabled := false
}
_CompareResults(ocrResult, gtTxtValue) {
if (ignoreSpaces) {
ocrResult := StrReplace(ocrResult, " ")
gtTxtValue := StrReplace(gtTxtValue, " ")
}
return ocrResult == gtTxtValue
}
_ShowPreviousImage(*) {
if (imageListPosition == 1) {
NotAllowedBox("Already at first one")
return
}
imageListPosition -= 1
_RefreshGui()
}
_ShowNextImage(*) {
if (imageListPosition == imageList.Length) {
NotAllowedBox("No more files")
return
}
imageListPosition += 1
_RefreshGui()
}
_GotoImagePosition(*) {
imageListPosition := gotoPageNumber.Value
_RefreshGui()
}
_GotoChange(ctrlObj, *) {
if (!ctrlObj.Value) {
return
}
if (ctrlObj.Value > imageList.Length) {
ctrlObj.Value := imageList.Length
} else if (imageList.Length < 1) {
ctrlObj.Value := 1
}
}
_CloseCb(*) {
if (searchEnabled || ocrInProgress) {
closeRequest := true
return
}
btnCtrl.Gui.Opt("-Disabled")
previewGui.Destroy()
}
_CheckGeneratedModelExistence() {
generatedModelFile := DATA_DIR "\" MODEL_NAME ".traineddata"
return FileExist(generatedModelFile)
}
_PsmChange(ctrlObj, *) {
previewPsm := ctrlObj.Value
if (previewPsm > 13) {
previewPsm := 13
ctrlObj.Value := previewPsm
} else if (previewPsm < 1) {
previewPsm := 1
ctrlObj.Value := previewPsm
}
_RefreshGui()
}
}