Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,18 @@
|
|
|
|
1 |
import tempfile
|
2 |
-
from PIL import Image
|
3 |
from pathlib import Path
|
4 |
-
|
5 |
-
from
|
6 |
import torch
|
7 |
import numpy as np
|
8 |
-
import os
|
9 |
-
from transformers import SamModel
|
10 |
import torchvision.transforms as transforms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
image_resize_transform = transforms.Compose([
|
13 |
transforms.Resize((1024, 1024)),
|
@@ -37,12 +42,10 @@ def server(input: Inputs, output: Outputs, session: Session):
|
|
37 |
transform = image_resize_transform
|
38 |
image_tensor = transform(image).to(device)
|
39 |
|
40 |
-
|
41 |
with torch.no_grad():
|
42 |
-
outputs = model2(pixel_values=image_tensor.unsqueeze(0),multimask_output=False)
|
43 |
predicted_masks = outputs.pred_masks.squeeze(1)
|
44 |
predicted_masks = predicted_masks[:, 0, :, :]
|
45 |
-
|
46 |
|
47 |
mask_tensor = predicted_masks.cpu().detach().squeeze()
|
48 |
mask_array = mask_tensor.numpy()
|
@@ -50,19 +53,16 @@ def server(input: Inputs, output: Outputs, session: Session):
|
|
50 |
mask = Image.fromarray(mask_array)
|
51 |
mask = mask.resize((1024, 1024), Image.LANCZOS)
|
52 |
mask = mask.convert('RGBA')
|
53 |
-
|
54 |
|
55 |
alpha = Image.new('L', mask.size, 128)
|
56 |
mask.putalpha(alpha)
|
57 |
|
58 |
-
|
59 |
image = Image.open(file[0]["datapath"]).convert('RGB')
|
60 |
image = image.resize((1024, 1024), Image.LANCZOS)
|
61 |
image = image.convert('RGBA')
|
62 |
|
63 |
combined = Image.alpha_composite(image, mask)
|
64 |
|
65 |
-
|
66 |
combined_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
67 |
original_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
68 |
image.save(original_file.name, "PNG", quality=100)
|
|
|
1 |
+
import os
|
2 |
import tempfile
|
|
|
3 |
from pathlib import Path
|
4 |
+
import pandas as pd
|
5 |
+
from PIL import Image
|
6 |
import torch
|
7 |
import numpy as np
|
|
|
|
|
8 |
import torchvision.transforms as transforms
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
|
11 |
+
from shiny.types import FileInfo
|
12 |
+
|
13 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache/"
|
14 |
+
|
15 |
+
from transformers import SamModel
|
16 |
|
17 |
image_resize_transform = transforms.Compose([
|
18 |
transforms.Resize((1024, 1024)),
|
|
|
42 |
transform = image_resize_transform
|
43 |
image_tensor = transform(image).to(device)
|
44 |
|
|
|
45 |
with torch.no_grad():
|
46 |
+
outputs = model2(pixel_values=image_tensor.unsqueeze(0), multimask_output=False)
|
47 |
predicted_masks = outputs.pred_masks.squeeze(1)
|
48 |
predicted_masks = predicted_masks[:, 0, :, :]
|
|
|
49 |
|
50 |
mask_tensor = predicted_masks.cpu().detach().squeeze()
|
51 |
mask_array = mask_tensor.numpy()
|
|
|
53 |
mask = Image.fromarray(mask_array)
|
54 |
mask = mask.resize((1024, 1024), Image.LANCZOS)
|
55 |
mask = mask.convert('RGBA')
|
|
|
56 |
|
57 |
alpha = Image.new('L', mask.size, 128)
|
58 |
mask.putalpha(alpha)
|
59 |
|
|
|
60 |
image = Image.open(file[0]["datapath"]).convert('RGB')
|
61 |
image = image.resize((1024, 1024), Image.LANCZOS)
|
62 |
image = image.convert('RGBA')
|
63 |
|
64 |
combined = Image.alpha_composite(image, mask)
|
65 |
|
|
|
66 |
combined_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
67 |
original_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
68 |
image.save(original_file.name, "PNG", quality=100)
|