sdapi/v1/img2img or txt2img api alwayson_scripts parameter "Batch Sources Images" support base64 images #105

Open
opened 2023-09-12 11:42:17 +02:00 by IAn2018cs · 7 comments
IAn2018cs commented 2023-09-12 11:42:17 +02:00 (Migrated from github.com)

like this:
image

I modified this file in the source code to achieve this function:
sd-webui-faceswaplab/scripts/faceswaplab_ui/faceswaplab_unit_settings.py

    @property
    def faces(self) -> List[Face]:
        """_summary_
        Extract all faces (including reference face) to provide an array of faces
        Only processed once.
        """
        if self.batch_files is not None and not hasattr(self, "_faces"):
            self._faces = (
                [self.reference_face] if self.reference_face is not None else []
            )
            # ===> begin change <===
            files = self.batch_files
            if isinstance(self.batch_files, str):
                batchs = []
                base64_batch_files = self.batch_files.split(',')
                for base64str in base64_batch_files:
                    batchs.append(api_utils.base64_to_pil(base64str))
                files = batchs
            for file in files:
            # ===> end change <===
                if isinstance(file, Image.Image):
                    img = file
                else:
                    img = Image.open(file.name)  # type: ignore

                face = swapper.get_or_default(
                    swapper.get_faces(pil_to_cv2(img)), 0, None
                )
                if face is not None:
                    self._faces.append(face)
        return self._faces

Hope it can be adopted

like this: <img width="1105" alt="image" src="https://github.com/glucauze/sd-webui-faceswaplab/assets/18318073/3ae35dce-f42a-4d90-a3e0-65b88383a38d"> I modified this file in the source code to achieve this function: `sd-webui-faceswaplab/scripts/faceswaplab_ui/faceswaplab_unit_settings.py` ``` @property def faces(self) -> List[Face]: """_summary_ Extract all faces (including reference face) to provide an array of faces Only processed once. """ if self.batch_files is not None and not hasattr(self, "_faces"): self._faces = ( [self.reference_face] if self.reference_face is not None else [] ) # ===> begin change <=== files = self.batch_files if isinstance(self.batch_files, str): batchs = [] base64_batch_files = self.batch_files.split(',') for base64str in base64_batch_files: batchs.append(api_utils.base64_to_pil(base64str)) files = batchs for file in files: # ===> end change <=== if isinstance(file, Image.Image): img = file else: img = Image.open(file.name) # type: ignore face = swapper.get_or_default( swapper.get_faces(pil_to_cv2(img)), 0, None ) if face is not None: self._faces.append(face) return self._faces ``` Hope it can be adopted
IAn2018cs commented 2023-09-13 14:30:49 +02:00 (Migrated from github.com)

Do I need to submit a pull request?

Do I need to submit a pull request?
glucauze commented 2023-09-13 14:43:11 +02:00 (Migrated from github.com)

I did not have time to look at it. Thanks the proposal, i will check if it does not break anything. I am mostly using my version of the API https://github.com/glucauze/sd-webui-faceswaplab/blob/main/client_api/faceswaplab_api_example.py

I did not have time to look at it. Thanks the proposal, i will check if it does not break anything. I am mostly using my version of the API https://github.com/glucauze/sd-webui-faceswaplab/blob/main/client_api/faceswaplab_api_example.py
andypotato commented 2023-09-21 09:01:56 +02:00 (Migrated from github.com)

I am mostly using my version of the API https://github.com/glucauze/sd-webui-faceswaplab/blob/main/client_api/faceswaplab_api_example.py

Your API works absolutely fine. Still there are valid use cases for using alwayson_scripts if you don't want to split image generation / face swap into two API calls. Depending on how many images you generate in a batch, this can be a quite significant overhead.

Not criticizing your extension here as it's a wonderful tool. Just want you to better understand why some prefer the alwayson_scripts.

> I am mostly using my version of the API https://github.com/glucauze/sd-webui-faceswaplab/blob/main/client_api/faceswaplab_api_example.py Your API works absolutely fine. Still there are valid use cases for using alwayson_scripts if you don't want to split image generation / face swap into two API calls. Depending on how many images you generate in a batch, this can be a quite significant overhead. Not criticizing your extension here as it's a wonderful tool. Just want you to better understand why some prefer the alwayson_scripts.
glucauze commented 2023-09-23 15:46:52 +02:00 (Migrated from github.com)

Got it, i will try to integrate that.

Got it, i will try to integrate that.
claygraffix commented 2023-10-17 02:33:25 +02:00 (Migrated from github.com)

