Spaces:
Running
Running
Commit ·
0d2a722
1
Parent(s): 34e8dd6
Hair edge: crisper feather + erode-to-solid + gentler lighting match -> natural hairline, no grey fringe on dark hair
Browse files- core/head_swap.py +12 -11
core/head_swap.py
CHANGED
|
@@ -111,7 +111,7 @@ def swap_hair(
|
|
| 111 |
source: np.ndarray,
|
| 112 |
target: np.ndarray,
|
| 113 |
include_face: bool = False,
|
| 114 |
-
feather: float = 0.
|
| 115 |
) -> np.ndarray:
|
| 116 |
"""
|
| 117 |
Transplant the source's hair onto the face-swapped result.
|
|
@@ -202,15 +202,14 @@ def swap_hair(
|
|
| 202 |
hair_region = cv2.dilate(binm, np.ones((3, 3), np.uint8))[..., None] > 0.5
|
| 203 |
warped_src = np.where(hair_region, warped_src, swapped)
|
| 204 |
|
| 205 |
-
#
|
| 206 |
-
#
|
| 207 |
-
#
|
| 208 |
-
#
|
| 209 |
-
|
| 210 |
-
|
| 211 |
k = max(3, int(min(h, w) * feather) | 1) # odd kernel
|
| 212 |
-
|
| 213 |
-
warped_mask = np.clip(np.maximum(core, soft), 0.0, 1.0)
|
| 214 |
alpha = np.stack([warped_mask] * 3, axis=-1)
|
| 215 |
|
| 216 |
# Match the source hair's exposure to the target scene before compositing.
|
|
@@ -447,8 +446,10 @@ def _match_lighting(src_region, dst, mask):
|
|
| 447 |
out = s.copy()
|
| 448 |
for c in range(3):
|
| 449 |
sm, dm = s[:, :, c][m].mean(), d[:, :, c][m].mean()
|
| 450 |
-
#
|
| 451 |
-
|
|
|
|
|
|
|
| 452 |
out[:, :, c] = s[:, :, c] + (dm - sm) * w
|
| 453 |
return cv2.cvtColor(np.clip(out, 0, 255).astype(np.uint8), cv2.COLOR_LAB2BGR)
|
| 454 |
|
|
|
|
| 111 |
source: np.ndarray,
|
| 112 |
target: np.ndarray,
|
| 113 |
include_face: bool = False,
|
| 114 |
+
feather: float = 0.01,
|
| 115 |
) -> np.ndarray:
|
| 116 |
"""
|
| 117 |
Transplant the source's hair onto the face-swapped result.
|
|
|
|
| 202 |
hair_region = cv2.dilate(binm, np.ones((3, 3), np.uint8))[..., None] > 0.5
|
| 203 |
warped_src = np.where(hair_region, warped_src, swapped)
|
| 204 |
|
| 205 |
+
# Crisp, natural edge: erode to SOLID hair, then feather with a SMALL
|
| 206 |
+
# kernel so the soft transition sits just INSIDE the hair. This drops the
|
| 207 |
+
# parser's fuzzy grey edge pixels AND avoids a wide grey band from blending
|
| 208 |
+
# dark hair into a bright background — the edge stays hair-coloured.
|
| 209 |
+
er_px = max(2, int(min(h, w) * 0.005))
|
| 210 |
+
solid = cv2.erode(binm, np.ones((er_px, er_px), np.uint8))
|
| 211 |
k = max(3, int(min(h, w) * feather) | 1) # odd kernel
|
| 212 |
+
warped_mask = np.clip(cv2.GaussianBlur(solid, (k, k), 0), 0.0, 1.0)
|
|
|
|
| 213 |
alpha = np.stack([warped_mask] * 3, axis=-1)
|
| 214 |
|
| 215 |
# Match the source hair's exposure to the target scene before compositing.
|
|
|
|
| 446 |
out = s.copy()
|
| 447 |
for c in range(3):
|
| 448 |
sm, dm = s[:, :, c][m].mean(), d[:, :, c][m].mean()
|
| 449 |
+
# Keep MOST of the source hair's own tone — it's the user's real hair, and
|
| 450 |
+
# the mask region in `dst` is the bright area the hair now covers, so a
|
| 451 |
+
# strong match would wash dark hair to grey. Just a gentle exposure nudge.
|
| 452 |
+
w = 0.25 if c == 0 else 0.15
|
| 453 |
out[:, :, c] = s[:, :, c] + (dm - sm) * w
|
| 454 |
return cv2.cvtColor(np.clip(out, 0, 255).astype(np.uint8), cv2.COLOR_LAB2BGR)
|
| 455 |
|