InvokeAI/invokeai/backend/image_util/patchmatch.py

48 lines
1.3 KiB
Python
Raw Normal View History

2023-03-03 06:02:00 +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
"""
import numpy as np
2023-03-03 05:02:15 +00:00
from invokeai.backend.globals import Globals
2023-03-03 06:02:00 +00:00
class PatchMatch:
2023-03-03 06:02:00 +00:00
"""
Thin class wrapper around the patchmatch function.
2023-03-03 06:02:00 +00:00
"""
patch_match = None
2023-03-03 06:02:00 +00:00
tried_load: bool = False
def __init__(self):
super().__init__()
@classmethod
def _load_patch_match(self):
if self.tried_load:
return
if Globals.try_patchmatch:
from patchmatch import patch_match as pm
2023-03-03 06:02:00 +00:00
if pm.patchmatch_available:
2023-03-03 06:02:00 +00:00
print(">> Patchmatch initialized")
else:
2023-03-03 06:02:00 +00:00
print(">> Patchmatch not loaded (nonfatal)")
self.patch_match = pm
else:
2023-03-03 06:02:00 +00:00
print(">> Patchmatch loading disabled")
self.tried_load = True
@classmethod
2023-03-03 06:02:00 +00:00
def patchmatch_available(self) -> bool:
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:
if self.patchmatch_available():
2023-03-03 06:02:00 +00:00
return self.patch_match.inpaint(*args, **kwargs)