-
Notifications
You must be signed in to change notification settings - Fork 588
/
ocroex-pagerec
executable file
·51 lines (41 loc) · 1.29 KB
/
ocroex-pagerec
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
#!/usr/bin/python
import signal
signal.signal(signal.SIGINT,lambda *args:sys.exit(1))
from pylab import *
import sys,os,re,optparse
from scipy.ndimage import measurements
from scipy.misc import imsave
from PIL import Image
import ocrolib
parser = optparse.OptionParser(usage="""
""")
options,args = parser.parse_args()
binarizer = ocrolib.make_IBinarize("StandardPreprocessing")
model = ocrolib.load_component("2m2-reject.cmodel")
ion(); gray()
sizes = {}
for arg in args:
image = ocrolib.read_image_gray(arg)
print image.shape,image.dtype
bin,gray = binarizer.binarize(image)
h,w = bin.shape
bin = amax(bin)-bin
imshow(bin); ginput(1,timeout=1)
labels,n = measurements.label(array(bin,'uint32'))
imshow(labels); ginput(1,timeout=1)
print labels.shape,n
objects = measurements.find_objects(labels)[1:]
for s in objects:
bbox = (s[0].start,s[0].stop,s[1].start,s[1].stop)
char = bin[s]
classifications = model.coutputs(char)
classifications.sort(key=lambda x:-x[1])
for k,v in classifications:
if k=="~":
reject_p = v
break
cost = -log(reject_p)
remaining = classifications[:]
remaining.remove(("~",reject_p))
cls,p = remaining[0]
print bbox,cls,p