Spaces:
Sleeping
Sleeping
jasoncordova
commited on
Commit
•
98053af
1
Parent(s):
6f3af15
Update app.py
Browse files
app.py
CHANGED
@@ -1,151 +1,125 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import ipyleaflet as L
|
2 |
+
from transformers import SamModel, SamConfig, SamProcessor
|
3 |
+
import torch
|
4 |
+
from faicons import icon_svg
|
5 |
+
from geopy.distance import geodesic, great_circle
|
6 |
+
from shiny import reactive
|
7 |
+
from shiny.express import input, render, ui
|
8 |
+
from shinywidgets import render_widget
|
9 |
+
import numpy as np
|
10 |
+
import ipywidgets as widgets
|
11 |
+
import io
|
12 |
+
import base64
|
13 |
+
from PIL import Image
|
14 |
+
import matplotlib.pyplot as plt
|
15 |
+
|
16 |
+
ui.tags.style(
|
17 |
+
"#file1_progress { height: 100%; }",
|
18 |
+
".bslib-sidebar-layout {--_sidebar-width: 360px !important; }",
|
19 |
+
" img { object-fit: contain; }",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
)
|
21 |
|
22 |
+
ui.page_opts(title="Segment Anything Model: Sidewalk Masking", fillable=True)
|
23 |
+
{"class": "bslib-page-dashboard"}
|
24 |
|
25 |
+
with ui.sidebar():
|
26 |
+
|
27 |
+
ui.input_file("file1", "Upload Image", accept=[".jpg", ".png", ".jpeg"], multiple=False),
|
28 |
+
ui.input_dark_mode(mode="dark")
|
29 |
+
|
30 |
+
with ui.card():
|
31 |
+
|
32 |
+
ui.card_header("Finalized Segment")
|
33 |
+
|
34 |
+
@render.text
|
35 |
+
def slider_val():
|
36 |
+
if input.file1() is None:
|
37 |
+
return None
|
38 |
+
else:
|
39 |
+
return "Here is the prediction mask:"
|
40 |
+
# return input.file1()[0]['datapath']
|
41 |
+
|
42 |
+
def getSegments():
|
43 |
+
# Load the model configuration
|
44 |
+
model_config = SamConfig.from_pretrained("facebook/sam-vit-base")
|
45 |
+
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
46 |
+
|
47 |
+
# Create an instance of the model architecture with the loaded configuration
|
48 |
+
my_mito_model = SamModel(config=model_config)
|
49 |
+
#Update the model by loading the weights from saved file.
|
50 |
+
my_mito_model.load_state_dict(torch.load("../modelv2.pth"))
|
51 |
+
|
52 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
53 |
+
my_mito_model.to(device)
|
54 |
+
|
55 |
+
# Define the size of your array
|
56 |
+
array_size = 256
|
57 |
+
|
58 |
+
# Define the size of your grid
|
59 |
+
grid_size = 10
|
60 |
+
|
61 |
+
# Generate the grid points
|
62 |
+
x = np.linspace(0, array_size-1, grid_size)
|
63 |
+
y = np.linspace(0, array_size-1, grid_size)
|
64 |
+
|
65 |
+
# Generate a grid of coordinates
|
66 |
+
xv, yv = np.meshgrid(x, y)
|
67 |
+
|
68 |
+
# Convert the numpy arrays to lists
|
69 |
+
xv_list = xv.tolist()
|
70 |
+
yv_list = yv.tolist()
|
71 |
+
|
72 |
+
# Combine the x and y coordinates into a list of list of lists
|
73 |
+
input_points = [[[int(x), int(y)] for x, y in zip(x_row, y_row)] for x_row, y_row in zip(xv_list, yv_list)]
|
74 |
+
input_points = torch.tensor(input_points).view(1, 1, grid_size*grid_size, 2)
|
75 |
+
|
76 |
+
inputs = processor(Image.open(input.file1()[0]['datapath']), input_points=input_points, return_tensors="pt")
|
77 |
+
|
78 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
79 |
+
my_mito_model.eval()
|
80 |
+
|
81 |
+
# forward pass
|
82 |
+
with torch.no_grad():
|
83 |
+
outputs = my_mito_model(**inputs, multimask_output=False)
|
84 |
+
|
85 |
+
# apply sigmoid
|
86 |
+
single_patch_prob = torch.sigmoid(outputs.pred_masks.squeeze(1))
|
87 |
+
# convert soft mask to hard mask
|
88 |
+
single_patch_prob = single_patch_prob.cpu().numpy().squeeze()
|
89 |
+
single_patch_prediction = (single_patch_prob > 0.5).astype(np.uint8)
|
90 |
+
return single_patch_prediction
|
91 |
+
|
92 |
+
# @render.image
|
93 |
+
# def render_image():
|
94 |
+
# # Get the uploaded file
|
95 |
+
# uploaded_file = input.file1()
|
96 |
+
|
97 |
+
# # If there is no uploaded file, return None
|
98 |
+
# if uploaded_file is None:
|
99 |
+
# return None
|
100 |
+
|
101 |
+
# # Read the image file
|
102 |
+
# imagePath = uploaded_file[0]['datapath']
|
103 |
+
|
104 |
+
# # processImage()
|
105 |
+
# return {"src": imagePath, "width": "100%"}
|
106 |
+
|
107 |
+
@render.image
|
108 |
+
def render_image():
|
109 |
+
# Get the uploaded file
|
110 |
+
uploaded_file = input.file1()
|
111 |
+
|
112 |
+
# If there is no uploaded file, return None
|
113 |
+
if uploaded_file is None:
|
114 |
+
return None
|
115 |
+
|
116 |
+
# Call getSegments to get the segmented image numpy array
|
117 |
+
segmented_image = np.array(getSegments())
|
118 |
+
segmented_image = segmented_image * 255
|
119 |
+
colorArray = segmented_image.astype(np.uint8)
|
120 |
+
image = Image.fromarray(colorArray)
|
121 |
+
|
122 |
+
imagePath = "test.jpg"
|
123 |
+
image.save(imagePath)
|
124 |
+
|
125 |
+
return {"src": imagePath, "height": "100%", "class": "contain"}
|