File size: 1,581 Bytes
80aa6a1
811bcfe
14cacb2
811bcfe
 
01e150d
 
83e2c83
01e150d
 
 
 
 
 
 
 
c1cce1c
80aa6a1
83e2c83
c1cce1c
01e150d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80aa6a1
01e150d
 
 
 
 
 
 
c1cce1c
 
14cacb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import datetime
import os

os.system('python -m pip install \'git+https://github.com/facebookresearch/detectron2.git\'')

import cv2
import gradio as gr
import numpy as np

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(image):
    print(datetime.datetime.now())
    # Convert PIL Image to numpy array
    input_image = cv2.imread(image.name)
    # 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]

    return segmented_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/6178_3003_0.jpg"])
iface.launch()