Demotes inpainting to img2img when mask is empty

This commit is contained in:
psychedelicious 2022-10-30 23:17:51 +11:00
parent 62c346850c
commit 5efd2ed7a8
3 changed files with 56 additions and 24 deletions

View File

@ -336,7 +336,8 @@ class InvokeAIWebServer:
seed = (
original_image["metadata"]["seed"]
if "metadata" in original_image and "seed" in original_image["metadata"]
if "metadata" in original_image
and "seed" in original_image["metadata"]
else "unknown_seed"
)
@ -561,6 +562,9 @@ class InvokeAIWebServer:
)
generation_parameters["init_img"] = cropped_init_image
if generation_parameters["is_mask_empty"]:
generation_parameters["init_mask"] = None
else:
# grab an Image of the mask
mask_image = Image.open(
io.BytesIO(
@ -569,7 +573,6 @@ class InvokeAIWebServer:
)
)
)
# crop the mask image
cropped_mask_image = copy_image_from_bounding_box(
mask_image, **generation_parameters["bounding_box"]
@ -750,7 +753,7 @@ class InvokeAIWebServer:
all_parameters["init_img"] = init_img_url
if "init_mask" in all_parameters:
all_parameters["init_mask"] = "" #
all_parameters["init_mask"] = "" # TODO: store the mask in metadata
metadata = self.parameters_to_generated_image_metadata(all_parameters)

View File

@ -111,20 +111,26 @@ export const frontendToBackendParameters = (
...boundingBoxDimensions,
};
if (shouldUseInpaintReplace) {
generationParameters.inpaint_replace = inpaintReplace;
}
generationParameters.init_img = imageToProcessUrl;
generationParameters.strength = img2imgStrength;
generationParameters.fit = false;
const maskDataURL = generateMask(maskImageElement, lines);
const { maskDataURL, isMaskEmpty } = generateMask(
maskImageElement,
lines,
boundingBox
);
generationParameters.is_mask_empty = isMaskEmpty;
generationParameters.init_mask = maskDataURL.split(
'data:image/png;base64,'
)[1];
if (shouldUseInpaintReplace) {
generationParameters.inpaint_replace = inpaintReplace;
}
generationParameters.bounding_box = boundingBox;
}

View File

@ -1,4 +1,5 @@
import Konva from 'konva';
import { IRect } from 'konva/lib/types';
import { MaskLine } from '../inpaintingSlice';
/**
@ -11,15 +12,19 @@ import { MaskLine } from '../inpaintingSlice';
* drawing the mask and compositing everything correctly to output a valid
* mask image.
*/
const generateMask = (image: HTMLImageElement, lines: MaskLine[]) => {
const { width, height } = image;
const generateMask = (
image: HTMLImageElement,
lines: MaskLine[],
boundingBox: IRect
) => {
const { x, y, width, height } = boundingBox;
const offscreenContainer = document.createElement('div');
const stage = new Konva.Stage({
container: offscreenContainer,
width: width,
height: height,
width: image.width,
height: image.height,
});
const layer = new Konva.Layer();
@ -42,13 +47,31 @@ const generateMask = (image: HTMLImageElement, lines: MaskLine[]) => {
)
);
// check if mask is empty
const pixelBuffer = new Uint32Array(
layer.getContext().getImageData(x, y, width, height).data.buffer
);
const isMaskEmpty = !pixelBuffer.some((color) => color !== 0);
if (isMaskEmpty) {
layer.add(
new Konva.Rect({
...boundingBox,
fill: 'rgb(0,0,0)',
})
);
}
layer.add(
new Konva.Image({ image: image, globalCompositeOperation: 'source-out' })
);
const maskDataURL = stage.toDataURL();
offscreenContainer.remove();
return stage.toDataURL();
return { maskDataURL, isMaskEmpty };
};
export default generateMask;