-
Notifications
You must be signed in to change notification settings - Fork 1
/
webffoct.py
executable file
·365 lines (313 loc) · 9.98 KB
/
webffoct.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
import cherrypy
import json
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')))
import re
from ffoct.util import loadgrey8, loadgrey16, loadmask, distrib
from ffoct.samples import find_samples
from glcm import glcm, energy, entropy, contrast
from PIL import Image
import numpy
import csv
RE_SIZE = r'([0-9]+)x([0-9]+)mm'
RE_DEPTH = r'([0-9.]+)um'
REGEXES = {
RE_SIZE: ('width_mm', 'height_mm'),
RE_DEPTH: ('depth_um',)
}
THUMBNAIL_SZ = 100
LORES_SZ = 500
LORES_QUAL = 85
SAMPLE_SZ = 200
SAMP_THUMB_SZ = 100
def need_update(output_path, *deps):
if not os.path.exists(output_path) or any(os.path.getmtime(output_path) < os.path.getmtime(path) for path in deps):
try:
os.remove(output_path)
except OSError:
pass
return True
return False
class SampleServer(object):
def __init__(self, imgdir, workdir):
self.imgdir = imgdir
self.workdir = workdir
self.masters = { } # { id: (fname, props) }
self.masks = { } # { id: { label: path } }
self.find_imgs()
def find_imgs(self):
filenames = os.listdir(self.imgdir)
for fname in filenames:
ext = fname.split('.')[-1]
if ext.lower() not in ('tif', 'png', 'jpg'):
continue
noext = fname[:- (len(ext) + 1)]
parts = noext.split('-')
id = parts[0]
if parts[1] == 'MASK':
label = parts[2]
if id not in self.masks:
self.masks[id] = { }
masks = self.masks[id]
masks[label] = fname
else:
props = { }
for part in parts[1:]:
for (regex, keys) in REGEXES.iteritems():
m = re.match(regex + '$', part)
if m:
data = zip(keys, m.groups())
props.update(data)
break
self.masters[id] = (fname, props)
def master_path(self, id):
fname, props = self.masters[id]
master_path = os.path.join(self.imgdir, fname)
return master_path
def _master_thumbnail_path(self, id):
return os.path.join(self.workdir, '%s-thumb.png' % id)
def master_thumbnail(self, id):
thumb_path = self._master_thumbnail_path(id)
master_path = self.master_path(id)
if not os.path.exists(thumb_path) or os.path.getmtime(thumb_path) < os.path.getmtime(master_path):
try:
os.remove(thumb_path)
except OSError:
pass
im = Image.open(master_path)
w, h = im.size
fact = float(max(w, h)) / float(THUMBNAIL_SZ)
w2 = int(w / fact)
h2 = int(h / fact)
im.thumbnail((w2, h2))
im = im.convert(mode = 'I') # 8-bit
im.save(thumb_path)
return thumb_path
def _master_lores_path(self, id):
return os.path.join(self.workdir, '%s-lores.jpg' % id)
def master_lores(self, id):
lores_path = self._master_lores_path(id)
master_path = self.master_path(id)
if not os.path.exists(lores_path) or os.path.getmtime(lores_path) < os.path.getmtime(master_path):
try:
os.remove(lores_path)
except OSError:
pass
m = loadgrey8(master_path)
im = Image.fromarray(m, mode = 'L')
w, h = im.size
fact = float(max(w, h)) / float(LORES_SZ)
w2 = int(w / fact)
h2 = int(h / fact)
im.thumbnail((w2, h2))
im.save(lores_path, quality = LORES_QUAL)
return lores_path
def _sample_thumbnail_path(self, id, x, w, y, h):
return os.path.join(self.workdir, 'sample.%s.%d-%d.%dx%d.thumb.png' % (id, x, y, w, h))
def sample_thumbnail(self, id, x, y, w, h):
thumb_path = self._sample_thumbnail_path(id, x, y, w, h)
master_path = self.master_path(id)
if need_update(thumb_path, master_path):
im = Image.open(master_path)
smp = im.crop((x, y, x + w, y + h))
smp.thumbnail((SAMP_THUMB_SZ, SAMP_THUMB_SZ))
smp = smp.convert(mode = 'I') # 8-bit
smp.save(thumb_path)
return thumb_path
def get_master(self, id):
master_path = self.master_path(id)
master = loadgrey16(master_path)
return master
def get_mask(self, id, label):
mask_path = os.path.join(self.imgdir, self.masks[id][label])
mask = loadmask(mask_path)
return mask
def _samples(self, id):
im = self.get_master(id)
samples = { }
for label in self.masks[id]:
mask = self.get_mask(id, label)
samps = find_samples(im, mask, SAMPLE_SZ, SAMPLE_SZ)
samples[label] = samps
return samples
def samples(self, id):
samples_path = os.path.join(self.workdir, 'samples-%s-%sx%s.json' % (id, SAMPLE_SZ, SAMPLE_SZ))
master_path = self.master_path(id)
if need_update(samples_path, master_path):
data = self._samples(id)
with file(samples_path, 'w') as f:
json.dump(data, f)
with file(samples_path, 'r') as f:
data = json.load(f)
return data
class StatsServer(object):
workdir = property(lambda self: self.samples.workdir)
GLCM_DX = 0
GLCM_DY = 10
def __init__(self, samples):
self.samples = samples
self.statfuncs = {
'glcm-entropy': lambda master, samp: entropy(self.glcm(master, samp, GLCM_DX, GLCM_DY)),
'glcm-energy': lambda master, samp: energy(self.glcm(master, samp, GLCM_DX, GLCM_DY)),
'glcm-contrast': lambda master, samp: contrast(self.glcm(master, samp, GLCM_DX, GLCM_DY)),
}
def filter_samples(self, masters = None, labels = None, filter = None):
if masters is None:
masters = self.samples.masters.keys()
samples = [ (label, master, samp)
for master in masters
for (label, lst) in self.samples.samples(master).items()
if (labels is not None) and label in labels
for sample in lst
if filter and filter(master, sample)
]
return samples
def calc_stats(self, stat, samples):
res = list( (label, master, samp, stat(master, samp))
for (label, master, samp) in samples )
return res
def distrib(self, stats):
X = [ val for (label, master, samp, val) in stats ]
q = numpy.linspace(0, 1, 21)
m, p = distrib(X, q)
return m, p
def _glcm_path(self, master, x, y, w, h, dx, dy):
return os.path.join(self.workdir, 'sample-glcm.%s.%d-%d.%dx%d.(%d_%d).csv' % (master, x, y, w, h, dx, dy))
def get_glcm(self, master, samp, dx, dy):
x, y, h, w = [samp[k] for k in 'xyhw']
glcm_path = self._glcm_path(master, x, y, w, h, dx, dy)
master_path = self.samples.master_path(master)
if need_update(glcm_path, master_path):
m = util.sampflt(master_path, x, y, w, h)
C = glcm(m, dx, dy)
with file(glcm_path, 'w') as f:
csvout = csv.writer(f, lineterminator = '\n')
for row in C:
csvout.writerow(row)
with file(glcm_path, 'r') as f:
csvin = csv.reader(f)
C = [ map(float, row) for row in csvin ]
C = numpy.array(C)
return C
def _stat_path(self, master, stat):
return os.path.join(self.workdir, 'sample-%s.%s.json' % (stat, master))
def get_stat(self, master, stat):
stat_path = self._stat_path(master, stat)
master_path = self.samples.master_path(master)
if need_update(stat_path, master_path):
res = [ ]
with file(stat_path, 'w') as out:
allsamples = self.samples.samples(master)
for (label, samples) in allsamples.iteritems():
statfunc = self.statfuncs[stat]
for samp in samples:
val = statfunc(master, samp)
res.append([label, master, samp, val])
json.dump(res, out)
class WebFFOCT:
def __init__(self, samples):
self.samples = samples
def setup_routes(self, routes):
routes.connect(
name = 'masters',
route = '/masters/',
controller = self,
action = 'get_masters',
conditions = dict(method = ['GET'])
)
routes.connect(
name = 'master_thumbnail',
route = '/masters/{id}/thumbnail',
controller = self,
action = 'get_thumbnail',
conditions = dict(method = ['GET'])
)
routes.connect(
name = 'master_lores',
route = '/masters/{id}/lores',
controller = self,
action = 'get_lores',
conditions = dict(method = ['GET'])
)
routes.connect(
name = 'samples',
route = '/masters/{id}/samples',
controller = self,
action = 'get_samples',
conditions = dict(method = ['GET'])
)
routes.connect(
name = 'sample',
route = '/masters/{id}/sample/thumbnail',
controller = self,
action = 'get_sample_thumbnail',
conditions = dict(method = ['GET'])
)
routes.connect(
name = 'stat',
route = '/stats/{stat}',
controller = self,
action = 'get_stat',
conditions = dict(method = ['GET'])
)
def get_masters(self, **kwargs):
cherrypy.response.headers['Content-type'] = 'application/json'
res = list({'id': id, 'props': props} for (id, (path, props)) in self.samples.masters.iteritems() )
res_json = json.dumps(res)
return res_json
def get_thumbnail(self, id, **kwargs):
cherrypy.response.headers['Content-type'] = 'image/png'
path = self.samples.master_thumbnail(id)
f = file(path, 'r')
return f.read()
def get_lores(self, id, **kwargs):
cherrypy.response.headers['Content-type'] = 'image/jpeg'
path = self.samples.master_lores(id)
f = file(path, 'r')
return f.read()
def get_samples(self, id, **kwargs):
cherrypy.response.headers['Content-type'] = 'application/json'
res = self.samples.samples(id)
res_json = json.dumps(res)
return res_json
def get_sample_thumbnail(self, id, x, y, w, h, **kwargs):
cherrypy.response.headers['Content-type'] = 'image/png'
x, y, w, h = map(int, (x, y, w, h))
path = self.samples.sample_thumbnail(id, x=x, y=y, w=w, h=h)
f = file(path, 'r')
return f.read()
def get_stat(self, stat, **kwargs):
pass
if __name__ == '__main__':
import os, sys
imgdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'img'))
workdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'tmp'))
samplesrv = SampleServer(imgdir, workdir)
api = WebFFOCT(samplesrv)
routes = cherrypy.dispatch.RoutesDispatcher()
routes.mapper.explicit = False
api.setup_routes(routes)
HTDOCS = os.path.abspath(os.path.join(os.path.dirname(__file__), 'www'))
global_config = {
'server.socket_host': '0.0.0.0',
'server.socket_port': 8080,
}
root_config = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir': HTDOCS,
'tools.staticdir.index': 'index.html',
}
}
cherrypy.tree.mount(None, '/', config = root_config)
API_ROOT = '/api'
api_config = {
'/': {
'request.dispatch': routes,
'tools.sessions.on': True
}
}
cherrypy.tree.mount(None, API_ROOT, config = api_config)
cherrypy.engine.start()
cherrypy.engine.block()