@IAn2018cs could you post the rest of your alwayson_script params? I don't need your entire code changes since i'm only using 1 reference photo I think. About to start testing now, looks like you got it working though!

@IAn2018cs could you post the rest of your alwayson_script params? I don't need your entire code changes since i'm only using 1 reference photo I think. About to start testing now, looks like you got it working though!
IAn2018cs commented 2023-10-17 08:14:23 +02:00 (Migrated from github.com)

@IAn2018cs could you post the rest of your alwayson_script params? I don't need your entire code changes since i'm only using 1 reference photo I think. About to start testing now, looks like you got it working though!

        "faceswaplab": {
                "args": [
                    # ============> Face 1 <============
                    # Reference
                    get_base64_image('./test_img/t1.png'),
                    # Face Checkpoint (precedence over reference face)
                    None,
                    # Batch Sources Images
                    f"{get_base64_image('./test_img/t1.png')},{get_base64_image('./test_img/t2.webp')},{get_base64_image('./test_img/t3.jpg')}",
                    # Blend Faces ((Source|Checkpoint)+References = 1)
                    True,
                    # Enable
                    True,
                    # Same Gender
                    False,
                    # Sort by size (larger>smaller)
                    True,
                    # Check similarity
                    True,
                    # Compute similarity
                    True,
                    # Min similarity
                    0.5,
                    # "Min reference similarity"
                    0.2,
                    # Target face : Comma separated face number(s)
                    "0",
                    # Reference source face : start from 0
                    0,
                    # Swap in source image (blended face)
                    True,
                    # Swap in generated image
                    True,
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0,
                    # Restore Face
                    "None",
                    # Restore visibility
                    1,
                    # codeformer weight
                    1,
                    # Upscaler
                    "",
                    # Use improved segmented mask (use pastenet to mask only the face)
                    False,
                    # Use color corrections
                    False,
                    # sharpen face
                    False,
                    # Upscaled swapper mask erosion factor, 1 = default behaviour.
                    1,
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0,

                    # ============> Face 2 <============
                    # Reference
                    None,
                    # Face Checkpoint (precedence over reference face)
                    None,
                    # Batch Sources Images
                    None,
                    # Blend Faces ((Source|Checkpoint)+References = 1)
                    True,
                    # Enable
                    False,
                    # Same Gender
                    False,
                    # Sort by size (larger>smaller)
                    False,
                    # Check similarity
                    False,
                    # Compute similarity
                    False,
                    # Min similarity
                    0,
                    # "Min reference similarity"
                    0,
                    # Target face : Comma separated face number(s)
                    "1",
                    # Reference source face : start from 0
                    0,
                    # Swap in source image (blended face)
                    False,
                    # Swap in generated image
                    True,
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0,
                    # Restore Face
                    "None",
                    # Restore visibility
                    1,
                    # codeformer weight
                    1,
                    # Upscaler
                    "",
                    # Use improved segmented mask (use pastenet to mask only the face)
                    False,
                    # Use color corrections
                    False,
                    # sharpen face
                    False,
                    # Upscaled swapper mask erosion factor, 1 = default behaviour.
                    1,
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0,

                    # ============> Face 3 <============
                    # Reference
                    None,
                    # Face Checkpoint (precedence over reference face)
                    None,
                    # Batch Sources Images
                    None,
                    # Blend Faces ((Source|Checkpoint)+References = 1)
                    True,
                    # Enable
                    False,
                    # Same Gender
                    False,
                    # Sort by size (larger>smaller)
                    False,
                    # Check similarity
                    False,
                    # Compute similarity
                    False,
                    # Min similarity
                    0,
                    # "Min reference similarity"
                    0,
                    # Target face : Comma separated face number(s)
                    "2",
                    # Reference source face : start from 0
                    0,
                    # Swap in source image (blended face)
                    False,
                    # Swap in generated image
                    True,
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0,
                    # Restore Face
                    "None",
                    # Restore visibility
                    1,
                    # codeformer weight
                    1,
                    # Upscaler
                    "",
                    # Use improved segmented mask (use pastenet to mask only the face)
                    False,
                    # Use color corrections
                    False,
                    # sharpen face
                    False,
                    # Upscaled swapper mask erosion factor, 1 = default behaviour.
                    1,
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0,

                    # ============> Global Post-Processing <============
                    # Restore Face
                    "GFPGAN",
                    # Restore visibility
                    1,
                    # codeformer weight
                    1,
                    # Upscaler
                    "R-ESRGAN 4x+",
                    # Upscaler scale
                    1,
                    # Upscaler visibility (if scale = 1)
                    1,
                    # Enable/When
                    "After Upscaling/Before Restore Face",
                    # Denoising strenght
                    0,
                    # Inpainting prompt use [gender] instead of men or woman
                    "Portrait of a [gender]",
                    # Inpainting negative prompt use [gender] instead of men or woman
                    "blurry",
                    # Inpainting steps
                    20,
                    # Inpainting Sampler
                    "Euler a",
                    # sd model (experimental)
                    None,
                    # Inpainting seed
                    0
                ]
            }
