From f2067cbb78602d33f67377bd6f52dfa27652e899 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 5 Jun 2023 10:39:52 +0000 Subject: [PATCH] adding tiling --- src/controlnet_aux/__init__.py | 1 + src/controlnet_aux/processor.py | 8 ++++++-- src/controlnet_aux/tiling/__init__.py | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/controlnet_aux/tiling/__init__.py diff --git a/src/controlnet_aux/__init__.py b/src/controlnet_aux/__init__.py index 58e3671..2bbfcbb 100644 --- a/src/controlnet_aux/__init__.py +++ b/src/controlnet_aux/__init__.py @@ -14,3 +14,4 @@ from .shuffle import ContentShuffleDetector from .mediapipe_face import MediapipeFaceDetector from .segment_anything import SamDetector +from .tiling import TilingDetector \ No newline at end of file diff --git a/src/controlnet_aux/processor.py b/src/controlnet_aux/processor.py index ee1d4a7..5c65c9e 100644 --- a/src/controlnet_aux/processor.py +++ b/src/controlnet_aux/processor.py @@ -18,7 +18,9 @@ CannyDetector, ContentShuffleDetector, ZoeDetector, - MediapipeFaceDetector) + MediapipeFaceDetector, + TilingDetector, + ) LOGGER = logging.getLogger(__name__) @@ -44,11 +46,12 @@ 'lineart_coarse': {'class': LineartDetector, 'checkpoint': True}, 'lineart_realistic': {'class': LineartDetector, 'checkpoint': True}, 'lineart_anime': {'class': LineartAnimeDetector, 'checkpoint': True}, - 'depth_zoe': {'class': ZoeDetector, 'checkpoint': True}, + 'depth_zoe': {'class': ZoeDetector, 'checkpoint': True}, # instantiate 'shuffle': {'class': ContentShuffleDetector, 'checkpoint': False}, 'mediapipe_face': {'class': MediapipeFaceDetector, 'checkpoint': False}, 'canny': {'class': CannyDetector, 'checkpoint': False}, + 'tiling': {'class': TilingDetector, 'checkpoint': False}, } @@ -76,6 +79,7 @@ 'shuffle': {'resize': False}, 'depth_zoe': {'resize': False}, 'mediapipe_face': {'resize': False}, + 'tiling': {'resize': False}, } CHOICES = f"Choices for the processor are {list(MODELS.keys())}" diff --git a/src/controlnet_aux/tiling/__init__.py b/src/controlnet_aux/tiling/__init__.py new file mode 100644 index 0000000..8669e07 --- /dev/null +++ b/src/controlnet_aux/tiling/__init__.py @@ -0,0 +1,25 @@ +from PIL import Image + + +class TilingDetector: + def __call__(self, input_image: Image.Image, resolution: int = 1024): + input_image = input_image.convert("RGB") + original_width, original_height = input_image.size + + # Calculate the scale factor based on the desired resolution + scale_factor = float(resolution) / min(original_height, original_width) + + # Scale the width and height of the image + scaled_height = original_height * scale_factor + scaled_width = original_width * scale_factor + + # Round the scaled dimensions to the nearest multiple of 64 + scaled_height = int(round(scaled_height / 64.0)) * 64 + scaled_width = int(round(scaled_width / 64.0)) * 64 + + # Resize the image using Lanczos interpolation + resized_image = input_image.resize( + (scaled_width, scaled_height), resample=Image.LANCZOS + ) + + return resized_image