Spaces:
Sleeping
Sleeping
import os | |
os.system('python -m pip install \'git+https://github.com/facebookresearch/detectron2.git\'') | |
import cv2 | |
import gradio as gr | |
from detectron2.config import get_cfg | |
from detectron2 import model_zoo | |
from detectron2.engine import DefaultPredictor | |
from detectron2.utils.visualizer import Visualizer | |
from detectron2.data import MetadataCatalog | |
def predict(input_image): | |
# Initialise model | |
cfg = get_cfg() | |
cfg.merge_from_file(model_zoo.get_config_file("COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml")) | |
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml") | |
cfg.MODEL.DEVICE = "cpu" | |
predictor = DefaultPredictor(cfg) | |
assert input_image.shape[2] == 3 | |
height, width, _ = input_image.shape | |
# Apply Panoptic segmentation | |
panoptic_seg, segments_info = predictor(input_image)["panoptic_seg"] | |
v = Visualizer(input_image[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2) | |
out = v.draw_panoptic_seg_predictions(panoptic_seg.to("cpu"), segments_info) | |
segmented_image = out.get_image()[:, :, ::-1] | |
# Resize image if required | |
if not segmented_image.shape[:2] == (height, width): | |
segmented_image = cv2.resize(segmented_image, (height, width)) | |
# Combine the segmented and original image | |
combined_image = cv2.hconcat([segmented_image, input_image]) | |
return combined_image | |
# Create Gradio interface | |
image_input = gr.Image(type="pil", label="Input Image") | |
iface = gr.Interface(fn=predict, | |
inputs=[image_input], | |
outputs=gr.Image(type="pil"), | |
examples=[["examples/trump-fake.jpeg"]]) | |
iface.launch() |