File size: 9,423 Bytes
e619418
e0e0fed
7c9213f
e0e0fed
 
7938b64
76a890e
e0e0fed
6ca913f
e0e0fed
e9f434e
e0e0fed
4ec1154
76a890e
 
e0e0fed
 
 
 
 
e619418
 
9e26efb
e0e0fed
 
1a05b57
7c9213f
1a05b57
 
 
9e26efb
1a05b57
 
9e26efb
 
1a05b57
e0e0fed
 
b0f5547
 
 
 
 
 
 
 
e619418
 
 
 
 
 
 
 
 
 
7c9213f
e619418
 
 
 
 
 
 
9e26efb
c43da2a
 
 
e619418
 
9e26efb
 
e619418
7c9213f
 
 
 
 
e619418
 
 
 
 
 
 
e0e0fed
 
 
 
b0f5547
 
 
 
 
 
1a05b57
e619418
 
 
 
 
 
e9f434e
e0e0fed
 
38c394b
 
e0e0fed
 
e619418
b0f5547
1a05b57
e619418
 
 
e9f434e
e0e0fed
 
 
 
 
 
00f2e1a
fc051c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0e0fed
 
 
00f2e1a
e0e0fed
 
 
 
 
 
e9f434e
e0e0fed
e1592e8
 
e0e0fed
 
 
 
 
 
da8336d
 
 
 
 
 
e0e0fed
da8336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0e0fed
da8336d
 
 
 
 
 
 
 
 
 
 
2a8bb88
e0e0fed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9f434e
e0e0fed
e1592e8
 
e0e0fed
 
 
 
 
da8336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7938b64
e0e0fed
2a8bb88
e0e0fed
 
 
 
 
7938b64
da8336d
aa7dd73
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import dataclasses as dc
import io
from functools import cache
from typing import Any

import gradio as gr
import pillow_heif
from environs import Env
from finegrain import EditorAPIContext
from gradio_image_annotation import image_annotator
from gradio_imageslider import ImageSlider
from PIL import Image

pillow_heif.register_heif_opener()
pillow_heif.register_avif_opener()

env = Env()
env.read_env()

with env.prefixed("ERASER_"):
    API_USER: str | None = env.str("API_USER")
    API_PASSWORD: str | None = env.str("API_PASSWORD")
    API_URL: str | None = env.str("API_URL", None)
    CA_BUNDLE: str | None = env.str("CA_BUNDLE", None)


@cache
def _ctx() -> EditorAPIContext:
    assert API_USER is not None
    assert API_PASSWORD is not None
    ctx = EditorAPIContext(user=API_USER, password=API_PASSWORD, priority="low")
    if CA_BUNDLE:
        ctx.verify = CA_BUNDLE
    if API_URL:
        ctx.base_url = API_URL
    return ctx


def resize(image: Image.Image, shortest_side: int = 768) -> Image.Image:
    if image.width <= shortest_side and image.height <= shortest_side:
        return image
    if image.width < image.height:
        return image.resize(size=(shortest_side, int(shortest_side * image.height / image.width)))
    return image.resize(size=(int(shortest_side * image.width / image.height), shortest_side))


@dc.dataclass(kw_only=True)
class ProcessParams:
    image: Image.Image
    prompt: str | None = None
    bbox: tuple[int, int, int, int] | None = None


async def _process(ctx: EditorAPIContext, params: ProcessParams) -> Image.Image:
    with io.BytesIO() as f:
        params.image.save(f, format="JPEG")
        response = await ctx.request("POST", "state/upload", files={"file": f})
    st_input = response.json()["state"]

    if params.bbox:
        segment_input_st = st_input
        segment_params = {"bbox": list(params.bbox)}
    else:
        assert params.prompt
        segment_input_st = await ctx.ensure_skill(
            f"infer-bbox/{st_input}",
            {"product_name": params.prompt},
        )
        segment_params = {}

    st_mask = await ctx.ensure_skill(f"segment/{segment_input_st}", segment_params)
    st_erased = await ctx.ensure_skill(f"erase/{st_input}/{st_mask}", {"mode": "free"})

    response = await ctx.request(
        "GET",
        f"state/image/{st_erased}",
        params={"format": "JPEG", "resolution": "DISPLAY"},
    )
    f = io.BytesIO()
    f.write(response.content)
    f.seek(0)
    return Image.open(f)


def process_bbox(prompts: dict[str, Any]) -> tuple[Image.Image, Image.Image]:
    assert isinstance(img := prompts["image"], Image.Image)
    assert isinstance(boxes := prompts["boxes"], list)
    assert len(boxes) == 1
    assert isinstance(box := boxes[0], dict)

    resized_img = resize(img)
    bbox = [box[k] for k in ["xmin", "ymin", "xmax", "ymax"]]
    if resized_img.width != img.width:
        bbox = [int(v * resized_img.width / img.width) for v in bbox]

    output_image = _ctx().run_one_sync(
        _process,
        ProcessParams(
            image=resized_img,
            bbox=(bbox[0], bbox[1], bbox[2], bbox[3]),
        ),
    )
    return (img, output_image)


def on_change_bbox(prompts: dict[str, Any] | None):
    return gr.update(interactive=prompts is not None and len(prompts["boxes"]) > 0)


def process_prompt(img: Image.Image, prompt: str) -> tuple[Image.Image, Image.Image]:
    resized_img = resize(img)
    output_image = _ctx().run_one_sync(
        _process,
        ProcessParams(image=resized_img, prompt=prompt),
    )
    return (img, output_image)


