File size: 1,777 Bytes
65a3955
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import warnings

import gradio as gr
import numpy as np
from PIL import Image

from lang_efficient_sam.LangEfficientSAM import LangEfficientSAM
from lang_efficient_sam.utils.draw_image import draw_image

warnings.filterwarnings("ignore")

model = LangEfficientSAM()


def predict(box_threshold, text_threshold, image_path, text_prompt):
    print("Predicting... ", box_threshold, text_threshold, image_path, text_prompt)

    image_pil = Image.open(image_path).convert("RGB")

    masks, boxes, phrases, logits = model.predict(image_pil, text_prompt, box_threshold, text_threshold)

    labels = [f"{phrase} {logit:.2f}" for phrase, logit in zip(phrases, logits)]

    image_array = np.asarray(image_pil)
    image = draw_image(image_array, masks, boxes, labels)
    image = Image.fromarray(np.uint8(image)).convert("RGB")

    return image


title = "LangEfficientSAM"

inputs = [
    gr.Slider(0, 1, value=0.3, label="Box threshold"),
    gr.Slider(0, 1, value=0.25, label="Text threshold"),
    gr.Image(type="filepath", label='Image'),
    gr.Textbox(lines=1, label="Text Prompt"),
]

outputs = [gr.Image(type="pil", label="Output Image")]

examples = [
    [
        0.20,
        0.20,
        os.path.join(os.path.dirname(__file__), "images", "living.jpg"),
        "fabric",
    ],
    [
        0.36,
        0.25,
        os.path.join(os.path.dirname(__file__), "images", "fruits.jpg"),
        "apple",
    ],
    [
        0.20,
        0.20,
        os.path.join(os.path.dirname(__file__), "images", "street.jpg"),
        "car",
    ]
]

demo = gr.Interface(fn=predict,
                    inputs=inputs,
                    outputs=outputs,
                    examples=examples,
                    title=title)

demo.launch(debug=False, share=False)