yownas / sd-webui-faceswapper

Extension to swap faces in Stable Diffusion webui
MIT License
44 stars 8 forks source link

API support #1

Closed shubhamDJ19 closed 1 year ago

shubhamDJ19 commented 1 year ago

Is there any way I can use this extension in the img2img API? Similar to how we include control net scripts in img2img API requests.

yownas commented 1 year ago

Yes. I think. There was a bug I just fixed where the script had the wrong name. But if you add 'face swapper' to alwayson_scripts it should work.

(I'm trying to figure out how the API works, never used it more than a simple txt2img test. I'll post more here if I get anything to work.)

shubhamDJ19 commented 1 year ago

I'm new to all these. Can you give me any particular example of how to add 'face swapper' to alwayson_script? That would be really helpful to me.

yownas commented 1 year ago

I got stuck trying to understand why the webui didn't decode the image sent via the api, until I checked the code and saw that it simply doesn't... anyway, I pushed an update that should fix it.

Here is a small example on how to use it. It loads an image called face.jpg and place it on a super hero. (It uses this library: https://github.com/mix1009/sdwebuiapi)

The "args" to "face swapper" are the booleans; Enabled, Replace original, Restore faces and then the image.

Small warning. This is literary the second attempt I've ever done at using the API, so don't take this as a definite guide on how things can/should work. :)

import webuiapi
from PIL import Image
import base64

# create API client
api = webuiapi.WebUIApi(host='localhost', port=7860)

with open("face.jpg", "rb") as img_file:
    face = base64.b64encode(img_file.read()).decode('utf-8')

result1 = api.txt2img(
        prompt="photo of a cool superhero, funny hair",
        negative_prompt="mask, nsfw",
        steps=25,
        width=512,
        height=768,
        sampler_index="Euler a",
        cfg_scale=7,
        seed=-1,
        alwayson_scripts = {
            'face swapper': {
                    'args': [True, True, True, face],
                }
            }
        )

result1.image.show()
shubhamDJ19 commented 1 year ago

Thank you so much, you really made my day!! It worked 👍

yownas commented 1 year ago

Hi. Just wanted to give a heads up since you were using the API.

I'm changing the image input so it will support masks for some simple editing. But that will break the API.

It mostly works as before with a slight tweak. Instead of sending the face image, you need to send "image" and "mask". But it is ok to set mask to None.

(Edit: I broke it again)

You also need a None or empty string at the end for the "swap rules". I can be left empty, it is only useful if the input image has multiple faces.

        alwayson_scripts = {
            'face swapper': {
                    'args': [True, True, True, {'image': face, 'mask': None}, None],
                }
            }
        )

Hope it doesn't cause too much problems for you.