Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -93,6 +93,37 @@ def server(input, output, session: Session):
|
|
| 93 |
# Update reactive value
|
| 94 |
analysis_results.set(results)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
@output
|
| 97 |
@render.ui
|
| 98 |
def results_container():
|
|
@@ -105,13 +136,15 @@ def server(input, output, session: Session):
|
|
| 105 |
for i, r in enumerate(results):
|
| 106 |
|
| 107 |
id = "image_" + str(i)
|
| 108 |
-
opacity = ui.input_slider(id, "Opacity", 0, 1.0, 0.5)
|
|
|
|
|
|
|
| 109 |
|
| 110 |
ui_output.append(
|
| 111 |
ui.div(
|
| 112 |
ui.row(
|
| 113 |
ui.column(4, ui.img(src=f"data:image/png;base64,{r['image_base64']}")),
|
| 114 |
-
ui.column(4, ui.output_plot("plot_" + str(
|
| 115 |
),
|
| 116 |
opacity,
|
| 117 |
ui.h5(r['filename'], style="margin-top: 15px;"),
|
|
@@ -125,35 +158,6 @@ def server(input, output, session: Session):
|
|
| 125 |
class_="card p-3"
|
| 126 |
)
|
| 127 |
)
|
| 128 |
-
|
| 129 |
-
@output(id="plot_" + str(i))
|
| 130 |
-
@render.plot
|
| 131 |
-
def plot_prediction():
|
| 132 |
-
fig, ax = plt.subplots()
|
| 133 |
-
|
| 134 |
-
# ax = plt.Axes(fig, [0., 0., 1., 1.])
|
| 135 |
-
# ax.set_axis_off()
|
| 136 |
-
# fig.add_axes(ax)
|
| 137 |
-
|
| 138 |
-
v = Visualizer(r["image"][:, :, ::-1],
|
| 139 |
-
scale=1, instance_mode=ColorMode.SEGMENTATION, font_size_scale=1)
|
| 140 |
-
|
| 141 |
-
colours = []
|
| 142 |
-
for cls in r["instances"].pred_classes:
|
| 143 |
-
if cls == 0:
|
| 144 |
-
colours.append([1,0,0])
|
| 145 |
-
elif cls == 1:
|
| 146 |
-
colours.append([1,1,0])
|
| 147 |
-
elif cls == 2:
|
| 148 |
-
colours.append([0,0,0])
|
| 149 |
-
|
| 150 |
-
out = v.overlay_instances(masks = r["instances"].pred_masks.to("cpu"),
|
| 151 |
-
assigned_colors = colours,
|
| 152 |
-
alpha = input[id]())
|
| 153 |
-
|
| 154 |
-
ax.imshow(cv2.cvtColor(out.get_image()[:, :, ::-1], cv2.COLOR_BGR2RGB))
|
| 155 |
-
|
| 156 |
-
return fig
|
| 157 |
|
| 158 |
return ui.div(ui_output)
|
| 159 |
|
|
|
|
| 93 |
# Update reactive value
|
| 94 |
analysis_results.set(results)
|
| 95 |
|
| 96 |
+
def create_plot(i, r, opacity):
|
| 97 |
+
@output(id="plot_" + str(i))
|
| 98 |
+
@render.plot
|
| 99 |
+
def plot_prediction():
|
| 100 |
+
fig, ax = plt.subplots()
|
| 101 |
+
|
| 102 |
+
# ax = plt.Axes(fig, [0., 0., 1., 1.])
|
| 103 |
+
# ax.set_axis_off()
|
| 104 |
+
# fig.add_axes(ax)
|
| 105 |
+
|
| 106 |
+
v = Visualizer(r["image"][:, :, ::-1],
|
| 107 |
+
scale=1, instance_mode=ColorMode.SEGMENTATION, font_size_scale=1)
|
| 108 |
+
|
| 109 |
+
colours = []
|
| 110 |
+
for cls in r["instances"].pred_classes:
|
| 111 |
+
if cls == 0:
|
| 112 |
+
colours.append([1,0,0])
|
| 113 |
+
elif cls == 1:
|
| 114 |
+
colours.append([1,1,0])
|
| 115 |
+
elif cls == 2:
|
| 116 |
+
colours.append([0,0,0])
|
| 117 |
+
|
| 118 |
+
out = v.overlay_instances(masks = r["instances"].pred_masks.to("cpu"),
|
| 119 |
+
assigned_colors = colours,
|
| 120 |
+
alpha = opacity)
|
| 121 |
+
|
| 122 |
+
ax.imshow(cv2.cvtColor(out.get_image()[:, :, ::-1], cv2.COLOR_BGR2RGB))
|
| 123 |
+
|
| 124 |
+
return fig
|
| 125 |
+
return plot_prediction
|
| 126 |
+
|
| 127 |
@output
|
| 128 |
@render.ui
|
| 129 |
def results_container():
|
|
|
|
| 136 |
for i, r in enumerate(results):
|
| 137 |
|
| 138 |
id = "image_" + str(i)
|
| 139 |
+
opacity = ui.input_slider(id, "Opacity", 0, 1.0, 0.5)
|
| 140 |
+
|
| 141 |
+
output(create_plot(i, r, input[id]()), id="plot_" + str(i))
|
| 142 |
|
| 143 |
ui_output.append(
|
| 144 |
ui.div(
|
| 145 |
ui.row(
|
| 146 |
ui.column(4, ui.img(src=f"data:image/png;base64,{r['image_base64']}")),
|
| 147 |
+
ui.column(4, ui.output_plot("plot_" + str(i))),
|
| 148 |
),
|
| 149 |
opacity,
|
| 150 |
ui.h5(r['filename'], style="margin-top: 15px;"),
|
|
|
|
| 158 |
class_="card p-3"
|
| 159 |
)
|
| 160 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
return ui.div(ui_output)
|
| 163 |
|