sd-webui-roop icon indicating copy to clipboard operation
sd-webui-roop copied to clipboard

[Feature] Add api Support

Open saaldigital-cw opened this issue 2 years ago • 14 comments

Hi like others here I hope you also add api support… this would be great

saaldigital-cw avatar Jun 24 '23 06:06 saaldigital-cw

I came for this! :)

charlesmccarthy avatar Jun 24 '23 20:06 charlesmccarthy

in urgent need of API support

2shou avatar Jun 25 '23 04:06 2shou

Same here. There was another issue mentioning that there is a way to make it work but doesn't mention the request body for it

mtzortzis avatar Jun 25 '23 12:06 mtzortzis

which issue? @MichaelTzortzis

2shou avatar Jun 25 '23 12:06 2shou

#17

mtzortzis avatar Jun 25 '23 12:06 mtzortzis

I finally got through, using the method that #17 mentioned. Here is how

import base64
import io
import requests

address = 'http://127.0.0.1:7860'
image_file = "path/to/local/image/file"
im = Image.open(image_file)

img_bytes = io.BytesIO()
im.save(img_bytes, format='PNG') 
img_base64 = base64.b64encode(img_bytes.getvalue()).decode('utf-8')

# the most important is this one
args=[img_base64,True,'0','F:\stable-diffusion-webui\models/roop\inswapper_128.onnx','CodeFormer',1,None,1,'None',False,True]

# The args for roop can be found by 
# requests.get(url=f'{address}/sdapi/v1/script-info')

prompt = "(8k, best quality, masterpiece, ultra highres:1.2),Realistic image style,Vertical orientation, Girl,White short hair,Shoulder-length, tousled White blossom pink hair,Clothing"
neg = "ng_deepnegative_v1_75t, (worst quality:2), (low quality:2), (normal quality:2), lowres, bad anatomy, normal quality, ((monochrome)), ((grayscale)), (verybadimagenegative_v1.3:0.8), negative_hand-neg, (lamp), badhandv4"
payload = {
    "prompt": prompt,
    "negative_prompt": neg,
    "seed": -1,
    "sampler_name": "DPM++ SDE Karras",
    "steps": 20,
    "cfg_scale": 7,
    "width": 512,
    "height": 768,
    "restore_faces": True,
    #"script_name":"roop", # this doesn't work
    #"script_args":args, # this doesn't work
    "alwayson_scripts": {"roop":{"args":args}}  # This is the args.
}
result = requests.post(url=f'{address}/sdapi/v1/txt2img', json=payload)

yeeyou avatar Jun 25 '23 17:06 yeeyou

@yeeyou finally a request payload. I have actually tried this already and even though no errors showed up I didn't seem to affect the generated image. Will give it a retry though

mtzortzis avatar Jun 25 '23 18:06 mtzortzis

Perfect ...thanks for the effort

saaldigital-cw avatar Jun 26 '23 05:06 saaldigital-cw

according to faceswap.py (line:91~line:103)

        return [
            img,
            enable,
            faces_index,
            model,
            face_restorer_name,
            face_restorer_visibility,
            upscaler_name,
            upscaler_scale,
            upscaler_visibility,
            swap_in_source,
            swap_in_generated,
        ]

the params of roop in request payload could be

args = [img_base64, True, '0', 'path/to/models/roop/inswapper_128.onnx', 'CodeFormer', 1, 'None', 1, 1, False, True]

jovijovi avatar Jun 26 '23 06:06 jovijovi

okay so it still isn't working for me through the api when sent from c#, and i have copied the structure from

I finally got through, using the method that #17 mentioned. Here is how

import base64
import io
import requests

address = 'http://127.0.0.1:7860'
image_file = "path/to/local/image/file"
im = Image.open(image_file)

img_bytes = io.BytesIO()
im.save(img_bytes, format='PNG') 
img_base64 = base64.b64encode(img_bytes.getvalue()).decode('utf-8')

# the most important is this one
args=[img_base64,True,'0','F:\stable-diffusion-webui\models/roop\inswapper_128.onnx','CodeFormer',1,None,1,'None',False,True]

# The args for roop can be found by 
# requests.get(url=f'{address}/sdapi/v1/script-info')

