/
ocroex-chargen
executable file
·203 lines (177 loc) · 6.33 KB
/
ocroex-chargen
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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import sys,os,re,glob,math,glob,signal,sqlite3
from pylab import *
from optparse import OptionParser
from ocrolib.fgen import *
from ocrolib import utils
import ocrolib
class unif:
def __init__(self,lo,hi):
self.lo = lo
self.hi = hi
def __call__(self):
return rand()*(self.hi-self.lo)+self.lo
class bn:
def __init__(self,m,s):
self.m = m
self.s = s
def __call__(self):
while 1:
x = randn()
if x>-2 and x<2: break
return x*self.s+self.m
class c:
def __init__(self,c):
self.c = c
def __call__(self):
return self.c
scale = unif(1.0,4.0)
aspect = unif(0.9,1.1)
margin = unif(0.0,2.0)
noise = unif(0.0,0.1)
maxdelta = unif(0.0,2.0)
rotation = unif(-1.0,1.0) # in degrees
inner = c(1.0)
parser = OptionParser("""
usage: %prog [options] ...
Generates characters from TrueType fonts and stores them in a character database.
""")
parser.add_option("-o","--output",help="output file",default="temp_chargen.db")
parser.add_option("-D","--display",help="display chars",action="store_true")
parser.add_option("-R","--record",help="record parameter info in database",action="store_true")
parser.add_option("-v","--verbose",help="verbose output",action="store_true")
parser.add_option("-c","--classes",help="list of classes (one per line)",default=None)
parser.add_option("-s","--sizes",help="list of sizes (Python expression)",default="range(24,38)")
parser.add_option("-f","--specs",help="list of font specs",default=None)
parser.add_option("-n","--nvariants",help="number of variants",default=8,type=int)
parser.add_option("-Q","--parallel",help="number of parallel processes",default=0,type=int)
parser.add_option("-e","--eval",help="evaluate expressions (for additional options)",default="None")
parser.add_option("-g","--geo",help="estimate line geometry",action="store_false",default=True)
parser.add_option("-i","--inner",help="inner offset for degradation (<1: some broken chars)",default=1,type=float)
(options,args) = parser.parse_args()
exec options.eval
latin_and_ligs = """
! " $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9
: ; < = > ? @
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[ \ ] ^ `
a b c d e f g h i j k l m n o p q r s t u v w x y z
{ | }
ä ö ü Ä Ö Ü ß
00 La Th ac ai ak al all am an ar as be bo ca co ct
di dr ec ed ee es ff ffi fl fr ft gh gi gr gu hi il
in ir is ki li ll ma mi mm ni oc oo pe po re ri rin
rm rn ro r rs rt ru rv ry se so ta te th ti to tr
ts tt tu ul um un ur vi wi wn
a. c. e. m. n. t. z. A. C. E. K. L. M. N. R.
a, c, e, m, n, t, z, A, C, E, K, L, M, N, R,
a- b- e- d- g- m- n- o- p- u-
"B "D "F "H "K "L "P "R "T "W "Z "b "h "l
'B 'D 'F 'H 'K 'L 'P 'R 'T 'W 'Z 'b 'h 'l
d" f" l"
d' f' l'
"""
if options.classes is not None:
testset = []
with open(options.classes,"r") as stream:
for line in stream.readlines():
line = line[:-1]
testset.append(line)
else:
testset = sorted(list(set(latin_and_ligs.split())))
n = len(testset)
sizes = eval(options.sizes)
if options.specs is not None:
specs = []
with open(options.specs,"r") as stream:
for line in stream.readlines():
line = line[:-1]
specs.append(line)
else:
families = ['DejaVu Sans', 'DejaVu Sans Mono', 'DejaVu Serif',
'FreeMono', 'FreeSans', 'FreeSerif', 'cmr10']
styles = [ '', ' italic', ' bold' ]
specs = []
for family in families:
for style in styles:
specs.append(family+style)
print "#specs",len(specs)
print "#variants",options.nvariants
print "sizes",options.sizes
print
# quick run-through to make sure all the fonts exist
for spec in specs:
pango_render_string("hello",spec=spec,size=30,pad=10,markup=0)
if os.path.exists(options.output):
print options.output,"exists; please remove"
sys.exit(1)
db = sqlite3.connect(options.output)
db.row_factory = utils.DbRow
db.text_factory = sqlite3.OptimizedUnicode
utils.charcolumns(db,"chars")
db.execute("create index if not exists cls_index on chars (cls)")
db.commit()
del db
if options.display: ion(); show(); gray()
jobs = []
for spec in specs:
for size in sizes:
inputs = []
jobs.append((spec,size))
def process_arg(arg):
db = sqlite3.connect(options.output)
db.row_factory = utils.DbRow
db.text_factory = sqlite3.OptimizedUnicode
db.execute("pragma synchronous=0")
spec,size = arg
print "loading",spec,size
total = 0
for d in range(options.nvariants):
for i in range(n):
c = testset[i]
scl = scale()
asp = aspect()
rot = rotation()
if options.geo:
c1 = "Hello, good world "+c+" how are you now?"
image = pango_render_string(c1,spec=spec,size=size,pad=10,
markup=0,scale=scl,aspect=asp,rotation=rot)
else:
image = pango_render_string(c,spec=spec,size=size,pad=10,
markup=0,scale=scl,aspect=asp,rotation=rot)
image = average(image,axis=2)
image = array(image,'f')
image /= amax(image)
mrg = margin()
nse = noise()
inn = inner()
image = gauss_degrade(image,margin=mrg,noise=nse,inner=inn)
mdl = maxdelta()
image = gauss_distort([image],maxdelta=mdl)[0]
if options.display:
clf(); imshow(image); draw()
if options.geo:
center = image.shape[1]/2
image = image[:,center-100:center+100]
raw = array(image*255,'B')
if options.record:
css = "s%ds%.2fa%.2fm%.2fn%.2fm%.2fi%.2fr%.2f"%(size,scl,asp,mrg,nse,mdl,inn,rot)
utils.dbinsert(db,"chars",image=utils.image2blob(raw),cls=c,count=1,classes=css)
else:
utils.dbinsert(db,"chars",image=utils.image2blob(raw),cls=c,count=1)
total += 1
if total%1000==0: db.commit()
db.commit()
sys.stdout.flush()
return None
from multiprocessing import Pool
if options.parallel>1:
pool = Pool(processes=options.parallel)
result = pool.map(process_arg,jobs)
else:
for arg in jobs:
process_arg(arg)
# import objgraph
# objgraph.show_most_common_types(limit=50)