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 = ( seed = (
original_image["metadata"]["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" else "unknown_seed"
) )
@ -561,20 +562,22 @@ class InvokeAIWebServer:
) )
generation_parameters["init_img"] = cropped_init_image generation_parameters["init_img"] = cropped_init_image
# grab an Image of the mask if generation_parameters["is_mask_empty"]:
mask_image = Image.open( generation_parameters["init_mask"] = None
io.BytesIO( else:
base64.decodebytes( # grab an Image of the mask
bytes(generation_parameters["init_mask"], "utf-8") mask_image = Image.open(
io.BytesIO(
base64.decodebytes(
bytes(generation_parameters["init_mask"], "utf-8")
)
) )
) )
) # crop the mask image
cropped_mask_image = copy_image_from_bounding_box(
# crop the mask image mask_image, **generation_parameters["bounding_box"]
cropped_mask_image = copy_image_from_bounding_box( )
mask_image, **generation_parameters["bounding_box"] generation_parameters["init_mask"] = cropped_mask_image
)
generation_parameters["init_mask"] = cropped_mask_image
totalSteps = self.calculate_real_steps( totalSteps = self.calculate_real_steps(
steps=generation_parameters["steps"], steps=generation_parameters["steps"],
@ -750,7 +753,7 @@ class InvokeAIWebServer:
all_parameters["init_img"] = init_img_url all_parameters["init_img"] = init_img_url
if "init_mask" in all_parameters: 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) metadata = self.parameters_to_generated_image_metadata(all_parameters)

View File

@ -111,20 +111,26 @@ export const frontendToBackendParameters = (
...boundingBoxDimensions, ...boundingBoxDimensions,
}; };
if (shouldUseInpaintReplace) {
generationParameters.inpaint_replace = inpaintReplace;
}
generationParameters.init_img = imageToProcessUrl; generationParameters.init_img = imageToProcessUrl;
generationParameters.strength = img2imgStrength; generationParameters.strength = img2imgStrength;
generationParameters.fit = false; 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( generationParameters.init_mask = maskDataURL.split(
'data:image/png;base64,' 'data:image/png;base64,'
)[1]; )[1];
if (shouldUseInpaintReplace) {
generationParameters.inpaint_replace = inpaintReplace;
}
generationParameters.bounding_box = boundingBox; generationParameters.bounding_box = boundingBox;
} }

View File

@ -1,4 +1,5 @@
import Konva from 'konva'; import Konva from 'konva';
import { IRect } from 'konva/lib/types';
import { MaskLine } from '../inpaintingSlice'; import { MaskLine } from '../inpaintingSlice';
/** /**
@ -11,15 +12,19 @@ import { MaskLine } from '../inpaintingSlice';
* drawing the mask and compositing everything correctly to output a valid * drawing the mask and compositing everything correctly to output a valid
* mask image. * mask image.
*/ */
const generateMask = (image: HTMLImageElement, lines: MaskLine[]) => { const generateMask = (
const { width, height } = image; image: HTMLImageElement,
lines: MaskLine[],
boundingBox: IRect
) => {
const { x, y, width, height } = boundingBox;
const offscreenContainer = document.createElement('div'); const offscreenContainer = document.createElement('div');
const stage = new Konva.Stage({ const stage = new Konva.Stage({
container: offscreenContainer, container: offscreenContainer,
width: width, width: image.width,
height: height, height: image.height,
}); });
const layer = new Konva.Layer(); 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( layer.add(
new Konva.Image({ image: image, globalCompositeOperation: 'source-out' }) new Konva.Image({ image: image, globalCompositeOperation: 'source-out' })
); );
const maskDataURL = stage.toDataURL();
offscreenContainer.remove(); offscreenContainer.remove();
return stage.toDataURL(); return { maskDataURL, isMaskEmpty };
}; };
export default generateMask; export default generateMask;