File size: 3,340 Bytes
ff605cf
b808c95
ff605cf
b808c95
ff605cf
 
b808c95
ff605cf
 
 
91feba4
ff605cf
 
 
5c7957c
ff605cf
 
 
91feba4
b808c95
ff605cf
 
5c7957c
ff605cf
 
 
 
 
b808c95
ff605cf
b808c95
ff605cf
91feba4
b808c95
ff605cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ad7527
b808c95
 
0ad7527
b808c95
ff605cf
 
 
 
 
b808c95
 
 
 
 
 
 
 
 
 
 
 
 
 
ff605cf
 
b808c95
 
 
ff605cf
b808c95
 
ff605cf
 
b808c95
ff605cf
b808c95
ff605cf
b808c95
ff605cf
b808c95
 
 
 
 
 
 
 
 
 
ff605cf
b808c95
 
 
 
 
 
 
 
 
ff605cf
 
 
 
 
 
 
 
 
 
 
0ad7527
ff605cf
 
b808c95
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import gradio as gr
import ast
import cv2
import torch
import traceback
import numpy as np
from itertools import chain
from transformers import SamModel, SamProcessor


model = SamModel.from_pretrained('facebook/sam-vit-huge')
processor = SamProcessor.from_pretrained('facebook/sam-vit-huge')


def set_predictor(image):
    """
    Creates a Sam predictor object based on a given image and model.
    """
    device = 'cpu'
    inputs = processor(image, return_tensors='pt').to(device)
    image_embedding = model.get_image_embeddings(inputs['pixel_values'])

    return [image, image_embedding, 'Done']


def get_polygon(points, image, image_embedding):
    """
    Returns the points of the polygon given a bounding box and a prediction
    made by Sam.
    """
    points = list(chain.from_iterable(points))

    device = 'cpu'
    inputs = processor(image, input_boxes=[points], return_tensors="pt").to(device)

    # pop the pixel_values as they are not neded
    inputs.pop("pixel_values", None)
    inputs.update({"image_embeddings": image_embedding})

    with torch.no_grad():
        outputs = model(**inputs)

    masks = processor.image_processor.post_process_masks(
        outputs.pred_masks.cpu(), 
        inputs["original_sizes"].cpu(), 
        inputs["reshaped_input_sizes"].cpu()
    )

    mask = masks[0].squeeze().numpy()
    img = mask.astype(np.uint8)[0]
    contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    if len(contours) == 0:
        return [], img

    points = contours[0]

    polygon = []
    for point in points:
        for x, y in point:
            polygon.append([int(x), int(y)])

    mask = np.zeros(image.shape, dtype='uint8')
    poly = np.array(polygon)
    cv2.fillPoly(mask, [poly], (0, 255, 0))

    return polygon, mask


def add_bbox(bbox, evt: gr.SelectData):
    if bbox[0] == [0, 0]:
        bbox[0] = [evt.index[0], evt.index[1]]
        return bbox, bbox

    bbox[1] = [evt.index[0], evt.index[1]]
    return bbox, bbox


def clear_bbox(bbox):
    updated_bbox = [[0, 0], [0, 0]]
    return updated_bbox, updated_bbox


with gr.Blocks() as demo:
    image = gr.State()
    embedding = gr.State()
    bbox = gr.State([[0, 0], [0, 0]])

    with gr.Row():
        input_image = gr.Image(label='Image')
        mask = gr.Image(label='Mask')

    with gr.Row():
        with gr.Column():
            output_status = gr.Textbox(label='Status')
            
        with gr.Column():            
            predictor_button = gr.Button('Send Image')
 
    with gr.Row():
        with gr.Column():
            bbox_box = gr.Textbox(label="bbox")

        with gr.Column():
            bbox_button = gr.Button('Clear bbox')

    with gr.Row():
        with gr.Column():
            polygon = gr.Textbox(label='Polygon')

        with gr.Column():
            points_button = gr.Button('Send bounding box')


    predictor_button.click(
        set_predictor, 
        input_image,
        [image, embedding, output_status],
    )

    points_button.click(
        get_polygon, 
        [bbox, image, embedding],
        [polygon, mask],
    )

    bbox_button.click(
        clear_bbox, 
        bbox,
        [bbox, bbox_box],
    )    

    input_image.select(
        add_bbox,
        bbox,
        [bbox, bbox_box]
    )


demo.launch(debug=True)