/
app.py
349 lines (305 loc) · 10.7 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
"""
This app creates a collapsible, responsive sidebar layout with
dash-bootstrap-components and some custom css with media queries.
When the screen is small, the sidebar moved to the top of the page, and the
links get hidden in a collapse element. We use a callback to toggle the
collapse when on a small screen, and the custom CSS to hide the toggle, and
force the collapse to stay open when the screen is large.
dcc.Location is used to track the current location, a callback uses the current
location to render the appropriate page content. The active prop of each
NavLink is set automatically according to the current pathname. To use this
feature you must install dash-bootstrap-components >= 0.11.0.
For more details on building multi-page Dash applications, check out the Dash
documentation: https://dash.plot.ly/urls
"""
import os
import base64
from utils import *
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
app = dash.Dash(
__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP],
meta_tags=[
{"name": "viewport", "content": "width=device-width, initial-scale=1"}
],
suppress_callback_exceptions=True
)
server = app.server
image_filename = 'emanon.jpg' # replace with your own image
with open(image_filename, 'rb') as f:
encoded_image = base64.b64encode(f.read()).decode('ascii')
def three_lines_button():
div_component = html.Div(style={
"width": 35,
"height": 5,
"background-color": "white",
"marginTop": 5,
"marginBottom": 5
})
butt_component = html.Div([
div_component,
div_component,
div_component
])
return butt_component
SIDEBAR_STYLE = {
"position": "fixed",
"top": 0,
"left": 0,
"bottom": 0,
"width": "16rem",
"padding": "2rem 1rem",
"background-color": "black",
"color": "white"
}
button_nav = three_lines_button()
# we use the Row and Col components to construct the sidebar header
# it consists of a title, and a toggle, the latter is hidden on large screens
sidebar_header = dbc.Row(
[
dbc.Col(
[
html.Button(
# use the Bootstrap navbar-toggler classes to style
# html.Span(className="navbar-toggler-icon"),
button_nav,
className="navbar-toggler",
# the navbar-toggler classes don't set color
style={
"border-color": "rgba(255,255,255,.6)",
'marginBottom': 20,
'marginTop': 20,
'marginLeft': 20
},
id="navbar-toggle",
),
],
# the column containing the toggle will be only as wide as the
# toggle, resulting in the toggle being right aligned
width="auto",
# vertically align the toggle in the center
align="center",
)
],
style={"background-color": "black"},
className="sidebar"
)
sidebar = html.Div(
[
sidebar_header,
dbc.Collapse(
dbc.Nav(
[
dbc.NavLink("Face Verification", href="/face-verification", active="exact")
],
vertical=True,
pills=True,
style={
"background-color": "black",
"color": "white"
}
),
id="collapse"
),
],
id="sidebar"
)
simple_sidebar = html.Div(
[
html.H2("Menu", className="display-4"),
html.Hr(),
dbc.Nav(
[
dbc.NavLink("Face Verification", href="/face-verification", active="exact")
],
vertical=True,
pills=False,
),
],
style=SIDEBAR_STYLE,
className="simplesidebar"
)
images_component = dbc.Row(
[
dbc.Col(
html.Div(
html.Img(src='data:image/png;base64,{}'.format(encoded_image), className="img"),
id="output-data-upload",
className="img-div"
),
width=5
),
dbc.Col(
html.Div(
html.Img(src='data:image/png;base64,{}'.format(encoded_image), className="img"),
id="output-data-upload-2",
className="img-div"
),
width=5
),
], justify="center", className="images-component")
images_component_cell = dbc.Row([
dbc.Col(html.Div([
dbc.Row(
html.Div(
html.Img(src='data:image/png;base64,{}'.format(encoded_image), className="img"),
style={'margin': 50},
id="output-data-upload-c"
)
),
dbc.Row(
html.Div(
html.Img(src='data:image/png;base64,{}'.format(encoded_image), className="img"),
style={'margin': 50},
id="output-data-upload-c-2"
)
)
]))
], justify="center", className="images-component-cell")
page_content_face = [
dbc.Container([
html.Div([
dbc.Row([
dbc.Col(simple_sidebar, width={"size":2}, className="simplesidebar"),
dbc.Col([
dbc.Row([
html.Div([
html.H2('Face Verification API', style={"margin": 20}),
html.Hr()
], style={"text-align": "center"})
], justify="center"),
dbc.Row([
dcc.Upload(
id="upload-data",
children=html.Div([
"Drag and Drop or ",
html.A("Select Files")
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin-bottom': '20px'
}
),
], justify="center"),
images_component,
images_component_cell,
dbc.Card([
dbc.CardHeader("Similarity Porcentage: {}".format(90), style={"color": "white", "text-align": "center", "font-size": "20px"}, id="similarity-result"),
dbc.CardBody([
dbc.Button("Get Similarity", color="dark", style={"margin-top": "20px", "margin-bottom": "20px"}, block=True)
]),
], style={"margin-top": "20px", "margin-bottom": "20px"}, color="secondary")
])
])
])
],
fluid=True)
]
content = html.Div(id="page-content")
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
if pathname == "/":
return page_content_face
elif pathname == "/face-verification":
return page_content_face
# If the user tries to reach a different page, return a 404 message
return dbc.Jumbotron(
[
html.H1("404: Not found", className="text-danger"),
html.Hr(),
html.P(f"The pathname {pathname} was not recognised..."),
]
)
# @app.callback(dash.dependencies.Output('display-value', 'children'),
# [dash.dependencies.Input('dropdown', 'value')])
# def display_value(value):
# return 'You have selected "{}"'.format(value)
@app.callback(Output('output-data-upload', 'children'),
Input('upload-data', 'contents'))
def update_output(content):
if content is not None:
# with open("content_callback", "w") as f:
# f.write(content)
# data:tipo;base64
children = [html.Img(src=content, className="img")]
return children
else:
return [html.Img(src='data:image/png;base64,{}'.format(encoded_image), className="img")]
@app.callback(
Output("collapse", "is_open"),
[Input("navbar-toggle", "n_clicks")],
[State("collapse", "is_open")],
)
def toggle_collapse(n, is_open):
if n:
return not is_open
return is_open
if __name__ == "__main__":
app.run_server(debug=True)
# import os
# import base64
# from utils import *
# import dash
# from dash.dependencies import Input, Output, State
# import dash_core_components as dcc
# import dash_html_components as html
# import dash_bootstrap_components as dbc
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# image_filename = 'emanon.jpg' # replace with your own image
# with open(image_filename, 'rb') as f:
# encoded_image = base64.b64encode(f.read()).decode('ascii')
# app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
# server = app.server
# app.layout = html.Div([
# html.H2('Hello World'),
# dcc.Dropdown(
# id='dropdown',
# options=[{'label': i, 'value': i} for i in ['LA', 'NYC', 'MTL']],
# value='LA'
# ),
# dcc.Upload(
# id="upload-data",
# children=html.Div([
# "Drag and Drop or ",
# html.A("Select Files")
# ]),
# style={
# 'width': '100%',
# 'height': '60px',
# 'lineHeight': '60px',
# 'borderWidth': '1px',
# 'borderStyle': 'dashed',
# 'borderRadius': '5px',
# 'textAlign': 'center',
# 'margin': '10px'
# }
# ),
# html.Img(src='data:image/png;base64,{}'.format(encoded_image)),
# html.Div(id="output-data-upload")
# ])
# @app.callback(dash.dependencies.Output('display-value', 'children'),
# [dash.dependencies.Input('dropdown', 'value')])
# def display_value(value):
# return 'You have selected "{}"'.format(value)
# @app.callback(Output('output-data-upload', 'children'),
# Input('upload-data', 'contents'))
# def update_output(content):
# if content is not None:
# # with open("content_callback", "w") as f:
# # f.write(content)
# # data:tipo;base64
# children = [html.Img(src=content)]
# return children
# if __name__ == '__main__':
# app.run_server(debug=True)