[GH-ISSUE #20] What is the API for sd webui? #15

Open
opened 2026-10-30 21:08:22 +00:00 by bsakaguchi · 4 comments
Owner

Originally created by @zhuchenye on GitHub (May 23, 2023).
Original GitHub issue: https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg/issues/20

I want to use the API on webui server with the payload as :
payload = {
"image": encode_pil_to_base64(image),
"model": "u2net",
"return_mask": False,
"alpha_matting": False,
"alpha_matting_foreground_threshold": 240,
"alpha_matting_background_threshold": 10,
"alpha_matting_erode_size": 10,
}
but it dos not work.

Is there any docs for remby API?

Originally created by @zhuchenye on GitHub (May 23, 2023). Original GitHub issue: https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg/issues/20 I want to use the API on webui server with the payload as : payload = { "image": encode_pil_to_base64(image), "model": "u2net", "return_mask": False, "alpha_matting": False, "alpha_matting_foreground_threshold": 240, "alpha_matting_background_threshold": 10, "alpha_matting_erode_size": 10, } but it dos not work. Is there any docs for remby API?
Author
Owner

@Theroniya commented on GitHub (May 23, 2023):

Me too. As a workaround I installed rembg standalone. But why load and wait every time when it is already loaded on the sd server anyway?

<!-- gh-comment-id:1559692518 --> @Theroniya commented on GitHub (May 23, 2023): Me too. As a workaround I installed rembg standalone. But why load and wait every time when it is already loaded on the sd server anyway?
Author
Owner

@zhuchenye commented on GitHub (May 24, 2023):

I found there is no API route for rembg. So if I used extra_single_image interface, the rembg params will be none.

<!-- gh-comment-id:1560320601 --> @zhuchenye commented on GitHub (May 24, 2023): I found there is no API route for rembg. So if I used extra_single_image interface, the rembg params will be none.
Author
Owner

@hubcart commented on GitHub (May 25, 2023):

It used to work up until a couple weeks ago. I think an update on the webui side broke it.
I still have it working on our older setups but any new setups are broke. when working, you have to put postprocessing_rembg_ before each parameter (see pic)

Screenshot 2023-05-25 135050

That setup 100% was working up until about a week or two ago, so if anyone wants to play with that to see what you can find, please post back here if you solve it.

<!-- gh-comment-id:1563493636 --> @hubcart commented on GitHub (May 25, 2023): It used to work up until a couple weeks ago. I think an update on the webui side broke it. I still have it working on our older setups but any new setups are broke. when working, you have to put postprocessing_rembg_ before each parameter (see pic) <img width="436" alt="Screenshot 2023-05-25 135050" src="https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg/assets/131904991/905df97c-43f2-4def-950b-4dfaee097aff"> That setup 100% was working up until about a week or two ago, so if anyone wants to play with that to see what you can find, please post back here if you solve it.
Author
Owner

@ccfco commented on GitHub (Jun 2, 2023):

One alternative approach is to create a new file called api.py in the scripts folder of the plugin.

# api.py
from fastapi import FastAPI, Body

from modules.api.models import *
from modules.api import api
import gradio as gr

import rembg

def rembg_api(_: gr.Blocks, app: FastAPI):
    @app.post("/rembg")
    async def rembg_remove(
        input_image: str = Body("", title='rembg input image'),
        model: str = Body("u2net", title='rembg model'), 
        return_mask: bool = Body(False, title='return mask'), 
        alpha_matting: bool = Body(False, title='alpha matting'), 
        alpha_matting_foreground_threshold: int = Body(240, title='alpha matting foreground threshold'), 
        alpha_matting_background_threshold: int = Body(10, title='alpha matting background threshold'), 
        alpha_matting_erode_size: int = Body(10, title='alpha matting erode size')
    ):
        if not model or model == "None":
            return

        input_image = api.decode_base64_to_image(input_image)

        image = rembg.remove(
            input_image,
            session=rembg.new_session(model),
            only_mask=return_mask,
            alpha_matting=alpha_matting,
            alpha_matting_foreground_threshold=alpha_matting_foreground_threshold,
            alpha_matting_background_threshold=alpha_matting_background_threshold,
            alpha_matting_erode_size=alpha_matting_erode_size,
        )

        return {"image": api.encode_pil_to_base64(image).decode("utf-8")}

try:
    import modules.script_callbacks as script_callbacks

    script_callbacks.on_app_started(rembg_api)
except:
    pass
image
<!-- gh-comment-id:1573024144 --> @ccfco commented on GitHub (Jun 2, 2023): One alternative approach is to create a new file called `api.py` in the scripts folder of the plugin. ``` # api.py from fastapi import FastAPI, Body from modules.api.models import * from modules.api import api import gradio as gr import rembg def rembg_api(_: gr.Blocks, app: FastAPI): @app.post("/rembg") async def rembg_remove( input_image: str = Body("", title='rembg input image'), model: str = Body("u2net", title='rembg model'), return_mask: bool = Body(False, title='return mask'), alpha_matting: bool = Body(False, title='alpha matting'), alpha_matting_foreground_threshold: int = Body(240, title='alpha matting foreground threshold'), alpha_matting_background_threshold: int = Body(10, title='alpha matting background threshold'), alpha_matting_erode_size: int = Body(10, title='alpha matting erode size') ): if not model or model == "None": return input_image = api.decode_base64_to_image(input_image) image = rembg.remove( input_image, session=rembg.new_session(model), only_mask=return_mask, alpha_matting=alpha_matting, alpha_matting_foreground_threshold=alpha_matting_foreground_threshold, alpha_matting_background_threshold=alpha_matting_background_threshold, alpha_matting_erode_size=alpha_matting_erode_size, ) return {"image": api.encode_pil_to_base64(image).decode("utf-8")} try: import modules.script_callbacks as script_callbacks script_callbacks.on_app_started(rembg_api) except: pass ``` <img width="1411" alt="image" src="https://github.com/AUTOMATIC1111/stable-diffusion-webui-rembg/assets/54824048/7a5241d1-c13c-47bb-b131-a8f82a03cd97">
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
AUTOMATIC1111/stable-diffusion-webui-rembg#15
No description provided.