2023-03-03 06:02:00 +00:00
|
|
|
"""
|
2022-12-20 23:32:35 +00:00
|
|
|
This module defines a singleton object, "patchmatch" that
|
|
|
|
wraps the actual patchmatch object. It respects the global
|
|
|
|
"try_patchmatch" attribute, so that patchmatch loading can
|
|
|
|
be suppressed or deferred
|
2023-03-03 06:02:00 +00:00
|
|
|
"""
|
2024-02-29 23:04:59 +00:00
|
|
|
|
2023-03-03 06:02:00 +00:00
|
|
|
import numpy as np
|
2023-08-18 15:13:28 +00:00
|
|
|
|
2023-04-29 13:43:40 +00:00
|
|
|
import invokeai.backend.util.logging as logger
|
2023-05-26 00:41:26 +00:00
|
|
|
from invokeai.app.services.config import InvokeAIAppConfig
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-05-26 00:41:26 +00:00
|
|
|
config = InvokeAIAppConfig.get_config()
|
2023-05-04 03:36:51 +00:00
|
|
|
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-20 23:32:35 +00:00
|
|
|
class PatchMatch:
|
2023-03-03 06:02:00 +00:00
|
|
|
"""
|
2022-12-20 23:32:35 +00:00
|
|
|
Thin class wrapper around the patchmatch function.
|
2023-03-03 06:02:00 +00:00
|
|
|
"""
|
2022-12-20 23:32:35 +00:00
|
|
|
|
|
|
|
patch_match = None
|
2023-03-03 06:02:00 +00:00
|
|
|
tried_load: bool = False
|
|
|
|
|
2022-12-20 23:32:35 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _load_patch_match(self):
|
|
|
|
if self.tried_load:
|
|
|
|
return
|
2023-05-04 03:36:51 +00:00
|
|
|
if config.try_patchmatch:
|
2022-12-20 23:32:35 +00:00
|
|
|
from patchmatch import patch_match as pm
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-20 23:32:35 +00:00
|
|
|
if pm.patchmatch_available:
|
2023-04-29 13:43:40 +00:00
|
|
|
logger.info("Patchmatch initialized")
|
2022-12-20 23:32:35 +00:00
|
|
|
else:
|
2023-04-29 13:43:40 +00:00
|
|
|
logger.info("Patchmatch not loaded (nonfatal)")
|
2022-12-20 23:32:35 +00:00
|
|
|
self.patch_match = pm
|
|
|
|
else:
|
2023-04-29 13:43:40 +00:00
|
|
|
logger.info("Patchmatch loading disabled")
|
2022-12-20 23:32:35 +00:00
|
|
|
self.tried_load = True
|
|
|
|
|
|
|
|
@classmethod
|
2023-03-03 06:02:00 +00:00
|
|
|
def patchmatch_available(self) -> bool:
|
2022-12-20 23:32:35 +00:00
|
|
|
self._load_patch_match()
|
|
|
|
return self.patch_match and self.patch_match.patchmatch_available
|
|
|
|
|
|
|
|
@classmethod
|
2023-03-03 06:02:00 +00:00
|
|
|
def inpaint(self, *args, **kwargs) -> np.ndarray:
|
2022-12-20 23:32:35 +00:00
|
|
|
if self.patchmatch_available():
|
2023-03-03 06:02:00 +00:00
|
|
|
return self.patch_match.inpaint(*args, **kwargs)
|