Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,151 +1,81 @@
|
|
1 |
-
from pathlib import Path
|
2 |
-
from typing import List, Dict, Tuple
|
3 |
-
import matplotlib.colors as mpl_colors
|
4 |
-
|
5 |
import pandas as pd
|
6 |
-
import
|
7 |
-
import
|
8 |
-
|
9 |
-
from
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
species: List[str] = df["Species"].unique().tolist()
|
18 |
-
species.sort()
|
19 |
-
|
20 |
-
app_ui = ui.page_fillable(
|
21 |
-
shinyswatch.theme.minty(),
|
22 |
-
ui.layout_sidebar(
|
23 |
-
ui.sidebar(
|
24 |
-
# Artwork by @allison_horst
|
25 |
-
ui.input_selectize(
|
26 |
-
"xvar",
|
27 |
-
"X variable",
|
28 |
-
numeric_cols,
|
29 |
-
selected="Bill Length (mm)",
|
30 |
-
),
|
31 |
-
ui.input_selectize(
|
32 |
-
"yvar",
|
33 |
-
"Y variable",
|
34 |
-
numeric_cols,
|
35 |
-
selected="Bill Depth (mm)",
|
36 |
-
),
|
37 |
-
ui.input_checkbox_group(
|
38 |
-
"species", "Filter by species", species, selected=species
|
39 |
-
),
|
40 |
-
ui.hr(),
|
41 |
-
ui.input_switch("by_species", "Show species", value=True),
|
42 |
-
ui.input_switch("show_margins", "Show marginal plots", value=True),
|
43 |
-
),
|
44 |
-
ui.output_ui("value_boxes"),
|
45 |
-
ui.output_plot("scatter", fill=True),
|
46 |
-
ui.help_text(
|
47 |
-
"Artwork by ",
|
48 |
-
ui.a("@allison_horst", href="https://twitter.com/allison_horst"),
|
49 |
-
class_="text-end",
|
50 |
-
),
|
51 |
-
),
|
52 |
)
|
53 |
|
54 |
-
|
55 |
def server(input: Inputs, output: Outputs, session: Session):
|
56 |
-
@reactive.
|
57 |
-
def
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# Filter the rows so we only include the desired species
|
64 |
-
return df[df["Species"].isin(input.species())]
|
65 |
-
|
66 |
@output
|
67 |
@render.plot
|
68 |
-
def
|
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 |
-
return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
|
127 |
-
|
128 |
-
|
129 |
-
# "darkorange", "purple", "cyan4"
|
130 |
-
colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
|
131 |
-
colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
|
132 |
-
|
133 |
-
palette: Dict[str, Tuple[float, float, float]] = {
|
134 |
-
"Adelie": colors[0],
|
135 |
-
"Chinstrap": colors[1],
|
136 |
-
"Gentoo": colors[2],
|
137 |
-
"default": sns.color_palette()[0], # type: ignore
|
138 |
-
}
|
139 |
-
|
140 |
-
bg_palette = {}
|
141 |
-
# Use `sns.set_style("whitegrid")` to help find approx alpha value
|
142 |
-
for name, col in palette.items():
|
143 |
-
# Adjusted n_colors until `axe` accessibility did not complain about color contrast
|
144 |
-
bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
|
145 |
-
|
146 |
-
|
147 |
-
app = App(
|
148 |
-
app_ui,
|
149 |
-
server,
|
150 |
-
static_assets=str(www_dir),
|
151 |
-
)
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
+
import asyncio
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import numpy as np
|
5 |
+
from transformers import SamModel, SamConfig, SamProcessor
|
6 |
+
import torch
|
7 |
+
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
app_ui = ui.page_fluid(
|
11 |
+
ui.input_file("file1", "Upload Tile image for sidewalk segmentation", accept=".tif", multiple=False),
|
12 |
+
ui.output_plot("mask"), # Changed from ui.output_table to ui.output_plot based on the context of output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
)
|
14 |
|
|
|
15 |
def server(input: Inputs, output: Outputs, session: Session):
|
16 |
+
@reactive.calc
|
17 |
+
def parsed_file():
|
18 |
+
file_info = input.file1()
|
19 |
+
if file_info is None or len(file_info) == 0:
|
20 |
+
return None
|
21 |
+
return file_info[0]["datapath"]
|
22 |
+
|
|
|
|
|
|
|
23 |
@output
|
24 |
@render.plot
|
25 |
+
async def mask():
|
26 |
+
filepath = parsed_file()
|
27 |
+
if filepath is None:
|
28 |
+
return
|
29 |
+
print(filepath)
|
30 |
+
# Assuming the model and processor are correctly configured
|
31 |
+
model_config = SamConfig.from_pretrained("facebook/sam-vit-base")
|
32 |
+
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
33 |
+
my_sidewalk_model = SamModel(model_config)
|
34 |
+
my_sidewalk_model.load_state_dict(torch.load("./sidwalk_model_checkpoint.pth", map_location='cpu'))
|
35 |
+
device = torch.device("cpu")
|
36 |
+
my_sidewalk_model.to(device)
|
37 |
+
|
38 |
+
# Load image
|
39 |
+
image = Image.open(filepath)
|
40 |
+
imarray = np.array(image)
|
41 |
+
single_patch = Image.fromarray(imarray)
|
42 |
+
|
43 |
+
inputs = processor(single_patch, return_tensors="pt")
|
44 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
45 |
+
my_sidewalk_model.eval()
|
46 |
+
# Model inference
|
47 |
+
with torch.no_grad():
|
48 |
+
outputs = my_sidewalk_model(**inputs, multimask_output=False)
|
49 |
+
single_patch_prob = torch.sigmoid(outputs.pred_masks.squeeze(1))
|
50 |
+
# convert soft mask to hard mask
|
51 |
+
single_patch_prob = single_patch_prob.cpu().numpy().squeeze()
|
52 |
+
single_patch_prediction = (single_patch_prob > 0).astype(np.uint8)
|
53 |
+
|
54 |
+
|
55 |
+
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
56 |
+
|
57 |
+
# Plot the first image on the left
|
58 |
+
axes[0].imshow(np.array(single_patch), cmap='gray') # Assuming the first image is grayscale
|
59 |
+
axes[0].set_title("Image")
|
60 |
+
|
61 |
+
# Plot the second image on the right
|
62 |
+
axes[1].imshow(single_patch_prob) # Assuming the second image is grayscale
|
63 |
+
axes[1].set_title("Probability Map")
|
64 |
+
|
65 |
+
# Plot the second image on the right
|
66 |
+
axes[2].imshow(single_patch_prediction, cmap='gray') # Assuming the second image is grayscale
|
67 |
+
axes[2].set_title("Prediction")
|
68 |
+
|
69 |
+
# Hide axis ticks and labels
|
70 |
+
for ax in axes:
|
71 |
+
ax.set_xticks([])
|
72 |
+
ax.set_yticks([])
|
73 |
+
ax.set_xticklabels([])
|
74 |
+
ax.set_yticklabels([])
|
75 |
+
|
76 |
+
# Display the images side by side
|
77 |
+
return fig
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
app = App(app_ui, server)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|