-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
387 lines (332 loc) · 12.8 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import flask
import dash
from dash.exceptions import PreventUpdate
from dash.dependencies import Input, Output, State, MATCH, ALL
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import os
import sys
import matplotlib.pyplot as plt
import uuid
import time
import shutil
from gevent.pywsgi import WSGIServer
from utils import *
from inpaint import Inpainter
from layout import *
PREVIEW_HEIGHT = '500px'
TEST_DIR = 'test_images'
CANVAS_WIDTH = 500
CANVAS_HEIGHT = 800
MAX_DIMENSION = 200
# Initialize app
server = flask.Flask(__name__) # define flask app.server
app = dash.Dash(__name__, title='Inpainter', eager_loading=True, server=server)
# Initialize Redis Queue
# from rq import Queue
# from rq.job import Job
# from worker import conn
# q = Queue(connection=conn)
# Modify app layout to include bootstrap and custom css
app.index_string = '''
<!DOCTYPE html>
<html>
<head>
{%metas%}
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>{%title%}</title>
{%favicon%}
<link rel="stylesheet" href="https://codepen.io/chriddyp/pen/bWLwgP.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" rel="stylesheet">
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
'''
# Define the app and main layouts
main_layout = html.Div([
getUploadLayout(app.get_asset_url(TEST_DIR))
],
id = 'main-div',
className = 'main')
app.layout = html.Div([
dcc.Store(id='session', storage_type='session'),
navbar_layout,
header_layout,
main_layout,
footer_layout
])
# Navbar callback for toggling
@app.callback(
Output("navbar-collapse", "is_open"),
[Input("navbar-toggler", "n_clicks")],
[State("navbar-collapse", "is_open")],
)
def toggle_navbar_collapse(n, is_open):
"""
Callback method to toggle the navbar
Parameters :
n: number of clicks on the navbar toggler
is_open: state of the navbar, whether open or closed
"""
if n:
return not is_open
return is_open
# Upload callbacks
## Sample images selector callback
@app.callback(
Output('sample-image-chosen', 'children'),
Input({'type': 'sample-img', 'source': ALL}, 'n_clicks'),
prevent_initial_call=True
)
def set_chosen_img(n_clicks) :
"""
Callback method that stores the selected sample image in the
'sample-image-chosen' element for use later in preview and upload
Parameters :
n_clicks: clicks on any of the 'sample-img' elements (note the ALL)
"""
change_id = dash.callback_context.triggered[0]['prop_id'].split('.')[:-1]
json_source = ".".join(dash.callback_context.triggered[0]['prop_id'].split('.')[:-1])
if change_id:
return [json.loads(json_source)["source"], time.time()]
else :
return ["", time.time()]
## Upload time callback
@app.callback(
Output('upload-time-div', 'children'),
Input('upload-image', 'contents'),
prevent_initial_call=True
)
def set_upload_time(contents) :
"""
Callback method that stores the time of the last upload
Parameters :
content: contents of the upload element
"""
return time.time()
## Upload logic callback
@app.callback(
[
Output('upload-preview', 'children'),
Output('upload-image', 'style'),
Output('main-div', 'children'),
Output('upload-text', 'children'),
Output('upload-alert-div', 'children'),
Output('session', 'data')
],
[
Input('inpaint-button', 'n_clicks'),
Input('upload-image', 'contents'),
Input('sample-image-chosen', 'children'),
Input('upload-time-div', 'children')
],
[
State('upload-image', 'filename')
],
prevent_initial_call=True
)
def update_output(inpaint_clicks, image_content, sample_img_data, date_upload, name):
"""
Callback method that handles the update of the preview, whether from the
sample images or the upload element, and also handle the inpaint button
logic
Parameters :
inpaint_clicks: clicks on the inpaint button
image_content: contents of the upload element
sample_img_data: last selected sample image and its source path
date_upload: the date of the last image upload by user
name: name of the uploaded image file
"""
# Get the element(s) that fired the callback
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
# Initialize variables depending on whether a sample image was chosen or not
if sample_img_data :
source, date_sample = sample_img_data[0], sample_img_data[1]
else :
source, date_sample = None, None
# If the trigger didn't come from the "inpaint" button
if 'inpaint-button' not in changed_id :
if 'upload-image.contents' in changed_id :
# Get image contents from upload and show it in preview
return parseContents(image_content, name), {'height': '100px'}, dash.no_update, 'Change Image', dash.no_update, dash.no_update
elif 'sample-image-chosen.children' in changed_id :
# Show the selected sample image in preview
return parseContentsDir(source, name), {'height': '100px'}, dash.no_update, 'Change Image', dash.no_update, dash.no_update
else :
raise PreventUpdate
# Handle the logic of the "inpaint" button
else :
if image_content is not None or source is not None:
# If there is at least a sample image selected or an uploaded image
# Get the latest of the sample image selected and the uploaded image
# and save it, reduce its size, then generate the inpaint layout using it
image_ID = str(uuid.uuid1())
image_filename = 'source/' + image_ID + '.png'
if date_upload is not None and (date_sample is None or date_upload > date_sample) :
# If the uploaded image is the latest
saveImage(image_content, app.get_asset_url(image_filename))
reduceImageSize(app.get_asset_url(image_filename)[1:], MAX_DIMENSION)
inpaint_layout = getInpaintLayout(image_content, app.get_asset_url(image_filename), CANVAS_WIDTH, CANVAS_HEIGHT)
else :
# If the sample image was selected last
shutil.copy(source, app.get_asset_url(image_filename)[1:])
reduceImageSize(app.get_asset_url(image_filename)[1:], MAX_DIMENSION)
inpaint_layout = getInpaintLayout(None, app.get_asset_url(image_filename), CANVAS_WIDTH, CANVAS_HEIGHT)
return dash.no_update, dash.no_update, inpaint_layout, dash.no_update, dash.no_update, {'image_ID': image_ID}
else :
# If no image was selected or uploaded
alert = dbc.Alert("Please upload or choose image!", color="danger", duration=2000)
return dash.no_update, dash.no_update, dash.no_update, dash.no_update, alert, dash.no_update
# Callbacks for inpainting parameters
@app.callback(
Output('annot-canvas', 'lineWidth'),
[Input('brush-width-slider', 'value')],
prevent_initial_call=True
)
def update_canvas_linewidth(value):
return value
@app.callback(
Output('local-radius-output', 'children'),
[Input('local-radius-slider', 'value')],
prevent_initial_call=True
)
def update_output(value):
return '{}px'.format(value)
@app.callback(
Output('data-term-output', 'children'),
[Input('data-term-slider', 'value')],
prevent_initial_call=True
)
def update_output(value):
return value
@app.callback(
Output('center-similarity-output', 'children'),
[Input('center-similarity-slider', 'value')],
prevent_initial_call=True
)
def update_output(value):
return value
# Main callback for inpainting
@app.callback(
[
Output('inpaint-alert-div', 'children'),
Output('result-div', 'children')
],
[
Input('annot-canvas', 'json_data'),
Input('session', 'modified_timestamp'),
Input('upload-mask', 'contents'),
Input('patch-size-slider', 'value'),
Input('local-radius-slider', 'value'),
Input('data-term-slider', 'value'),
Input('center-similarity-slider', 'value'),
Input('fill-rect', 'value'),
Input('use-data-term', 'value'),
Input('use-center-threshold', 'value'),
Input('live-preview', 'value')
],
State('session', 'data'),
prevent_initial_call=True)
def inpaint_image(string, ts, mask_contents, patch_size, local_radius, data_significance, \
threshold, rect_fill, use_data, use_threshold, show_live_preview, session_data):
"""
Callback method that handles the update of the preview, whether from the
sample images or the upload element, and also handle the inpaint button
logic
Parameters :
inpaint_clicks: clicks on the inpaint button
image_content: contents of the upload element
sample_img_data: last selected sample image and its source path
date_upload: the date of the last image upload by user
name: name of the uploaded image file
"""
# Get data that holds canvas objects, including masks on the image
if string:
data = json.loads(string)
else:
raise PreventUpdate
# Load image to run the algorithm on
image_ID = session_data.get('image_ID', '')
image_filename = 'source/' + image_ID + '.png'
image = np.array(Image.open(os.getcwd() + app.get_asset_url(image_filename)))
image_width = image.shape[1]
image_height = image.shape[0]
# Parse the mask from Canvas and save it to file
mask_filename = 'masks/' + image_ID + '.png'
print(mask_filename)
if mask_contents is not None :
saveImage(mask_contents, app.get_asset_url(mask_filename))
else :
maskFromData(string, data, app.get_asset_url(mask_filename), image_width, image_height, CANVAS_WIDTH, rect_fill)
mask = readMask((os.getcwd() + app.get_asset_url(mask_filename)))
if mask.shape != image.shape[:2] :
return dbc.Alert("Mask is not the same shape as image!", color="danger", duration=2000), dash.no_update
# Give default values to variables
if 'use' not in use_data :
data_significance = 0
if 'use' not in use_threshold :
threshold = None
live_preview = False
if 'show' in show_live_preview :
live_preview = True
# Initialize result image
inpainted_filename = 'results/' + image_ID + '.png'
inpainted = np.copy(image)
inpainted[mask == 0] = 0
# Run the inpainting algorithm
start_time = time.time()
inpaintingLogic(
image,
mask,
patch_size,
local_radius,
data_significance,
threshold,
live_preview,
app.get_asset_url(inpainted_filename),
app.get_asset_url(progress_filename)
)
print("ran inpainting algorithm in {} : ".format(time.time() - start_time))
# return the resulting div, with the result image
return dash.no_update, \
[
html.H3('Result'),
html.Hr(style = {'width' : '80%'}),
html.Div(
[
html.Div(
html.Img(
# id='result-image',
width=CANVAS_WIDTH,
src = app.get_asset_url(inpainted_filename)
),
id = 'result-image-div'
)
]
)
]
# Add external css for bootstrap
app.css.append_css({
'external_url': [
'https://codepen.io/chriddyp/pen/bWLwgP.css',
'https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css'
]
})
app.config['suppress_callback_exceptions'] = True
if __name__ == '__main__':
# To run server in local environment
app.run_server(debug=True, dev_tools_hot_reload = False)
# To run server when deployed
# http_server = WSGIServer(('0.0.0.0', int(os.environ.get("PORT", 5000))), server)
# print(int(os.environ.get("PORT", 5000)), file=sys.stderr)
# http_server.serve_forever()