Spaces:
Sleeping
Sleeping
Update faceless_processing.py
Browse files- 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 β
|
| 90 |
-
#
|
| 91 |
-
canny_low: int =
|
| 92 |
-
canny_high: int =
|
| 93 |
-
bilateral_d: int =
|
| 94 |
-
bilateral_sigma_color: int =
|
| 95 |
-
bilateral_sigma_space: int =
|
| 96 |
-
gaussian_ksize: int =
|
| 97 |
-
min_contour_perimeter: float =
|
| 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,18 +106,16 @@ class LineartConfig:
|
|
| 106 |
detail_bilateral_d: int = 7
|
| 107 |
detail_bilateral_sigma: int = 60
|
| 108 |
|
| 109 |
-
# Embroidery stitch feasibility
|
| 110 |
-
min_embroidery_stitch_px: int =
|
| 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.
|
| 116 |
|
| 117 |
-
# Aspect ratio filter:
|
| 118 |
-
|
| 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
|
| 362 |
-
1.
|
| 363 |
-
2.
|
| 364 |
-
3. Auto-Canny (median-based
|
| 365 |
-
|
| 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 |
-
# ---
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 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 |
-
# ---
|
| 384 |
-
sigma = getattr(cfg, 'canny_sigma', 0.
|
| 385 |
if sigma > 0:
|
| 386 |
-
|
| 387 |
else:
|
| 388 |
-
|
| 389 |
|
| 390 |
-
# ---
|
| 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)
|