vitordigitizing commited on
Commit
ee7a650
Β·
verified Β·
1 Parent(s): 30586c8

Update faceless_processing.py

Browse files
Files changed (1) hide show
  1. faceless_processing.py +33 -44
faceless_processing.py CHANGED
@@ -86,16 +86,16 @@ class LineartConfig:
86
  # Silhouette outline β€” thicker for embroidery visibility
87
  silhouette_line_thickness: int = 6 # bold outer contour
88
 
89
- # Edge detection β€” balanced: enough blur to skip fabric texture,
90
- # but not so much that body structure (arms, clothing borders) disappears.
91
- canny_low: int = 40 # fallback only (auto-Canny is used by default)
92
- canny_high: int = 150 # fallback only
93
- bilateral_d: int = 9 # reduced: was 11, too blurry
94
- bilateral_sigma_color: int = 110.0
95
- bilateral_sigma_space: int = 110
96
- gaussian_ksize: int = 13 # reduced: was 17, too blurry
97
- min_contour_perimeter: float = 60.0 # reduced: was 100, was filtering too much
98
- interior_line_thickness: int = 5 # thicker interior lines
99
 
100
  # Secondary detail pass β€” OFF (adds too much clothing/texture noise)
101
  enable_detail_pass: bool = False
@@ -106,18 +106,16 @@ class LineartConfig:
106
  detail_bilateral_d: int = 7
107
  detail_bilateral_sigma: int = 60
108
 
109
- # Embroidery stitch feasibility β€” balanced: removes micro-noise but keeps body lines
110
- min_embroidery_stitch_px: int = 45
111
 
112
  # Auto-Canny: sigma controls threshold spread around the image median.
113
  # Higher sigma = more edges detected. 0.25–0.35 recovers body structure.
114
  # Set to 0 to use fixed canny_low/canny_high instead.
115
- canny_sigma: float = 0.28
116
 
117
- # Aspect ratio filter: contours whose bounding box aspect ratio is below
118
- # this value are treated as texture blobs and dropped.
119
- # 1.5 is relaxed enough to keep arm/clothing boundaries.
120
- min_aspect_ratio: float = 1.5
121
 
122
  # Face detection
123
  face_scale_factor: float = 1.05