prompt = "(8k, best quality, masterpiece, ultra highres:1.2),Realistic image style,Vertical orientation, Girl,White short hair,Shoulder-length, tousled White blossom pink hair,Clothing"
neg = "ng_deepnegative_v1_75t, (worst quality:2), (low quality:2), (normal quality:2), lowres, bad anatomy, normal quality, ((monochrome)), ((grayscale)), (verybadimagenegative_v1.3:0.8), negative_hand-neg, (lamp), badhandv4"
payload = {
    "prompt": prompt,
    "negative_prompt": neg,
    "seed": -1,
    "sampler_name": "DPM++ SDE Karras",
    "steps": 20,
    "cfg_scale": 7,
    "width": 512,
    "height": 768,
    "restore_faces": True,
    #"script_name":"roop", # this doesn't work
    #"script_args":args, # this doesn't work
    "alwayson_scripts": {"roop":{"args":args}}  # This is the args.
}
result = requests.post(url=f'{address}/sdapi/v1/txt2img', json=payload)

does anyone have any idea? it seems to just ignore it as an extension and never enable it

mtzortzis avatar Jun 26 '23 16:06 mtzortzis

I am able to use it like this (just using default params) payload = {

      "denoising_strength": item["StableDiffusionParams"]["Strength"],   
      "prompt": item["Prompt"],    
      "seed": item["StableDiffusionParams"]["Seed"],  
      "batch_size": 1,
      "n_iter": 1,
      "steps":  item["StableDiffusionParams"]["Steps"],
      "cfg_scale":  item["StableDiffusionParams"]["Scale"],
      "width": width,
      "height": height,
      "restore_faces":  item["StableDiffusionParams"]["Restorefaces"],
      "tiling": False,
      "negative_prompt":  item["PromptNegative"],   
      "override_settings_restore_afterwards": override_settings_restore_afterwards,   
      "sampler_index": item["StableDiffusionParams"]["Scheduler"],
      "include_init_images": True,
     
   }
# Roop - face swap
if item["ProcessingQueueActionType"]==17:
    if item["StableDiffusionParams"]["Roop"]["Active"]==True:
        srcimage=item["StableDiffusionParams"]["Roop"]["SourceImage"]
        responseSubRoop = requests.get(srcimage)
        image_contentSubRoop = responseSubRoop.content
        **base64_dataSub = base64.b64encode(image_contentSubRoop).decode('utf-8')    

        new_entry = {
            "roop": {
                    "args": [
                        base64_dataSub,
                        True                           
                    ]
                }
        }  
        payload["alwayson_scripts"].update(new_entry) # Update alwayson_scripts**

try:
    response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
    r = response.json()
    if "images" in r:
        images=[]
        for i in r['images']:        
            imagebytes=io.BytesIO(base64.b64decode(i.split(",",1)[0]))
            if item["ImageEnhancementParams"]["IsrScale"]==0:
                image = Image.open(imagebytes)
            else:
                image=upscaleOnly(item,imagebytes.getvalue())
            item["WidthNew"]=image.width
            item["HeightNew"]=image.height     
            images.append(image)      
            break # Stop after first image
        return images
    else:
        aiservices_helper.log(str(r))  

saaldigital-cw avatar Jun 27 '23 06:06 saaldigital-cw

Hello, wondering whether this can be done directly using API without writing wrapper?

If need to write wrapper, please do let me know, and I can assist. :)

afeezaziz avatar Jul 03 '23 08:07 afeezaziz

根据faceswap.py(第91行~第103行)

        return [
            img,
            enable,
            faces_index,
            model,
            face_restorer_name,
            face_restorer_visibility,
            upscaler_name,
            upscaler_scale,
            upscaler_visibility,
            swap_in_source,
            swap_in_generated,
        ]

请求有效负载中的 roop 参数可以是

args = [img_base64, True, '0', 'path/to/models/roop/inswapper_128.onnx', 'CodeFormer', 1, 'None', 1, 1, False, True]

tanks, it is useful

coderzhoucf avatar Jul 13 '23 16:07 coderzhoucf

I have added support for the standard API in #214 . You can use my fork until it is merged by @s0md3v

SpenserCai avatar Aug 20 '23 15:08 SpenserCai