forked from buliasz/tesstrain-windows-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_helper.ahki
436 lines (369 loc) · 10.7 KB
/
_helper.ahki
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
; (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.
#include _console.ahki
#include _quick_sort.ahki
; ----------------
; STRING FUNCTIONS
; ----------------
StrCutEnd(text, numberOfCharacters) {
return SubStr(text, 1, -numberOfCharacters)
}
StrRCutTo(text, cutStartStr) {
cutIndex := InStr(text, cutStartStr,,, -1)
if (!cutIndex) {
return text
}
return SubStr(text, cutIndex + StrLen(cutStartStr))
}
StrRCutFrom(text, cutStartStr) {
cutIndex := InStr(text, cutStartStr,,, -1)
if (!cutIndex) {
return text
}
return SubStr(text, 1, cutIndex - 1)
}
StrEndsWith(text, ending) {
return SubStr(text, -StrLen(ending)) == ending
}
; ---------------
; ARRAY FUNCTIONS
; ---------------
ArrayContains(_array, itemToFind) {
for (item in _array) {
if (item == itemToFind) {
return true
}
}
return false
}
ArrayTransform(arr, method, argument) {
ret := []
for (element in arr) {
ret.Push(method(element, argument))
}
return ret
}
ArrayForEach(arr, Callback) {
for (item in arr) {
Callback(item)
}
}
ArrayToString(arr, joinWith:=" ") {
ret := ""
for element in arr {
if (A_Index > 1) {
ret .= joinWith element
} else {
ret .= element
}
}
return ret
}
ArrayHead(arr, headLength) {
headArray := []
remaining := headLength
for (item in arr) {
if (remaining <= 0) {
break
}
headArray.Push(item)
remaining -= 1
}
return headArray
}
ArrayTail(arr, tailLength) {
tailArray := []
if (arr.Length <= tailLength) {
return arr
}
skip := arr.Length - tailLength
for (item in arr) {
if (skip <= 0) {
tailArray.Push(item)
} else {
skip -= 1
}
}
return tailArray
}
ArrayPushAll(source, target) {
for (item in source) {
target.Push(item)
}
}
ArraySort(arr, func:="") {
if (arr.Length == 0) {
return []
}
if (!func && !IsNumber(arr[1])) {
func := StrCompare
}
return QuickSort(arr, func)
}
;----------------------
; MAP FUNCTIONS
;----------------------
MapSafeDelete(_map, _key) {
if (_map.Has(_key)) {
_map.Delete(_key)
}
}
;----------------------
; FILE SYSTEM FUNCTIONS
;----------------------
FileSave(fileName, content) {
myFile := FileOpen(fileName, "w")
myFile.Write(content)
myFile.Close()
}
FindAllFiles(pattern) {
filesFound := []
loop Files, pattern {
filesFound.Push(A_LoopFileFullPath)
}
return filesFound
}
FindAllFilesExtended(pattern) {
filesFound := []
loop Files, pattern {
filesFound.Push({path: A_LoopFileFullPath, modified: A_LoopFileTimeModified})
}
return filesFound
}
FindNewestFile(pattern) {
fileFound := ""
modified := ""
loop Files, pattern {
if (StrCompare(A_LoopFileTimeModified, modified) > 0) {
fileFound := A_LoopFileFullPath
modified := A_LoopFileTimeModified
}
}
return fileFound
}
FileGetFirstLine(filePath) {
return GetFileLine(filePath, 1)
}
GetFileLine(filePath, lineNumber) {
savedLine := ""
loop read, filePath {
if (A_Index == lineNumber) {
savedLine := A_LoopReadLine ; When loop finishes, this will hold the last line.
break
}
} else {
throw Error("File '" filePath "' not found")
}
return savedLine
}
; Removes path leaving only file name with extension
; Alternatively 'SplitPath' AHK command may be used
FileGetName(filePathWithName) {
return StrRCutTo(filePathWithName, "\")
}
IsFileOlder(file1, file2) {
return StrCompare(FileGetTime(file1), FileGetTime(file2)) < 0
}
IsFileNewer(file1, file2) {
return StrCompare(FileGetTime(file1), FileGetTime(file2)) > 0
}
; ----------------------
; OTHER HELPER FUNCTIONS
; ----------------------
GetNonEmptyLines(filePath) {
lines := []
loop read, filePath {
line := Trim(A_LoopReadLine)
if (line != "") {
lines.Push(line)
}
}
return lines
}
CmdLogAppend(text) {
FileAppend text, "command.log"
}
OcrImageFile(imageFullPath, lang:="", tessdataDir:="", pis:=0, local_psm:="") {
if (!tessdataDir) {
tessdataDir := TESSDATA
}
outputFile := DATA_DIR "\preview.out"
ocrOutput := ExecuteCommand("`"" BINARIES["tesseract"] "`" `"" imageFullPath "`" -"
. (lang ? " -l " lang : "")
. " --psm " (local_psm || PSM) " -c preserve_interword_spaces=" pis " -c page_separator= --tessdata-dir `"" tessdataDir "`" >`"" outputFile "`"", 2)
return Trim(FileRead(outputFile), "`t`n`r ")
}
ProgressStatusGui(newStatus:="", parentGui:="", windowTitle:="Training progress") {
static lastStatus:="", statusGui:="", savedParentGui:="", bcer:={}, generateBtn:={}
global SHUTDOWN_AFTER_TRAINING_COMPLETION, StatusUpdate
; Remove StatusGui if empty arguments and resets to default status function
if (!newStatus && statusGui) {
MonitorCheckpoints(false)
if (savedParentGui) {
savedParentGui.Show()
}
bcer := generateBtn := {} ; prevents error if there is an ongoing CheckCheckpoints timer
statusGui.Destroy()
lastStatus := statusGui := savedParentGui := ""
StatusUpdate := DEFAULT_STATUS_FUNCTION
return
}
if (!statusGui) {
statusGui := Gui("-Resize +AlwaysOnTop -SysMenu", windowTitle)
if (parentGui) {
parentGui.Hide()
savedParentGui := parentGui
}
statusGui.Add("Text", "section xm w115", "Best checkpoint BCER")
bcer := statusGui.Add("Text", "ys w45", "-")
generateBtn := statusGui.Add("Button", "ys w240", "&Generate model from currently best checkpoint")
generateBtn.OnEvent("Click", GenerateTraineddata)
generateBtn.Enabled := false
MonitorCheckpoints(true)
shutdownChb := statusGui.Add("Checkbox", "xs hp 0x20 Checked" SHUTDOWN_AFTER_TRAINING_COMPLETION, "Shutdown computer after successfully completed (automatically updates TessData)")
shutdownChb.OnEvent("Click", (ctrlObj,*)=>SHUTDOWN_AFTER_TRAINING_COMPLETION:=ctrlObj.Value)
}
if (lastStatus) {
lastStatus.Text := "Done"
}
statusGui.Add("Text", "section xm w360", newStatus)
lastStatus := statusGui.Add("Text", "ys w40", "...")
statusGui.Show("AutoSize")
return
MonitorCheckpoints(isEnable) {
if (isEnable) {
SetTimer CheckCheckpoints, 2000
} else {
SetTimer CheckCheckpoints, 0
}
}
CheckCheckpoints() {
if ((name:=FindNewestFile(OUTPUT_DIR "\checkpoints\" MODEL_NAME "_*.checkpoint")) && statusGui) {
bcer.Text := GetBcerFromName(name)
generateBtn.Enabled := true
}
}
GenerateTraineddata(*) {
checkpointFile := FindNewestFile(OUTPUT_DIR "\checkpoints\" MODEL_NAME "_*.checkpoint")
Checkpoint2Traineddata(checkpointFile, DATA_DIR "\" MODEL_NAME ".traineddata", false)
if (UpdateModelFileInTessdata()) {
MsgBox("New model successfully generated and/or updated", PROGRAM_TITLE)
}
}
}
GetBcerFromName(name) {
bcer := name
bcer := StrRCutFrom(bcer, "_")
bcer := StrRCutFrom(bcer, "_")
bcer := StrRCutTo(bcer, "_")
return bcer
}
VerifyRequirements() {
ProgressStatusGui("Starting up",, PROGRAM_TITLE)
VerifyPythonDependencies()
ProgressStatusGui()
return true
}
VerifyPythonDependencies() {
ProgressStatusGui("Verifying installed Python version")
try {
version := ExecuteCommand("python --version")
PYTHON_EXE := "python"
} catch Error as ePython {
try {
version := ExecuteCommand("python3 --version")
PYTHON_EXE := "python3"
} catch Error as ePython3 {
ErrorBox("Executing 'python' command returned error: " ePython.Message "`n`n"
. "Executing 'python3' command returned error: " ePython3.Message "`n`n"
. "Please make sure that you have a Python 3.x installed and that "
. "'python.exe' or 'python3.exe' executable file directory is in your PATH environment variable.")
ExitApp()
}
}
versionStr := StrRCutTo(Trim(version.StdOut, "`t`n`r "), " ")
versionArray := StrSplit(versionStr, ".")
if (versionArray.Length < 1 || !IsInteger(versionArray[1]) || versionArray[1] < 3) {
ErrorBox("Wrong Python version. Returned version: '" versionStr "'. Please install Python version 3 or above.")
ExitApp()
}
ProgressStatusGui("Verifying/installing required Python modules")
try {
ExecuteCommand(PYTHON_EXE " -m pip install Pillow>=6.2.1 python-bidi>=0.4 matplotlib pandas")
} catch Error as e {
if (YesNoConfirmation("Could not install required Python modules. Probably you don't have required privilages.`n"
. "Do you want me to try again as Administrator?")) {
ExecuteCommand(PYTHON_EXE " -m pip install Pillow>=6.2.1 python-bidi>=0.4 matplotlib pandas",, true)
ExecuteCommand()
} else {
ErrorBox("Error installing required Python modules.`n" e.Message)
ExitApp()
}
}
}
AotBox(message) {
MsgBox(message, PROGRAM_TITLE, 0x40000)
}
YesNoConfirmation(message) {
return MsgBox(message, PROGRAM_TITLE, "YesNo Icon? 0x40000") == "Yes"
}
ErrorBox(message) {
return MsgBox(message, PROGRAM_TITLE, "Icon! 0x40000")
}
NotAllowedBox(message) {
return MsgBox(message, PROGRAM_TITLE, "IconX 0x40000")
}
OnError MyErrorFunction
MyErrorFunction(_exception, _mode) {
ErrorBox(DescribeException(_exception) "`nMode: " _mode)
ExitApp ; It's an unhandled exception. We want to Shutdown the app.
}
DescribeException(e) {
extra := e.HasProp("Extra") ? e.Extra : ""
return "Exception: " e.Message " in " e.What " at " e.File ":" e.Line
. (extra ? "`nAdditional information (e.Extra):`n" extra : "") "`n`n"
. CallStack(2)
CallStack(startOffset:=0, maxLevels:="") {
if (A_IsCompiled) {
return
}
ret := ""
indent := "`t"
loop {
if (maxLevels && A_Index > maxLevels) {
break
}
offset := -(A_Index + startOffset)
e := Error(".", offset)
if (e.What == offset) {
break
}
fileName := ""
SplitPath e.file, &fileName
ret .= "[" (offset + startOffset) "]" fileName "(" e.Line "):`n"
. indent Trim(GetFileLine(e.file, e.line)) "`n"
ret .= "`t=> " e.What "`n"
}
return ret
}
}
DisableSystemStandby(shouldDisable) {
static oldState:=0
newState := shouldDisable ? 0x80000001 : 0x80000000
if (newState != oldState) {
DllCall("SetThreadExecutionState", "UInt", newState)
oldState := newState
}
}
TemporaryTooltip(message, seconds) {
ToolTip(message)
SetTimer(Tooltip, -1000 * seconds)
}