DawnC commited on
Commit
af4e7b9
·
verified ·
1 Parent(s): b0eb228

Update inpainting_module.py

Browse files
Files changed (1) hide show
  1. inpainting_module.py +17 -10
inpainting_module.py CHANGED
@@ -473,7 +473,8 @@ class InpaintingModule:
473
  self,
474
  image: Image.Image,
475
  mode: str = "canny",
476
- mask: Optional[Image.Image] = None
 
477
  ) -> Image.Image:
478
  """
479
  Generate ControlNet conditioning image.
@@ -485,15 +486,17 @@ class InpaintingModule:
485
  mode : str
486
  Conditioning mode: "canny" or "depth"
487
  mask : PIL.Image, optional
488
- If provided, weakens ControlNet edges in masked region to give prompt more control.
489
- This is crucial for color transformation tasks where strong edges can lock colors.
 
 
490
 
491
  Returns
492
  -------
493
  PIL.Image
494
  Generated control image (edges or depth map)
495
  """
496
- logger.info(f"Preparing control image with mode: {mode}")
497
 
498
  # Convert to RGB if needed
499
  if image.mode != 'RGB':
@@ -504,19 +507,20 @@ class InpaintingModule:
504
  if mode == "canny":
505
  canny_image = self._generate_canny_edges(img_array)
506
 
507
- # Mask-aware processing: weaken edges in masked region
508
- if mask is not None:
509
  canny_array = np.array(canny_image)
510
  mask_array = np.array(mask.convert('L'))
511
 
512
  # In masked region, completely suppress Canny edges
513
- # This prevents ControlNet from locking original colors
514
  mask_region = mask_array > 128 # White = masked area
515
  canny_array[mask_region] = 0
516
- # ↑ Complete suppression gives prompt maximum control over color transformation
517
 
518
  canny_image = Image.fromarray(canny_array)
519
- logger.info("Applied mask-aware ControlNet: suppressed edges in masked region completely")
 
 
520
 
521
  return canny_image
522
 
@@ -866,6 +870,7 @@ class InpaintingModule:
866
  )
867
  feather_radius = kwargs.get('feather_radius', self.config.feather_radius)
868
  strength = kwargs.get('strength', self.config.strength)
 
869
 
870
  if progress_callback:
871
  progress_callback("Preparing images...", 5)
@@ -914,7 +919,8 @@ class InpaintingModule:
914
  control_image = self.prepare_control_image(
915
  image,
916
  self._current_conditioning_type,
917
- mask=processed_mask # Pass mask for mask-aware ControlNet processing
 
918
  )
919
 
920
  # Conditional prompt enhancement based on template
@@ -1059,6 +1065,7 @@ class InpaintingModule:
1059
  "conditioning_type": self._current_conditioning_type,
1060
  "conditioning_scale": conditioning_scale,
1061
  "strength": strength,
 
1062
  "num_inference_steps": num_steps,
1063
  "guidance_scale": guidance,
1064
  "feather_radius": feather_radius,
 
473
  self,
474
  image: Image.Image,
475
  mode: str = "canny",
476
+ mask: Optional[Image.Image] = None,
477
+ preserve_structure: bool = False
478
  ) -> Image.Image:
479
  """
480
  Generate ControlNet conditioning image.
 
486
  mode : str
487
  Conditioning mode: "canny" or "depth"
488
  mask : PIL.Image, optional
489
+ If provided, can suppress edges in masked region (when preserve_structure=False).
490
+ preserve_structure : bool
491
+ If True, keep edges in masked region (for color change tasks).
492
+ If False, suppress edges in masked region (for replacement/removal tasks).
493
 
494
  Returns
495
  -------
496
  PIL.Image
497
  Generated control image (edges or depth map)
498
  """
499
+ logger.info(f"Preparing control image with mode: {mode}, preserve_structure: {preserve_structure}")
500
 
501
  # Convert to RGB if needed
502
  if image.mode != 'RGB':
 
507
  if mode == "canny":
508
  canny_image = self._generate_canny_edges(img_array)
509
 
510
+ # Mask-aware processing: suppress edges in masked region ONLY if not preserving structure
511
+ if mask is not None and not preserve_structure:
512
  canny_array = np.array(canny_image)
513
  mask_array = np.array(mask.convert('L'))
514
 
515
  # In masked region, completely suppress Canny edges
516
+ # This allows complete replacement/removal of the object
517
  mask_region = mask_array > 128 # White = masked area
518
  canny_array[mask_region] = 0
 
519
 
520
  canny_image = Image.fromarray(canny_array)
521
+ logger.info("Suppressed edges in masked region for replacement/removal")
522
+ elif preserve_structure:
523
+ logger.info("Preserving edges in masked region for color change")
524
 
525
  return canny_image
526
 
 
870
  )
871
  feather_radius = kwargs.get('feather_radius', self.config.feather_radius)
872
  strength = kwargs.get('strength', self.config.strength)
873
+ preserve_structure = kwargs.get('preserve_structure_in_mask', False)
874
 
875
  if progress_callback:
876
  progress_callback("Preparing images...", 5)
 
919
  control_image = self.prepare_control_image(
920
  image,
921
  self._current_conditioning_type,
922
+ mask=processed_mask,
923
+ preserve_structure=preserve_structure # True for color change, False for replacement/removal
924
  )
925
 
926
  # Conditional prompt enhancement based on template
 
1065
  "conditioning_type": self._current_conditioning_type,
1066
  "conditioning_scale": conditioning_scale,
1067
  "strength": strength,
1068
+ "preserve_structure": preserve_structure,
1069
  "num_inference_steps": num_steps,
1070
  "guidance_scale": guidance,
1071
  "feather_radius": feather_radius,