Spaces:
Runtime error
Runtime error
Ansal
commited on
Commit
•
ab52a15
1
Parent(s):
05a1f51
initial commit
Browse files
app.py
CHANGED
@@ -1,151 +1,92 @@
|
|
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 shiny import App, Inputs, Outputs, Session, reactive, render,
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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 |
-
|
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 |
-
# Only include boxes for _selected_ species
|
123 |
-
if name in input.species()
|
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 tempfile
|
3 |
+
from PIL import Image
|
4 |
+
from pathlib import Path
|
5 |
+
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
|
6 |
+
from shiny.types import FileInfo
|
7 |
+
import json
|
8 |
+
import torch
|
9 |
+
import numpy as np
|
10 |
+
import os
|
11 |
+
from transformers import SamModel
|
12 |
+
import torchvision.transforms as transforms
|
13 |
+
import matplotlib.pyplot as plt
|
14 |
+
|
15 |
+
image_resize_transform = transforms.Compose([
|
16 |
+
transforms.Resize((1024, 1024)),
|
17 |
+
transforms.ToTensor()
|
18 |
+
])
|
19 |
+
|
20 |
+
app_ui = ui.page_fluid(
|
21 |
+
ui.input_file("file2", "Choose Image", accept=".jpg, .jpeg, .png, .tiff, .tif", multiple=False),
|
22 |
+
ui.output_image("original_image"),
|
23 |
+
ui.output_image("image_display")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
|
|
|
26 |
def server(input: Inputs, output: Outputs, session: Session):
|
27 |
+
@reactive.calc
|
28 |
+
def loaded_image():
|
29 |
+
file: list[FileInfo] | None = input.file2()
|
30 |
+
if file is None:
|
31 |
+
return None
|
32 |
+
|
33 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
34 |
+
model2 = SamModel.from_pretrained("facebook/sam-vit-base")
|
35 |
+
model2.load_state_dict(torch.load('model.pth', map_location=device))
|
36 |
+
model2.eval()
|
37 |
+
model2.to(device)
|
38 |
+
|
39 |
+
image = Image.open(file[0]["datapath"]).convert('RGB')
|
40 |
+
transform = image_resize_transform
|
41 |
+
image_tensor = transform(image).to(device)
|
42 |
+
|
43 |
+
|
44 |
+
with torch.no_grad():
|
45 |
+
outputs = model2(pixel_values=image_tensor.unsqueeze(0),multimask_output=False)
|
46 |
+
predicted_masks = outputs.pred_masks.squeeze(1)
|
47 |
+
predicted_masks = predicted_masks[:, 0, :, :]
|
48 |
+
|
49 |
+
|
50 |
+
mask_tensor = predicted_masks.cpu().detach().squeeze()
|
51 |
+
mask_array = mask_tensor.numpy()
|
52 |
+
mask_array = (mask_array * 255).astype(np.uint8)
|
53 |
+
mask = Image.fromarray(mask_array)
|
54 |
+
mask = mask.resize((1024, 1024), Image.LANCZOS)
|
55 |
+
mask = mask.convert('RGBA')
|
56 |
+
|
57 |
+
|
58 |
+
alpha = Image.new('L', mask.size, 128)
|
59 |
+
mask.putalpha(alpha)
|
60 |
+
|
61 |
+
|
62 |
+
image = Image.open(file[0]["datapath"]).convert('RGB')
|
63 |
+
image = image.resize((1024, 1024), Image.LANCZOS)
|
64 |
+
image = image.convert('RGBA')
|
65 |
+
|
66 |
+
combined = Image.alpha_composite(image, mask)
|
67 |
+
|
68 |
+
|
69 |
+
combined_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
70 |
+
original_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
71 |
+
image.save(original_file.name, "PNG", quality=100)
|
72 |
+
mask.save(combined_file.name, "PNG", quality=100)
|
73 |
+
|
74 |
+
return original_file.name, combined_file.name
|
75 |
+
|
76 |
+
@render.image
|
77 |
+
def original_image():
|
78 |
+
result = loaded_image()
|
79 |
+
if result is None:
|
80 |
+
return None
|
81 |
+
img_path, _ = result
|
82 |
+
return {"src": img_path, "width": "300px"}
|
83 |
+
|
84 |
+
@render.image
|
85 |
+
def image_display():
|
86 |
+
result = loaded_image()
|
87 |
+
if result is None:
|
88 |
+
return None
|
89 |
+
_, img_path = result
|
90 |
+
return {"src": img_path, "width": "300px"}
|
91 |
+
|
92 |
+
app = App(app_ui, server)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|