def on_change_prompt(img: Image.Image | None, prompt: str | None):
    return gr.update(interactive=bool(img and prompt))


TITLE = """
<div style="
  background-color: #f08c00;
  color: #212529;
  padding: 0.5rem 1rem;
  font-size: 1.25rem;
" align="center">
  🥳 We're launching our API! It's way faster and more accurate than this space, so check it out!
  <a href="https://finegrain.ai/?utm_source=hf&utm_campaign=object-eraser" target="_blank">Try it now for free</a>
  using our Editor app! 🚀
</div>
<h1>Finegrain Object Eraser</h1>
<p>
  Erase any object from your image just by naming it — no manual work required!
  Not only will the object disappear, but so will its effects on the scene, like shadows or reflections.
</p>
<p>
  <a href="https://discord.gg/zFKg5TjXub" target="_blank">[Discord]</a>
  <a href="https://github.com/finegrain-ai" target="_blank">[GitHub]</a>
</p>
"""

with gr.Blocks() as demo:
    gr.HTML(TITLE)
    with gr.Tab("By prompt", id="tab_prompt"):
        with gr.Row():
            with gr.Column():
                iimg = gr.Image(type="pil", label="Input")
                prompt = gr.Textbox(label="What should we erase?")
            with gr.Column():
                oimg = ImageSlider(label="Output")
        with gr.Row():
            btn = gr.ClearButton(components=[oimg], value="Erase Object", interactive=False)

        for inp in [iimg, prompt]:
            inp.change(
                fn=on_change_prompt,
                inputs=[iimg, prompt],
                outputs=[btn],
            )
        btn.click(
            fn=process_prompt,
            inputs=[iimg, prompt],
            outputs=[oimg],
            api_name=False,
        )

        examples = [
            [
                "examples/white-towels-rattan-basket-white-table-with-bright-room-background.jpg",
                "soap",
            ],
            [
                "examples/interior-decor-with-mirror-potted-plant.jpg",
                "potted plant",
            ],
            [
                "examples/detail-ball-basketball-court-sunset.jpg",
                "basketball",
            ],
            [
                "examples/still-life-device-table_23-2150994394.jpg",
                "glass of water",
            ],
            [
                "examples/knife-fork-green-checkered-napkin_140725-63576.jpg",
                "knife and fork",
            ],
            [
                "examples/city-night-with-architecture-vibrant-lights_23-2149836930.jpg",
                "frontmost black car on right lane",
            ],
            [
                "examples/close-up-coffee-latte-wooden-table_23-2147893063.jpg",
                "coffee cup on plate",
            ],
            [
                "examples/empty-chair-with-vase-plant_74190-2078.jpg",
                "chair",
            ],
        ]

        ex = gr.Examples(
            examples=examples,
            inputs=[iimg, prompt],
            outputs=[oimg],
            fn=process_prompt,
            cache_examples=True,
        )
    with gr.Tab("By bounding box", id="tab_bb"):
        with gr.Row():
            with gr.Column():
                annotator = image_annotator(
                    image_type="pil",
                    disable_edit_boxes=True,
                    show_download_button=False,
                    show_share_button=False,
                    single_box=True,
                    label="Input",
                )
            with gr.Column():
                oimg = ImageSlider(label="Output")
        with gr.Row():
            btn = gr.ClearButton(components=[oimg], value="Erase Object", interactive=False)

        annotator.change(
            fn=on_change_bbox,
            inputs=[annotator],
            outputs=[btn],
        )
        btn.click(
            fn=process_bbox,
            inputs=[annotator],
            outputs=[oimg],
            api_name=False,
        )

        examples = [
            {
                "image": "examples/white-towels-rattan-basket-white-table-with-bright-room-background.jpg",
                "boxes": [{"xmin": 836, "ymin": 475, "xmax": 1125, "ymax": 1013}],
            },
            {
                "image": "examples/interior-decor-with-mirror-potted-plant.jpg",
                "boxes": [{"xmin": 47, "ymin": 907, "xmax": 397, "ymax": 1633}],
            },
            {
                "image": "examples/detail-ball-basketball-court-sunset.jpg",
                "boxes": [{"xmin": 673, "ymin": 954, "xmax": 911, "ymax": 1186}],
            },
            {
                "image": "examples/still-life-device-table_23-2150994394.jpg",
                "boxes": [{"xmin": 429, "ymin": 586, "xmax": 571, "ymax": 834}],
            },
            {
                "image": "examples/knife-fork-green-checkered-napkin_140725-63576.jpg",
                "boxes": [{"xmin": 972, "ymin": 226, "xmax": 1092, "ymax": 1023}],
            },
            {
                "image": "examples/city-night-with-architecture-vibrant-lights_23-2149836930.jpg",
                "boxes": [{"xmin": 215, "ymin": 637, "xmax": 411, "ymax": 855}],
            },
            {
                "image": "examples/close-up-coffee-latte-wooden-table_23-2147893063.jpg",
                "boxes": [{"xmin": 255, "ymin": 456, "xmax": 1080, "ymax": 1064}],
            },
            {
                "image": "examples/empty-chair-with-vase-plant_74190-2078.jpg",
                "boxes": [{"xmin": 35, "ymin": 320, "xmax": 383, "ymax": 983}],
            },
        ]

        ex = gr.Examples(
            examples=examples,
            inputs=[annotator],
            outputs=[oimg],
            fn=process_bbox,
            cache_examples=True,
        )

demo.queue(max_size=30, api_open=False)
demo.launch(show_api=False)