Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import matplotlib.patches as mpatches
|
| 6 |
+
from matplotlib import cm
|
| 7 |
+
import torch
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import requests
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def replace(text):
|
| 14 |
+
processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-base-coco-panoptic")
|
| 15 |
+
model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-base-coco-panoptic")
|
| 16 |
+
# image = Image.open(text).convert("RGB")
|
| 17 |
+
inputs = processor(text, return_tensors="pt")
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
prediction = processor.post_process_panoptic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|
| 20 |
+
|
| 21 |
+
return draw_panoptic_segmentation(**prediction)
|
| 22 |
+
def draw_panoptic_segmentation(segmentation, segments_info):
|
| 23 |
+
# get the used color map
|
| 24 |
+
viridis = cm.get_cmap('viridis', torch.max(segmentation))
|
| 25 |
+
fig, ax = plt.subplots()
|
| 26 |
+
ax.imshow(segmentation)
|
| 27 |
+
instances_counter = defaultdict(int)
|
| 28 |
+
handles = []
|
| 29 |
+
# for each segment, draw its legend
|
| 30 |
+
for segment in segments_info:
|
| 31 |
+
segment_id = segment['id']
|
| 32 |
+
segment_label_id = segment['label_id']
|
| 33 |
+
segment_label = model.config.id2label[segment_label_id]
|
| 34 |
+
label = f"{segment_label}-{instances_counter[segment_label_id]}"
|
| 35 |
+
instances_counter[segment_label_id] += 1
|
| 36 |
+
color = viridis(segment_id)
|
| 37 |
+
handles.append(mpatches.Patch(color=color, label=label))
|
| 38 |
+
|
| 39 |
+
ax.legend(handles=handles)
|
| 40 |
+
|
| 41 |
+
# Save the figure to a buffer and convert it to a PIL image
|
| 42 |
+
buf = BytesIO()
|
| 43 |
+
plt.savefig(buf, format='png')
|
| 44 |
+
buf.seek(0)
|
| 45 |
+
plt.close(fig) # Close the figure to free memory
|
| 46 |
+
|
| 47 |
+
pil_image = Image.open(buf)
|
| 48 |
+
return pil_image
|
| 49 |
+
# Set up the Gradio interface with updated syntax
|
| 50 |
+
interface = gr.Interface(
|
| 51 |
+
fn=replace, # The function to execute
|
| 52 |
+
inputs=gr.Image(type="pil"), # Input type as PIL image
|
| 53 |
+
outputs="image", # Output type as an image
|
| 54 |
+
title="Image Segmentation with Mask Overlay", # Title for the Gradio app
|
| 55 |
+
description="Upload an image to see the segmentation mask applied." # Description for the app
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Launch the Gradio app
|
| 59 |
+
interface.launch(debug=True)
|