2022-12-01 05:33:20 +00:00
|
|
|
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
|
|
|
|
|
|
|
|
from typing import Literal
|
2023-03-03 06:02:00 +00:00
|
|
|
|
|
|
|
import cv2 as cv
|
2022-12-01 05:33:20 +00:00
|
|
|
import numpy
|
|
|
|
from PIL import Image, ImageOps
|
2023-04-10 09:07:48 +00:00
|
|
|
from pydantic import BaseModel, Field
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-04-04 01:05:15 +00:00
|
|
|
from invokeai.app.models.image import ImageField, ImageType
|
2023-04-10 22:24:48 +00:00
|
|
|
from .baseinvocation import BaseInvocation, InvocationContext, InvocationConfig
|
2023-04-04 01:05:15 +00:00
|
|
|
from .image import ImageOutput
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
|
2023-04-11 01:50:28 +00:00
|
|
|
class CvInvocationConfig(BaseModel):
|
2023-04-10 09:07:48 +00:00
|
|
|
"""Helper class to provide all OpenCV invocations with additional config"""
|
|
|
|
|
|
|
|
# Schema customisation
|
|
|
|
class Config(InvocationConfig):
|
|
|
|
schema_extra = {
|
|
|
|
"ui": {
|
|
|
|
"tags": ["cv", "image"],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-11 01:50:28 +00:00
|
|
|
class CvInpaintInvocation(BaseInvocation, CvInvocationConfig):
|
2022-12-01 05:33:20 +00:00
|
|
|
"""Simple inpaint using opencv."""
|
2023-03-03 20:25:08 +00:00
|
|
|
#fmt: off
|
2023-03-03 06:02:00 +00:00
|
|
|
type: Literal["cv_inpaint"] = "cv_inpaint"
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
# Inputs
|
|
|
|
image: ImageField = Field(default=None, description="The image to inpaint")
|
2023-03-03 20:25:08 +00:00
|
|
|
mask: ImageField = Field(default=None, description="The mask to use when inpainting")
|
|
|
|
#fmt: on
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def invoke(self, context: InvocationContext) -> ImageOutput:
|
2023-03-03 06:02:00 +00:00
|
|
|
image = context.services.images.get(
|
|
|
|
self.image.image_type, self.image.image_name
|
|
|
|
)
|
2022-12-01 05:33:20 +00:00
|
|
|
mask = context.services.images.get(self.mask.image_type, self.mask.image_name)
|
|
|
|
|
|
|
|
# Convert to cv image/mask
|
|
|
|
# TODO: consider making these utility functions
|
2023-03-03 06:02:00 +00:00
|
|
|
cv_image = cv.cvtColor(numpy.array(image.convert("RGB")), cv.COLOR_RGB2BGR)
|
2022-12-01 05:33:20 +00:00
|
|
|
cv_mask = numpy.array(ImageOps.invert(mask))
|
|
|
|
|
|
|
|
# Inpaint
|
|
|
|
cv_inpainted = cv.inpaint(cv_image, cv_mask, 3, cv.INPAINT_TELEA)
|
|
|
|
|
|
|
|
# Convert back to Pillow
|
|
|
|
# TODO: consider making a utility function
|
|
|
|
image_inpainted = Image.fromarray(cv.cvtColor(cv_inpainted, cv.COLOR_BGR2RGB))
|
|
|
|
|
|
|
|
image_type = ImageType.INTERMEDIATE
|
2023-03-03 06:02:00 +00:00
|
|
|
image_name = context.services.images.create_name(
|
|
|
|
context.graph_execution_state_id, self.id
|
|
|
|
)
|
2022-12-01 05:33:20 +00:00
|
|
|
context.services.images.save(image_type, image_name, image_inpainted)
|
|
|
|
return ImageOutput(
|
2023-03-03 06:02:00 +00:00
|
|
|
image=ImageField(image_type=image_type, image_name=image_name)
|
2022-12-01 05:33:20 +00:00
|
|
|
)
|