Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
15 |
|
|
|
|
|
|
|
|
|
16 |
return segmented_image
|
17 |
|
18 |
-
|
19 |
-
gr.Interface(
|
20 |
-
|
21 |
-
inputs=gr.
|
22 |
-
outputs=gr.
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torchvision import transforms
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
# Load the segmentation model (replace 'path/to/lightmed_model' with the actual path)
|
7 |
+
model_path = 'path/to/lightmed_model'
|
8 |
+
segmentation_model = torch.load(model_path, map_location=torch.device('cpu'))
|
9 |
+
segmentation_model.eval()
|
10 |
|
11 |
+
# Define the preprocessing function for the input image
|
12 |
+
def preprocess(image):
|
13 |
+
# Resize the image to match the model's expected input size
|
14 |
+
transform = transforms.Compose([
|
15 |
+
transforms.Resize((256, 256)),
|
16 |
+
transforms.ToTensor(),
|
17 |
+
])
|
18 |
+
img = Image.fromarray(image)
|
19 |
+
img = transform(img).unsqueeze(0)
|
20 |
+
return img
|
21 |
|
22 |
+
# Define the segmentation function
|
23 |
+
def segment_image(input_image):
|
24 |
+
# Preprocess the input image
|
25 |
+
input_tensor = preprocess(input_image)
|
26 |
|
27 |
+
# Perform segmentation using the model
|
28 |
+
with torch.no_grad():
|
29 |
+
output = segmentation_model(input_tensor)
|
30 |
|
31 |
+
# Convert the output tensor to a segmented image
|
32 |
+
segmented_image = torch.argmax(output, dim=1).squeeze().numpy()
|
33 |
+
|
34 |
+
# Return the segmented image
|
35 |
return segmented_image
|
36 |
|
37 |
+
# Define the Gradio interface
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=segment_image,
|
40 |
+
inputs=gr.Image(type="pil", preprocess=preprocess),
|
41 |
+
outputs=gr.Image(type="numpy")
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch the Gradio app
|
45 |
+
iface.launch()
|