Spaces:
Sleeping
Sleeping
add example images for segmentation
Browse files
app.py
CHANGED
|
@@ -50,19 +50,27 @@ def segment(image_files):
|
|
| 50 |
|
| 51 |
# components for Gradion interface
|
| 52 |
def update_gallery(images):
|
| 53 |
-
print(f"
|
| 54 |
-
gallery_data = []
|
| 55 |
|
|
|
|
| 56 |
if images:
|
| 57 |
-
segmented_images = segment(images) # Process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
return gallery_data
|
| 64 |
|
| 65 |
-
# Gradio UI for MEI segmentation
|
| 66 |
import gradio as gr
|
| 67 |
|
| 68 |
with gr.Blocks() as demo:
|
|
@@ -80,5 +88,21 @@ with gr.Blocks() as demo:
|
|
| 80 |
fn=update_gallery,
|
| 81 |
inputs=[image_files],
|
| 82 |
outputs=[gallery])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
demo.launch()
|
|
|
|
| 50 |
|
| 51 |
# components for Gradion interface
|
| 52 |
def update_gallery(images):
|
| 53 |
+
print(f"Images received: {images}")
|
|
|
|
| 54 |
|
| 55 |
+
gallery_data = []
|
| 56 |
if images:
|
| 57 |
+
segmented_images = segment(images) # Process images
|
| 58 |
+
|
| 59 |
+
for i, image_path in enumerate(images):
|
| 60 |
+
try:
|
| 61 |
+
image = Image.open(image_path).convert("RGB") # Load original image
|
| 62 |
+
|
| 63 |
+
segmented_mask = segmented_images[i].to(dtype=torch.float32, device="cpu")
|
| 64 |
|
| 65 |
+
segmented_image_pil = transforms.ToPILImage()(segmented_mask) # Convert to PIL Image
|
| 66 |
+
|
| 67 |
+
gallery_data.extend([(image, "Original Image"), (segmented_image_pil, "Segmented Image")])
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error processing image {i}: {e}")
|
| 70 |
+
gallery_data.extend([(image, "Original Image"), (image, f"Error: {e}")])
|
| 71 |
|
| 72 |
return gallery_data
|
| 73 |
|
|
|
|
| 74 |
import gradio as gr
|
| 75 |
|
| 76 |
with gr.Blocks() as demo:
|
|
|
|
| 88 |
fn=update_gallery,
|
| 89 |
inputs=[image_files],
|
| 90 |
outputs=[gallery])
|
| 91 |
+
|
| 92 |
+
with gr.Column():
|
| 93 |
+
example_image = gr.Image(type="filepath", label="MRI Image", visible=False)
|
| 94 |
+
examples = gr.Examples(examples=["examples/Te-me_0194.jpg", "examples/Te-me_0111.jpg",
|
| 95 |
+
"examples/Te-me_0295.jpg", "examples/Te-me_0228.jpg",
|
| 96 |
+
"examples/Te-me_0218.jpg", "examples/Te-me_0164.jpg"],
|
| 97 |
+
inputs=[example_image])
|
| 98 |
+
|
| 99 |
+
with gr.Column(scale=0):
|
| 100 |
+
|
| 101 |
+
example_button = gr.Button("Process Example Image", variant="secondary")
|
| 102 |
+
example_button.click(
|
| 103 |
+
fn=lambda img: update_gallery([img]) if img else [],
|
| 104 |
+
inputs=[example_image],
|
| 105 |
+
outputs=[gallery]
|
| 106 |
+
)
|
| 107 |
|
| 108 |
demo.launch()
|