Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load your dataset only once
|
| 6 |
+
ds = load_dataset("Devenarya/Microsoft100", split="train")
|
| 7 |
+
|
| 8 |
+
# Generator as before
|
| 9 |
+
def get_image_and_mask(index):
|
| 10 |
+
# This assumes dataset alternates: image, mask, image, mask, ...
|
| 11 |
+
images = [item['image'] for item in ds]
|
| 12 |
+
img = images[2 * index].convert('RGB')
|
| 13 |
+
mask = images[2 * index + 1].convert('L')
|
| 14 |
+
return np.array(img), np.array(mask)
|
| 15 |
+
|
| 16 |
+
def demo_fn(index):
|
| 17 |
+
# Output two images: the photo and its segmentation
|
| 18 |
+
return get_image_and_mask(index)
|
| 19 |
+
|
| 20 |
+
demo = gr.Interface(
|
| 21 |
+
fn=demo_fn,
|
| 22 |
+
inputs=gr.Slider(0, (len(ds)//2)-1, step=1, label="Image Index"),
|
| 23 |
+
outputs=[gr.Image(label="Original Image"), gr.Image(label="Segmentation Mask")],
|
| 24 |
+
title="Microsoft100 Image & Segmentation Explorer"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
demo.launch()
|