-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
426 lines (368 loc) · 12.1 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import base64
from io import BytesIO
import os
import time
import urllib
import cv2
import dash
import dash_design_kit as ddk
from dash import dcc, html, Input, Output, State
from flask_caching import Cache
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import requests
from theme import custom_theme
os.environ["REDIS_URL"] = os.environ.get("REDIS_URL", "redis://127.0.0.1:6379")
def array_to_b64(img, enc="jpg"):
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
is_success, buffer = cv2.imencode(f".{enc}", img)
io_buf = BytesIO(buffer)
encoded = base64.b64encode(io_buf.getvalue()).decode("utf-8")
return f"data:img/{enc};base64, " + encoded
def download_file(url, filename):
if os.path.exists(filename):
print(f"{filename} already exists.")
else:
print(f"{filename} does not exist. Downloading...", end=" ")
r = requests.get(url, allow_redirects=True)
with open(filename, "wb") as f:
f.write(r.content)
print("Done.")
def get_selected_frames(summary, label, min_elts, max_elts):
return summary[
np.logical_and(summary[label] >= min_elts, summary[label] <= max_elts)
].index
def load_image(url):
with urllib.request.urlopen(url) as response:
image = np.asarray(bytearray(response.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
image = image[:, :, [2, 1, 0]] # BGR -> RGB
return image
def load_network(config_path, weights_path):
net = cv2.dnn.readNetFromDarknet(config_path, weights_path)
output_layer_names = net.getLayerNames()
output_layer_names = [
output_layer_names[i -1] for i in net.getUnconnectedOutLayers()
]
return net, output_layer_names
def add_bbox(fig, x0, y0, x1, y1, color="red", opacity=0.5, name=""):
fig.add_scatter(
x=[x0, x1, x1, x0, x0],
y=[y0, y0, y1, y1, y0],
mode="lines",
fill="toself",
opacity=opacity,
marker_color=color,
hoveron="fills",
hoverlabel_namelength=-1,
name=name,
)
def create_summary(metadata):
one_hot_encoded = pd.get_dummies(metadata[["frame", "label"]], columns=["label"])
summary = (
one_hot_encoded.groupby(["frame"])
.sum()
.rename(
columns={
"label_biker": "biker",
"label_car": "car",
"label_pedestrian": "pedestrian",
"label_trafficLight": "traffic light",
"label_truck": "truck",
}
)
)
return summary
def yolo_v3(image, net, output_layer_names, confidence_threshold, overlap_threshold):
# Run the YOLO neural net.
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layer_outputs = net.forward(output_layer_names)
# Supress detections in case of too low confidence or too much overlap.
boxes, confidences, class_IDs = [], [], []
H, W = image.shape[:2]
for output in layer_outputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > confidence_threshold:
box = detection[0:4] * np.array([W, H, W, H])
centerX, centerY, width, height = box.astype("int")
x, y = int(centerX - (width / 2)), int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
class_IDs.append(classID)
indices = cv2.dnn.NMSBoxes(
boxes, confidences, confidence_threshold, overlap_threshold
)
# Map from YOLO labels to Udacity labels.
UDACITY_LABELS = {
0: "pedestrian",
1: "biker",
2: "car",
3: "biker",
5: "truck",
7: "truck",
9: "trafficLight",
}
xmin, xmax, ymin, ymax, labels, scores = [], [], [], [], [], []
if len(indices) > 0:
# loop over the indexes we are keeping
for i in indices.flatten():
label = UDACITY_LABELS.get(class_IDs[i], None)
if label is None:
continue
# extract the bounding box coordinates
x, y, w, h = boxes[i][0], boxes[i][1], boxes[i][2], boxes[i][3]
xmin.append(x)
ymin.append(y)
xmax.append(x + w)
ymax.append(y + h)
labels.append(label)
scores.append(confidences[i])
boxes = pd.DataFrame(
{
"xmin": xmin,
"ymin": ymin,
"xmax": xmax,
"ymax": ymax,
"label": labels,
"confidence": scores,
}
)
return boxes
def display_images_with_bbox(image, boxes, title):
LABEL_COLORS = {
"car": ["LightSkyBlue", "RoyalBlue"],
"pedestrian": ["red", "darkred"],
"truck": ["green", "darkgreen"],
"trafficLight": ["lightyellow", "yellow"],
"biker": ["orange", "darkorange"],
}
img_height, img_width = image.shape[:2]
fig = go.Figure()
# Add invisible scatter trace.
# This trace is added to help the autoresize logic work.
fig.add_trace(
go.Scatter(
x=[img_width * 0.05, img_width * 0.95],
y=[img_height * 0.95, img_height * 0.05],
mode="markers",
marker_opacity=0,
hoverinfo="none",
)
)
fig.add_layout_image(
dict(
source=array_to_b64(image),
x=0,
y=0,
xref="x",
yref="y",
sizex=img_width,
sizey=img_height,
sizing="stretch",
opacity=1,
layer="below",
)
)
for index, box in boxes.iterrows():
fill_col, line_col = LABEL_COLORS[box.label]
add_bbox(
fig,
x0=box.xmin,
y0=box.ymin,
x1=box.xmax,
y1=box.ymax,
opacity=0.5,
color=fill_col,
name=f"class={box.label}<br>confidence={box.confidence:.3f}",
)
# Adapt axes to the right width and height, lock aspect ratio
fig.update_xaxes(
showgrid=False, visible=False, constrain="domain", range=[0, img_width]
)
fig.update_yaxes(
showgrid=False,
visible=False,
scaleanchor="x",
scaleratio=1,
range=[img_height, 0],
)
fig.update_layout(title=title, showlegend=False)
return fig
# Load labels and generate a summary
metadata = pd.read_csv("labels.csv.gz")
metadata["confidence"] = 1.0
summary = create_summary(metadata)
# Download model, and load it from the files
download_file(
"https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg",
"yolov3.cfg",
)
download_file(
"https://images.plot.ly/udacity-self-driving-cars/yolov3.weights", "yolov3.weights"
)
net, output_layer_names = load_network("yolov3.cfg", "yolov3.weights")
print("Redis cache:", os.environ.get("REDIS_URL", ""))
app = dash.Dash(__name__)
server = app.server # expose server variable for Procfile
learn_more_menu = ddk.CollapsibleMenu(
title="Learn More",
default_open=False,
children=[
html.A(
"About this App",
target="_blank",
href="https://medium.com/plotly/productionizing-object-detection-models-with-dash-enterprise-dba1c9402c2f",
),
html.A(
"Low-code Design",
href="https://plotly.com/dash/design-kit/",
target="_blank",
),
html.A(
"Snapshot Engine",
href="https://plotly.com/dash/snapshot-engine/",
target="_blank",
),
html.A("Enterprise Demo", href="https://plotly.com/get-demo/", target="_blank"),
html.A("Request Code", href="https://plotly.com/contact-us/", target="_blank"),
],
)
# Create a cache, and cache all the heavy functions
cache = Cache(
app.server,
config={"CACHE_TYPE": "redis", "CACHE_REDIS_URL": os.environ.get("REDIS_URL", "")},
)
load_image = cache.memoize()(load_image)
yolo_v3 = cache.memoize()(yolo_v3)
frame_controls = [
ddk.ControlItem(
dcc.Dropdown(
id="dropdown-object",
value=summary.columns[2],
clearable=False,
searchable=False,
options=[{"label": i, "value": i} for i in summary.columns],
),
label="Find a frame containing:",
width=30,
),
ddk.ControlItem(
width=45,
children=[
html.Button("Prev Frame", id="button-previous-frame", n_clicks=0,),
html.Button("Next Frame", id="button-next-frame", n_clicks=0,),
],
),
ddk.ControlItem(
html.Button("Random Frame", id="button-random-frame", n_clicks=0,), width=25,
),
]
model_controls = [
ddk.ControlItem(
dcc.Slider(
id="slider-confidence",
min=0,
max=1,
marks={0: "0.00", 1: "1.00"},
step=0.01,
value=0.5,
tooltip=dict(always_visible=True, placement="bottom"),
),
label="Yolo v3 Confidence Threshold:",
),
ddk.ControlItem(
dcc.Slider(
id="slider-overlap",
min=0,
max=1,
marks={0: "0.00", 1: "1.00"},
step=0.01,
value=0.5,
tooltip=dict(always_visible=True, placement="bottom"),
),
label="Yolo v3 Overlap Threshold:",
),
]
menu = ddk.Menu(
[
learn_more_menu,
]
)
app.layout = ddk.App(
show_editor=False,
theme=custom_theme,
children=[
ddk.Header(
[
ddk.Logo(
src=app.get_asset_url("logo.png"),
style={"maxHeight": "100px", "width": "auto"},
),
ddk.Title("Object detection for self-driving cars"),
menu,
]
),
ddk.Row(
[
ddk.ControlCard(ddk.Graph(id="graph-ground-truth"), width=50),
ddk.ControlCard(ddk.Graph(id="graph-yolo-v3"), width=50),
]
),
ddk.Row(
[
ddk.ControlCard(frame_controls, width=50, orientation="horizontal"),
ddk.ControlCard(model_controls, width=50, orientation="horizontal"),
]
),
],
)
@app.callback(
Output("button-next-frame", "n_clicks"),
[Input("button-random-frame", "n_clicks")],
[State("button-next-frame", "n_clicks")],
)
def select_random_frame(_, next_n_clicks):
return next_n_clicks + 50
@app.callback(
[Output("graph-ground-truth", "figure"), Output("graph-yolo-v3", "figure")],
[
Input("dropdown-object", "value"),
Input("button-previous-frame", "n_clicks"),
Input("button-next-frame", "n_clicks"),
Input("slider-confidence", "value"),
Input("slider-overlap", "value"),
],
)
def update_graphs(
object_type, prev_n_clicks, next_n_clicks, confidence_threshold, overlap_threshold
):
t1 = time.time()
min_elts, max_elts = 1, 25
selected_frames = get_selected_frames(summary, object_type, min_elts, max_elts)
num_frames = len(selected_frames)
selected_frame_index = (next_n_clicks - prev_n_clicks) % num_frames
selected_frame = selected_frames[selected_frame_index]
img_bucket = "https://images.plot.ly/udacity-self-driving-cars/"
image_url = os.path.join(img_bucket, selected_frame)
image = load_image(image_url)
t2 = time.time()
real_boxes = metadata[metadata.frame == selected_frame].drop(columns=["frame"])
real_fig = display_images_with_bbox(image, real_boxes, "Human Annotated Frame")
t3 = time.time()
yolo_boxes = yolo_v3(
image, net, output_layer_names, confidence_threshold, overlap_threshold
)
yolo_fig = display_images_with_bbox(image, yolo_boxes, "Yolo v3 Annotated Frame")
t4 = time.time()
print(
f"Finished predictions in {t4-t3:.2f}s. Loaded Image in {t2-t1:.2f}s. Total time: {t4-t1:.2f}s"
)
return real_fig, yolo_fig
if __name__ == "__main__":
app.run_server(debug=True)