Spaces:
Running
on
Zero
Running
on
Zero
Update inpainting_module.py
Browse files- inpainting_module.py +25 -3
inpainting_module.py
CHANGED
|
@@ -471,7 +471,8 @@ class InpaintingModule:
|
|
| 471 |
def prepare_control_image(
|
| 472 |
self,
|
| 473 |
image: Image.Image,
|
| 474 |
-
mode: str = "canny"
|
|
|
|
| 475 |
) -> Image.Image:
|
| 476 |
"""
|
| 477 |
Generate ControlNet conditioning image.
|
|
@@ -482,6 +483,9 @@ class InpaintingModule:
|
|
| 482 |
Input image
|
| 483 |
mode : str
|
| 484 |
Conditioning mode: "canny" or "depth"
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
Returns
|
| 487 |
-------
|
|
@@ -497,7 +501,24 @@ class InpaintingModule:
|
|
| 497 |
img_array = np.array(image)
|
| 498 |
|
| 499 |
if mode == "canny":
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
elif mode == "depth":
|
| 502 |
return self._generate_depth_map(image)
|
| 503 |
else:
|
|
@@ -890,7 +911,8 @@ class InpaintingModule:
|
|
| 890 |
|
| 891 |
control_image = self.prepare_control_image(
|
| 892 |
image,
|
| 893 |
-
self._current_conditioning_type
|
|
|
|
| 894 |
)
|
| 895 |
|
| 896 |
# Conditional prompt enhancement based on template
|
|
|
|
| 471 |
def prepare_control_image(
|
| 472 |
self,
|
| 473 |
image: Image.Image,
|
| 474 |
+
mode: str = "canny",
|
| 475 |
+
mask: Optional[Image.Image] = None
|
| 476 |
) -> Image.Image:
|
| 477 |
"""
|
| 478 |
Generate ControlNet conditioning image.
|
|
|
|
| 483 |
Input image
|
| 484 |
mode : str
|
| 485 |
Conditioning mode: "canny" or "depth"
|
| 486 |
+
mask : PIL.Image, optional
|
| 487 |
+
If provided, weakens ControlNet edges in masked region to give prompt more control.
|
| 488 |
+
This is crucial for color transformation tasks where strong edges can lock colors.
|
| 489 |
|
| 490 |
Returns
|
| 491 |
-------
|
|
|
|
| 501 |
img_array = np.array(image)
|
| 502 |
|
| 503 |
if mode == "canny":
|
| 504 |
+
canny_image = self._generate_canny_edges(img_array)
|
| 505 |
+
|
| 506 |
+
# Mask-aware processing: weaken edges in masked region
|
| 507 |
+
if mask is not None:
|
| 508 |
+
canny_array = np.array(canny_image)
|
| 509 |
+
mask_array = np.array(mask.convert('L'))
|
| 510 |
+
|
| 511 |
+
# In masked region, reduce Canny edge strength by 80%
|
| 512 |
+
# This prevents ControlNet from locking original colors
|
| 513 |
+
mask_region = mask_array > 128 # White = masked area
|
| 514 |
+
canny_array[mask_region] = (canny_array[mask_region] * 0.2).astype(np.uint8)
|
| 515 |
+
# ↑ Keep 20% edge strength for basic shape preservation
|
| 516 |
+
|
| 517 |
+
canny_image = Image.fromarray(canny_array)
|
| 518 |
+
logger.info("Applied mask-aware ControlNet: weakened edges in masked region by 80%")
|
| 519 |
+
|
| 520 |
+
return canny_image
|
| 521 |
+
|
| 522 |
elif mode == "depth":
|
| 523 |
return self._generate_depth_map(image)
|
| 524 |
else:
|
|
|
|
| 911 |
|
| 912 |
control_image = self.prepare_control_image(
|
| 913 |
image,
|
| 914 |
+
self._current_conditioning_type,
|
| 915 |
+
mask=processed_mask # Pass mask for mask-aware ControlNet processing
|
| 916 |
)
|
| 917 |
|
| 918 |
# Conditional prompt enhancement based on template
|