@@ -358,37 +356,30 @@ def build_interior_edges(rgb: np.ndarray, subject_mask: np.ndarray, cfg: Lineart
358
  """
359
  Detect structural body lines.
360
 
361
- Pipeline (robust to any photo):
362
- 1. CLAHE normalises uneven lighting before any detection.
363
- 2. Strong bilateral + gaussian smoothing suppresses fabric texture.
364
- 3. Auto-Canny (median-based adaptive thresholds) replaces fixed values
365
- so the detector self-tunes to each photo's brightness/contrast.
366
- 4. XDoG pass (optional) adds smooth artistic edges on top.
367
- 5. Contours are filtered by perimeter, embroidery length, AND aspect
368
- ratio β€” discarding small texture blobs while keeping body structure.
369
  """
370
  gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
 
 
371
 
372
- # --- 1. CLAHE: normalise lighting so Canny thresholds work on any photo ---
373
- clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
374
- gray = clahe.apply(gray)
375
-
376
- # --- 2. Strong bilateral + gaussian smooth (suppress fabric texture) ---
377
- blurred_primary = cv2.bilateralFilter(gray, cfg.bilateral_d,
378
- cfg.bilateral_sigma_color,
379
- cfg.bilateral_sigma_space)
380
- blurred_primary = cv2.GaussianBlur(blurred_primary,
381
- (cfg.gaussian_ksize, cfg.gaussian_ksize), 0)
382
 
383
- # --- 3. Edge detection: auto-Canny (adaptive) or fixed fallback ---
384
- sigma = getattr(cfg, 'canny_sigma', 0.20)
385
  if sigma > 0:
386
- edges_primary = _auto_canny(blurred_primary, sigma)
387
  else:
388
- edges_primary = cv2.Canny(blurred_primary, cfg.canny_low, cfg.canny_high)
389
 
390
- # --- 4. Detail pass (disabled by default β€” adds too much clothing noise) ---
391
- edges_detail = np.zeros_like(edges_primary)
392
  if getattr(cfg, 'enable_detail_pass', False):
393
  detail_d = getattr(cfg, 'detail_bilateral_d', 7)
394
  detail_sigma = getattr(cfg, 'detail_bilateral_sigma', 60)
@@ -400,9 +391,7 @@ def build_interior_edges(rgb: np.ndarray, subject_mask: np.ndarray, cfg: Lineart
400
  getattr(cfg, 'detail_canny_low', 15),
401
  getattr(cfg, 'detail_canny_high', 55)
402
  )
403
-
404
- # Combine passes
405
- edges = cv2.bitwise_or(edges_primary, edges_detail)
406
 
407
  # Erode subject mask to avoid catching the outer boundary twice
408
  erode_size = max(5, cfg.silhouette_line_thickness * 2 + 3)
 
86
  # Silhouette outline β€” thicker for embroidery visibility
87
  silhouette_line_thickness: int = 6 # bold outer contour
88
 
89
+ # Edge detection β€” bilateral smoothing suppresses texture, auto-Canny adapts thresholds.
90
+ # These are proven values: enough smoothing to kill wrinkles, enough signal for structure.
91
+ canny_low: int = 30 # fallback only (auto-Canny active by default)
92
+ canny_high: int = 120 # fallback only
93
+ bilateral_d: int = 7 # classic recommended diameter
94
+ bilateral_sigma_color: int = 75
95
+ bilateral_sigma_space: int = 75
96
+ gaussian_ksize: int = 9 # moderate pre-blur
97
+ min_contour_perimeter: float = 50.0 # keeps arms/clothing borders
98
+ interior_line_thickness: int = 5
99
 
100
  # Secondary detail pass β€” OFF (adds too much clothing/texture noise)
101
  enable_detail_pass: bool = False
 
106
  detail_bilateral_d: int = 7
107
  detail_bilateral_sigma: int = 60
108
 
109
+ # Embroidery stitch feasibility
110
+ min_embroidery_stitch_px: int = 30
111
 
112
  # Auto-Canny: sigma controls threshold spread around the image median.
113
  # Higher sigma = more edges detected. 0.25–0.35 recovers body structure.
114
  # Set to 0 to use fixed canny_low/canny_high instead.
115
+ canny_sigma: float = 0.33 # proven standard (pyimagesearch auto-Canny)
116
 
117
+ # Aspect ratio filter: 0 = disabled. Enable (e.g. 1.8) only if blob noise reappears.
118
+ min_aspect_ratio: float = 0
 
 
119
 
120
  # Face detection
121
  face_scale_factor: float = 1.05
 
356
  """
357
  Detect structural body lines.
358
 
359
+ Pipeline:
360
+ 1. Convert to grayscale (no CLAHE β€” CLAHE amplifies skin/fabric noise).
361
+ 2. Bilateral + Gaussian smooth to suppress fine texture.
362
+ 3. Auto-Canny (median-based) adapts thresholds to each photo's brightness.
363
+ 4. Contours filtered by perimeter and embroidery stitch length.
 
 
 
364
  """
365
  gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
366
+ # NOTE: No CLAHE here β€” it locally boosts contrast in skin/fabric regions,
367
+ # causing the auto-Canny to detect thousands of micro-edges.
368
 
369
+ # --- Bilateral + Gaussian smooth ---
370
+ blurred = cv2.bilateralFilter(gray, cfg.bilateral_d,
371
+ cfg.bilateral_sigma_color,
372
+ cfg.bilateral_sigma_space)
373
+ blurred = cv2.GaussianBlur(blurred, (cfg.gaussian_ksize, cfg.gaussian_ksize), 0)
 
 
 
 
 
374
 
375
+ # --- Auto-Canny (adaptive) or fixed fallback ---
376
+ sigma = getattr(cfg, 'canny_sigma', 0.33)
377
  if sigma > 0:
378
+ edges = _auto_canny(blurred, sigma)
379
  else:
380
+ edges = cv2.Canny(blurred, cfg.canny_low, cfg.canny_high)
381
 
382
+ # --- Detail pass (disabled by default) ---
 
383
  if getattr(cfg, 'enable_detail_pass', False):
384
  detail_d = getattr(cfg, 'detail_bilateral_d', 7)
385
  detail_sigma = getattr(cfg, 'detail_bilateral_sigma', 60)
 
391
  getattr(cfg, 'detail_canny_low', 15),
392
  getattr(cfg, 'detail_canny_high', 55)
393
  )
394
+ edges = cv2.bitwise_or(edges, edges_detail)
 
 
395
 
396
  # Erode subject mask to avoid catching the outer boundary twice
397
  erode_size = max(5, cfg.silhouette_line_thickness * 2 + 3)