This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
show_ocrdir.py
191 lines (174 loc) · 5.79 KB
/
show_ocrdir.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
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
# just run this with
import cherrypy
import glob,os,sys,string,re,math,random
import numpy,scipy,scipy.ndimage
import cStringIO
from PIL import Image
from imageutil import *
from ocrutil import *
_html = ""
def clear():
global _html
_html = ""
def html():
global _html
return _html
def out(*args):
global _html
for arg in args:
_html += arg
_html += "\n"
def fclean(file):
file = re.sub(r'/[.][.]*','/',file)
file = re.sub(r'[.][.]*/','/',file)
file = re.sub(r'[^/_0-9a-zA-Z.-]','',file)
file = re.sub(r'^/*','',file)
return file
def read_file(file):
stream = open(file,"r")
data = stream.read()
stream.close()
return data
class Display:
# TODO replace this with CherryPy's static file server
@cherrypy.expose
def send(self,file=None,type="image/jpeg"):
file = fclean(file)
stream = open(file,"r")
data = stream.read()
stream.close()
cherrypy.response.headers['Content-Type'] = type
return data
@cherrypy.expose
def recolor(self,file=None):
file = fclean(file)
image = read_rgb32(file)
image = numpy.array(image,'uint8')
image = numpy2pil(image,mode='L')
palette = []
palette.extend((0,0,0))
for i in range(1,255):
r = int(abs(math.sin(i))*220+30)
g = int(abs(math.sin(i*1.3))*220+30)
b = int(abs(math.sin(i/1.7))*220+30)
palette.extend((r,g,b))
palette.extend((0,0,0))
assert len(palette)==768
image.putpalette(palette)
cherrypy.response.headers['Content-Type'] = "image/png"
return pil2string(image,"png")
@cherrypy.expose
def extract(self,file=None,index=None):
file = fclean(file)
index = int(index)
image = read_pil(file)
subimage = extract_cseg(image,index)
if subimage!=None:
subimage = numpy2pil(subimage)
else:
subimage = Image.new("RGB",(11,11),(255,0,0))
cherrypy.response.headers['Content-Type'] = "image/png"
return pil2string(subimage,"png")
@cherrypy.expose
def index(self):
clear()
out("<h1>Index</h1>")
volumes = glob.glob("Volume*")
volumes.sort()
for vol in volumes:
out("<h2>%s</h2>"%vol)
pages = glob.glob("%s/????"%vol)
pages.sort()
for page in pages:
match = re.search(r'/(\d+)$',page)
pageno = int(match.group(1))
out("<a href='dir?dir=%s'>%d</a> "%(page,pageno))
return html()
@cherrypy.expose
def dir(self,dir=None):
dir = fclean(dir)
clear()
out("<h1>Text Lines [%s]</h1>"%dir)
for file in glob.glob("%s/[0-9][0-9][0-9][0-9].png"%dir):
base = re.sub(r'.png','',file)
# out("<img src='send?file=%s.jpg'><br>"%base)
out("<a href='line?file=%s.png'>"%base)
out("<img src='recolor?file=%s.cseg.png'><br>"%base)
out("</a>")
out("<p>")
return html()
@cherrypy.expose
def line(self,file=None):
file = fclean(file)
clear()
base = re.sub(r'.png','',file)
transcription = open("%s.txt"%base).read()
transcription = re.sub(r'[ ]','',transcription)
n = numpy.amax(white_to_black(read_pil("%s.cseg.png"%base)))
l = list(range(15,26))
out("<h2>%s</h2>"%file)
out("%d components<p>"%n)
out("<table border=1>")
if os.path.exists(base+".jpg"):
out("<img src='send?file=%s.jpg'><p>"%base)
if os.path.exists(base+".png"):
out("<img src='send?file=%s'><p>"%file)
if os.path.exists(base+".rseg.png"):
out("<img src='recolor?file=%s.rseg.png'><p>"%base)
if os.path.exists(base+".cseg.png"):
out("<img src='recolor?file=%s.cseg.png'><p>"%base)
if os.path.exists(base+".cut_dgb.png"):
out("<img src='send?file=%s.png.cut_dbg.png'><p>"%base)
out("<tr>")
for i in range(min(n,200)):
out("<td>")
out("<img src='extract?file=%s.cseg.png&index=%d'>"%(base,i))
out("</td>")
out("</tr>")
out("<tr>")
for i in range(min(n,200)):
out("<td>")
try:
out("<font size=6>%s</font>"%transcription[i])
except:
out("<font size=6>ERR</font>")
out("</td>")
out("</tr>")
out("<tr>")
if os.path.exists(base+".costs"):
stream = open(base+".costs")
for line in stream.readlines():
key,index,cost = line.split()
cost = float(cost)
out("<td>%s</td>"%("|"*int(math.floor(cost))))
out("</tr>")
out("<tr>")
if os.path.exists(base+".costs"):
stream = open(base+".costs")
for line in stream.readlines():
key,index,cost = line.split()
cost = float(cost)
out("<td>%.2f</td>"%cost)
out("</tr>")
out("</table>")
if os.path.exists(base+".align.log"):
out("<pre>")
out(read_file(base+".align.log"))
out("</pre>")
return html()
config = {
"server.socket_port": 9999,
"server.thread_pool": 10,
"server.environment": "development",
"server.showTracebacks": True,
}
# cherrypy.config.update(config)
if __name__ == "__main__":
root = "/home/tmb/data/g1000w"
if len(sys.argv)>1: root = sys.argv[1]
sys.argv[0] = os.path.abspath(sys.argv[0])
cherrypy.engine.reload_files += [ os.path.abspath(sys.argv[0]) ]
cherrypy.config["server.socket_port"] = 9999
cherrypy.config["server.showTracebacks"] = True
os.chdir(root)
cherrypy.quickstart(Display())