-
Notifications
You must be signed in to change notification settings - Fork 0
/
training-packer.py
88 lines (80 loc) · 3.52 KB
/
training-packer.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
#!/usr/bin/env python3
import os, sys
import re
folder = sys.argv[1].replace('"','').replace("'","").rstrip('/') + '/'
if len(sys.argv[2:]):
args = sys.argv[2:]
else:
args = False
fullfilelist = [files for root, dirs, files in os.walk(folder)][0]
filelist = [i for i in fullfilelist if not i.startswith('.')]
filelist = [l for l in filelist if not re.search(r'font_properties|traineddata|shapetable|unicharset \
|pffmtable|normproto|inttemp|unicharambigs|\.tr$', l)]
dawglist = [j for j in filelist if re.search('\.txt', j)]
filelist = sorted([k for k in filelist if not k in dawglist])
#Builder for the .tr files
def buildtr(pair):
for p in pair:
try:
command = "tesseract " + p[1] + " " + p[1].replace('.tif', '') + " nobatch box.train"
#print(command + '\n')
os.system(command)
except:
print("Processing failed on building a set of .tr files for ", p[1])
#Builder for unicharset and performs shapetable, mftraining, cntraining
def buildfiles(flist):
flist = [i.replace('.tif', '').replace('.box', '') for i in flist]
flist = list(set(flist))
commands = []
commands.append("unicharset_extractor " + ' '.join([l+'.box' for l in flist]))
commands.append("shapeclustering -F font_properties -U unicharset " + ' '.join([l+'.tr' for l in flist]))
commands.append("mftraining -F font_properties -U unicharset -O " + flist[0].split('.')[0] + ".unicharset " +
' '.join([l+'.tr' for l in flist]))
commands.append("cntraining " + ' '.join([l+'.tr' for l in flist]))
for cmd in commands:
try:
#print(cmd + '\n')
os.system(cmd)
except:
print("Processing failed on ", cmd.split()[0])
#Dawg file builder; -kind- can be "word-dawg" or "freq-dawg"
def builddawg(dfile, kind, prepend):
command = "wordlist2dawg " + dfile + " " + prepend + "." + kind + " " + prepend + ".unicharset"
#print(command)
os.system(command)
#Builder for fontproperties file
def fontproperties(flist):
flist = [i.replace('.tif', '').replace('.box', '').split('.')[1] for i in flist]
flist = list(set(flist))
fprop = ""
for font in flist:
fprop = fprop + font.split('.')[0]
if font[-1] == "i":
fprop = fprop + " 1 0 0 1 0\n"
else:
fprop = fprop + " 0 0 0 1 0\n"
with open(folder + "font_properties", 'w', encoding='utf-8') as outfile:
outfile.write(fprop)
outfile.close()
print("font_properties files created)")
def main():
if len(filelist) % 2 != 0:
print("File group error. Incomplete box/tif pair")
else:
os.chdir(os.path.dirname(folder))
langprepend = filelist[0].split('.')[0]
pairlist = [tuple(filelist[i:i+2]) for i in range(0, len(filelist), 2)]
buildtr(pairlist)
fontproperties(filelist)
buildfiles(filelist)
if args:
for arg in args:
builddawg(arg, "word-dawg", langprepend)
if langprepend + ".inttemp" not in fullfilelist:
list(map(os.system, [n + langprepend + "." + n.split()[1] for n in ["mv inttemp ", "mv normproto ", "mv pffmtable ", "mv shapetable "]]))
else:
list(map(os.system, [n + langprepend + "." + n.split()[1] + " -f" for n in ["mv inttemp ", "mv normproto ", "mv pffmtable ", "mv shapetable "]]))
os.system("combine_tessdata " + langprepend + ".")
#print("combine_tessdata " + langprepend + ".")
print("Training data packed!")
main()