Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

adding tiling processor #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/controlnet_aux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
from .shuffle import ContentShuffleDetector
from .mediapipe_face import MediapipeFaceDetector
from .segment_anything import SamDetector
from .tiling import TilingDetector
8 changes: 6 additions & 2 deletions src/controlnet_aux/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
CannyDetector,
ContentShuffleDetector,
ZoeDetector,
MediapipeFaceDetector)
MediapipeFaceDetector,
TilingDetector,
)

LOGGER = logging.getLogger(__name__)

Expand All @@ -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},
}


Expand Down Expand Up @@ -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())}"
Expand Down
25 changes: 25 additions & 0 deletions src/controlnet_aux/tiling/__init__.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -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