Thiago Hersan
update title
15625a2
raw
history blame
2.9 kB
import gradio as gr
import numpy as np
from transformers import MaskFormerFeatureExtractor, MaskFormerForInstanceSegmentation
# feature_extractor = MaskFormerFeatureExtractor.from_pretrained("facebook/maskformer-swin-tiny-coco")
# model = MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-tiny-coco")
feature_extractor = MaskFormerFeatureExtractor.from_pretrained("facebook/maskformer-swin-large-coco")
model = MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-large-coco")
def visualize_instance_seg_mask(img_in, mask, id2label):
img_out = np.zeros((mask.shape[0], mask.shape[1], 3))
image_total_pixels = mask.shape[0] * mask.shape[1]
label_ids = np.unique(mask)
vegetation_labels = ["tree-merged", "grass-merged"]
def get_color(id):
id_color = (np.random.randint(0, 2), np.random.randint(0, 4), np.random.randint(0, 256))
if id2label[id] in vegetation_labels:
id_color = (0, 140, 0)
return id_color
id2color = {id: get_color(id) for id in label_ids}
id2count = {id: 0 for id in label_ids}
for i in range(img_out.shape[0]):
for j in range(img_out.shape[1]):
img_out[i, j, :] = id2color[mask[i, j]]
id2count[mask[i, j]] = id2count[mask[i, j]] + 1
image_res = (0.5 * img_in + 0.5 * img_out) / 255
vegetation_count = sum([id2count[id] for id in label_ids if id2label[id] in vegetation_labels])
dataframe_vegetation_items = [[
f"{id2label[id]}",
f"{(100 * id2count[id] / image_total_pixels):.2f} %",
f"{np.sqrt(id2count[id] / image_total_pixels):.2f} m"
] for id in label_ids if id2label[id] in vegetation_labels]
dataframe_all_items = [[
f"{id2label[id]}",
f"{(100 * id2count[id] / image_total_pixels):.2f} %",
f"{np.sqrt(id2count[id] / image_total_pixels):.2f} m"
] for id in label_ids]
dataframe_vegetation_total = [[
f"vegetation",
f"{(100 * vegetation_count / image_total_pixels):.2f} %",
f"{np.sqrt(vegetation_count / image_total_pixels):.2f} m"]]
return image_res, dataframe_vegetation_total
def query_image(img):
img_size = (img.shape[0], img.shape[1])
inputs = feature_extractor(images=img, return_tensors="pt")
outputs = model(**inputs)
results = feature_extractor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]
results = visualize_instance_seg_mask(img, results.numpy(), model.config.id2label)
return results
demo = gr.Interface(
query_image,
inputs=[gr.Image(label="Input Image")],
outputs=[
gr.Image(label="Vegetation"),
gr.DataFrame(label="Info", headers=["Object Label", "Pixel Percent", "Square Length"])
],
title="Maskformer (large-coco)",
allow_flagging="never",
analytics_enabled=None
)
demo.launch(show_api=False)