Spaces:
Runtime error
Runtime error
liangfeng
commited on
Commit
·
e66e6e4
1
Parent(s):
583456e
update
Browse files
app.py
CHANGED
|
@@ -1,7 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates.
|
| 2 |
+
# Copyright (c) Meta Platforms, Inc. All Rights Reserved
|
| 3 |
+
|
| 4 |
+
import multiprocessing as mp
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
from detectron2.config import get_cfg
|
| 10 |
+
|
| 11 |
+
from detectron2.projects.deeplab import add_deeplab_config
|
| 12 |
+
from detectron2.data.detection_utils import read_image
|
| 13 |
+
from open_vocab_seg import add_ovseg_config
|
| 14 |
+
from open_vocab_seg.utils import VisualizationDemo
|
| 15 |
+
|
| 16 |
import gradio as gr
|
| 17 |
|
| 18 |
+
def setup_cfg(config_file):
|
| 19 |
+
# load config from file and command-line arguments
|
| 20 |
+
cfg = get_cfg()
|
| 21 |
+
add_deeplab_config(cfg)
|
| 22 |
+
add_ovseg_config(cfg)
|
| 23 |
+
cfg.merge_from_file(config_file)
|
| 24 |
+
cfg.freeze()
|
| 25 |
+
return cfg
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def inference(class_names, input_img):
|
| 29 |
+
mp.set_start_method("spawn", force=True)
|
| 30 |
+
config_file = './configs/ovseg_swinB_vitL_demo.yaml'
|
| 31 |
+
cfg = setup_cfg(config_file)
|
| 32 |
+
|
| 33 |
+
demo = VisualizationDemo(cfg)
|
| 34 |
+
|
| 35 |
+
class_names = class_names.split(',')
|
| 36 |
+
img = read_image(input_img, format="BGR")
|
| 37 |
+
_, visualized_output = demo.run_on_image(img, class_names)
|
| 38 |
+
|
| 39 |
+
return Image.fromarray(np.uint8(visualized_output.get_image())).convert('RGB')
|
| 40 |
+
|
| 41 |
+
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 42 |
+
# demo.launch()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
examples = [['Oculus, Ukulele', './resources/demo_samples/sample_03.jpeg'],]
|
| 46 |
+
output_labels = ['segmentation map']
|
| 47 |
+
|
| 48 |
+
title = 'OVSeg'
|
| 49 |
+
|
| 50 |
+
description = """
|
| 51 |
+
Gradio Demo for Open-Vocabulary Semantic Segmentation with Mask-adapted CLIP \n
|
| 52 |
+
You may click on of the examples or upload your own image. \n
|
| 53 |
+
OVSeg could perform open vocabulary segmentation, you may input more classes (seperate by comma).
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
article = """
|
| 57 |
+
<p style='text-align: center'>
|
| 58 |
+
<a href='https://arxiv.org/abs/2210.04150' target='_blank'>
|
| 59 |
+
Open-Vocabulary Semantic Segmentation with Mask-adapted CLIP
|
| 60 |
+
</a>
|
| 61 |
+
|
|
| 62 |
+
<a href='https://github.com' target='_blank'>Github Repo</a></p>
|
| 63 |
+
"""
|
| 64 |
|
| 65 |
+
gr.Interface(
|
| 66 |
+
inference,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.inputs.Textbox(
|
| 69 |
+
lines=1, placeholder=None, default='', label='class names'),
|
| 70 |
+
gr.inputs.Image(type='filepath')
|
| 71 |
+
],
|
| 72 |
+
outputs=gr.outputs.Image(label='segmentation map'),
|
| 73 |
+
title=title,
|
| 74 |
+
description=description,
|
| 75 |
+
article=article,
|
| 76 |
+
examples=examples).launch(enable_queue=True)
|