forked from NVlabs/ocropus3-ocrorot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch-extract
executable file
·74 lines (63 loc) · 2.3 KB
/
patch-extract
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
#!/usr/bin/python
import argparse
from random import randint
from numpy import random
from pylab import *
from scipy import ndimage as ndi
from dlinputs import gopen, utils
parser = argparse.ArgumentParser("extract patches for training rot/skew models")
parser.add_argument("--display", type=int, default=-1)
parser.add_argument("--npatches", type=int, default=64)
parser.add_argument("--minmean", type=float, default=0.02)
parser.add_argument("--bad", default="")
parser.add_argument("input")
parser.add_argument("output")
args = parser.parse_args()
if args.display > 0:
rc("image", cmap="gray")
ion()
bad = set(args.bad.split())
def get_patch(image, center, scale=1.0, alpha=0.0, shape=(256, 256)):
yx = array(center, 'f')
hw = array(shape, 'f')
m = array([[cos(alpha), -sin(alpha)], [sin(alpha), cos(alpha)]], 'f')/scale
offset = yx - dot(m, hw/2.0)
return ndi.affine_transform(image, m, offset=offset, output_shape=shape, order=1)
def get_patches(image, npatches=64, shape=(256, 256), ntrials=1024, minmean=0.02, ralpha=5.0):
patches = []
h, w = image.shape
for i in range(ntrials):
if len(patches) >= npatches:
break
y, x = randint(0, h-1), randint(0, w-1)
alpha = random.uniform(-ralpha*pi/180, ralpha*pi/180)
scale = random.uniform(0.7, 1.4)
patch = get_patch(image, (y, x), alpha=alpha, scale=scale, shape=shape)
if mean(patch) < minmean:
continue
patches.append((patch, dict(offset=(y, x), alpha=alpha, scale=scale)))
return patches
data = gopen.sharditerator_once(args.input)
sink = gopen.open_sink(args.output)
count = 0
for sample in data:
if sample["__key__"] in bad:
continue
utils.print_sample(sample)
page = sample.get("framed.png")
if page is None: page = sample["png"]
patches = get_patches(page, args.npatches, minmean=args.minmean)
for i, (patch, params) in enumerate(patches[:args.npatches]):
if args.display > 0 and count % args.display == 0:
clf()
imshow(patch)
ginput(1, 0.001)
result = {
"__key__": "{}-{}".format(sample["__key__"], i),
"patch.png": patch,
"params.json": params
}
sink.write(result)
count += 1
sink.close()
print "wrote", count, "records"