> @IAn2018cs could you post the rest of your alwayson_script params? I don't need your entire code changes since i'm only using 1 reference photo I think. About to start testing now, looks like you got it working though! ```python "faceswaplab": { "args": [ # ============> Face 1 <============ # Reference get_base64_image('./test_img/t1.png'), # Face Checkpoint (precedence over reference face) None, # Batch Sources Images f"{get_base64_image('./test_img/t1.png')},{get_base64_image('./test_img/t2.webp')},{get_base64_image('./test_img/t3.jpg')}", # Blend Faces ((Source|Checkpoint)+References = 1) True, # Enable True, # Same Gender False, # Sort by size (larger>smaller) True, # Check similarity True, # Compute similarity True, # Min similarity 0.5, # "Min reference similarity" 0.2, # Target face : Comma separated face number(s) "0", # Reference source face : start from 0 0, # Swap in source image (blended face) True, # Swap in generated image True, # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0, # Restore Face "None", # Restore visibility 1, # codeformer weight 1, # Upscaler "", # Use improved segmented mask (use pastenet to mask only the face) False, # Use color corrections False, # sharpen face False, # Upscaled swapper mask erosion factor, 1 = default behaviour. 1, # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0, # ============> Face 2 <============ # Reference None, # Face Checkpoint (precedence over reference face) None, # Batch Sources Images None, # Blend Faces ((Source|Checkpoint)+References = 1) True, # Enable False, # Same Gender False, # Sort by size (larger>smaller) False, # Check similarity False, # Compute similarity False, # Min similarity 0, # "Min reference similarity" 0, # Target face : Comma separated face number(s) "1", # Reference source face : start from 0 0, # Swap in source image (blended face) False, # Swap in generated image True, # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0, # Restore Face "None", # Restore visibility 1, # codeformer weight 1, # Upscaler "", # Use improved segmented mask (use pastenet to mask only the face) False, # Use color corrections False, # sharpen face False, # Upscaled swapper mask erosion factor, 1 = default behaviour. 1, # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0, # ============> Face 3 <============ # Reference None, # Face Checkpoint (precedence over reference face) None, # Batch Sources Images None, # Blend Faces ((Source|Checkpoint)+References = 1) True, # Enable False, # Same Gender False, # Sort by size (larger>smaller) False, # Check similarity False, # Compute similarity False, # Min similarity 0, # "Min reference similarity" 0, # Target face : Comma separated face number(s) "2", # Reference source face : start from 0 0, # Swap in source image (blended face) False, # Swap in generated image True, # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0, # Restore Face "None", # Restore visibility 1, # codeformer weight 1, # Upscaler "", # Use improved segmented mask (use pastenet to mask only the face) False, # Use color corrections False, # sharpen face False, # Upscaled swapper mask erosion factor, 1 = default behaviour. 1, # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0, # ============> Global Post-Processing <============ # Restore Face "GFPGAN", # Restore visibility 1, # codeformer weight 1, # Upscaler "R-ESRGAN 4x+", # Upscaler scale 1, # Upscaler visibility (if scale = 1) 1, # Enable/When "After Upscaling/Before Restore Face", # Denoising strenght 0, # Inpainting prompt use [gender] instead of men or woman "Portrait of a [gender]", # Inpainting negative prompt use [gender] instead of men or woman "blurry", # Inpainting steps 20, # Inpainting Sampler "Euler a", # sd model (experimental) None, # Inpainting seed 0 ] } ```
claygraffix commented 2023-10-17 21:42:22 +02:00 (Migrated from github.com)

@IAn2018cs I appreciate this so much. I almost had it all figured out by looking at the dev tools. I wasn't exactly sure where it started/stopped. This is great, thank you.

@IAn2018cs I appreciate this so much. I almost had it all figured out by looking at the dev tools. I wasn't exactly sure where it started/stopped. This is great, thank you.
Sign in to join this conversation.