-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·206 lines (176 loc) · 7.29 KB
/
app.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
"""
Demonstration: Ant Colony Optimization for Estimating Pith Position on Images of Tree Log Ends
demo editor: Phuc Ngo
"""
from lib import base_app, build, http, image, config
from lib.misc import app_expose, ctime
from lib.base_app import init_app
import cherrypy
from cherrypy import TimeoutError
import os.path
import shutil
import time
class app(base_app):
""" template demo app """
title = "Ant Colony Optimization for Estimating Pith Position on Images of Tree Log Ends: Online Demonstration"
xlink_article = 'https://www.ipol.im/'
xlink_src = 'https://www.ipol.im/pub/pre/67/gjknd_1.1.tgz'
demo_src_filename = 'gjknd_1.1.tgz'
demo_src_dir = 'ACO_PithDetection'
input_nb = 1 # number of input images
input_max_pixels = 4500 * 4500 # max size (in pixels) of an input image
input_max_weight = 1 * 4500 * 4500 # max size (in bytes) of an input file
input_dtype = '3x8i' # input image expected data type
input_ext = '.png' # input image expected extension (ie file format)
is_test = False # switch to False for deployment
commands = []
list_commands = ""
def __init__(self):
"""
app setup
"""
# setup the parent class
base_dir = os.path.dirname(os.path.abspath(__file__))
base_app.__init__(self, base_dir)
# select the base_app steps to expose
# index() is generic
app_expose(base_app.index)
app_expose(base_app.input_select)
app_expose(base_app.input_upload)
# params() is modified from the template
app_expose(base_app.params)
# run() and result() must be defined here
def build(self):
"""
program build/update
"""
return
@cherrypy.expose
@init_app
def wait(self, **kwargs):
"""
params handling and run redirection
"""
# save and validate the parameters
try:
self.cfg['param']['scale'] = kwargs['scale']
self.cfg['param']['dx'] = kwargs['dx']
self.cfg['param']['dy'] = kwargs['dy']
self.cfg['param']['gsigma'] = kwargs['gsigma']
self.cfg['param']['bsigma'] = kwargs['bsigma']
self.cfg['param']['lsigma'] = kwargs['lsigma']
self.cfg['param']['ant'] = kwargs['ant']
self.cfg['param']['block'] = kwargs['block']
self.cfg['param']['omega'] = kwargs['omega']
self.cfg['param']['quantizer'] = kwargs['quantizer']
self.cfg['param']['alpha'] = kwargs['alpha']
self.cfg['param']['beta'] = kwargs['beta']
self.cfg['param']['gamma'] = kwargs['gamma']
self.cfg['param']['lambda'] = kwargs['lambda']
self.cfg['param']['iter'] = kwargs['iter']
self.cfg['param']['kappa'] = kwargs['kappa']
self.cfg['param']['epsilon'] = kwargs['epsilon']
self.cfg.save()
except ValueError:
return self.error(errcode='badparams',
errmsg="The parameters must be numeric.")
http.refresh(self.base_url + 'run?key=%s' % self.key)
return self.tmpl_out("wait.html")
@cherrypy.expose
@init_app
def run(self):
"""
algo execution
"""
self.list_commands = ""
try:
self.run_algo(self)
except TimeoutError:
return self.error(errcode='timeout')
except RuntimeError:
return self.error(errcode='runtime',
errmsg="Something went wrong with the program.")
except ValueError:
return self.error(errcode='badparams',
errmsg="The parameters given produce no contours,\
please change them.")
http.redir_303(self.base_url + 'result?key=%s' % self.key)
# archive
if self.cfg['meta']['original']:
ar = self.make_archive()
ar.add_file("input_0.png", "input_0.png", info="Input image")
ar.add_file("algoLog.txt", info="algoLog.txt")
ar.add_file("commands.txt", info="commands.txt")
ar.add_file("Result.png", "Result.png", info="Pith detection result")
#ar.add_info({"version": self.cfg['param']["version"]})
ar.save()
return self.tmpl_out("run.html")
def run_algo(self, params):
"""
the core algo runner
could also be called by a batch processor
this one needs no parameter
"""
## -------
## Apply algorithm
## ---------
inputWidth = 512
inputHeight = 512
command_args = ['AntColonyPith'] + \
['input_0.png']+ \
['-s', str(self.cfg['param']['scale'])] + \
['-x', str(self.cfg['param']['dx'])]+ \
['-y', str(self.cfg['param']['dy'])]+ \
['-u', str(self.cfg['param']['gsigma'])]+ \
['-v', str(self.cfg['param']['bsigma'])]+ \
['-w', str(self.cfg['param']['lsigma'])]+ \
['-n', str(self.cfg['param']['ant'])]+ \
['-c', str(self.cfg['param']['block'])]+ \
['-o', str(self.cfg['param']['omega'])]+ \
['-q', str(self.cfg['param']['quantizer'])]+ \
['-a', str(self.cfg['param']['alpha'])]+ \
['-b', str(self.cfg['param']['beta'])]+ \
['-g', str(self.cfg['param']['gamma'])]+ \
['-l', str(self.cfg['param']['lambda'])]+ \
['-i', str(self.cfg['param']['iter'])] + \
['-k', str(self.cfg['param']['kappa'])] + \
['-e', str(self.cfg['param']['epsilon'])]
f = open(self.work_dir+"algoLog.txt", "w")
cmd = self.runCommand(command_args, None, f)
f.close()
## ----
## Save command line
## ----
f = open(self.work_dir+"commands.txt", "w")
f.write(self.list_commands)
f.close()
return
@cherrypy.expose
@init_app
def result(self, public=None):
"""
display the algo results
"""
resultHeight = image(self.work_dir + 'Result.png').size[1]
imageHeightResized = min (600, resultHeight)
resultHeight = max(300, resultHeight)
return self.tmpl_out("result.html", height=resultHeight, \
heightImageDisplay=imageHeightResized)
def runCommand(self, command, stdOut=None, stdErr=None, comp=None):
"""
Run command and update the attribute list_commands
"""
p = self.run_proc(command, stderr=stdErr, stdout=stdOut, \
env={'LD_LIBRARY_PATH' : self.bin_dir})
self.wait_proc(p, timeout=500)
#index = 0
# transform convert.sh in it classic prog command (equivalent)
#for arg in command:
# if arg == "convert.sh" :
# command[index] = "convert"
# index = index + 1
command_to_save = ' '.join(['"' + arg + '"' if ' ' in arg else arg for arg in command ])
if comp is not None:
command_to_save += comp
self.list_commands += command_to_save + '\n'
return command_to_save