Spaces:
Running
Fix the empty hair matte, and stop pasting hair when HairFastGAN is unavailable
Browse filesTwo separate hair problems, found by running the pipeline on real photos.
_soft_hair_matte parsed a very wide crop so long hair wouldn't be cut off before
parsing, but that also shrinks the head inside the 512x512 parse input and takes
BiSeNet's hair confidence down with it. On a 66x77 face the matte peaked at
0.341 — nothing at all above the 0.4 that swap_hair thresholds at — so the mask
came out empty and hair transfer silently did nothing at all. It now keeps the
wide crop when it is confident and tightens only when it is not; across five
test photos the matte went from 0.341/0.00% usable to 0.75-0.84, all usable.
That alone made things worse, though: with a mask to work from, the fallback
path warps the source's hair straight into the scene and composites a smeared,
semi-transparent blob over the head — the pasted-hair artifact this project has
been chasing. It is plainly worse than keeping the target's hair, which the
clean face swap already produces.
So the fallback is gone. Hair is transferred only when HairFastGAN actually
returns a portrait; otherwise the target's hair is kept and the reason is
logged. The matte fix still matters there, since that path parses the generated
portrait. HairFastGAN currently fails on the Space because it connects to the
public ZeroGPU Space anonymously and hits its quota — setting HF_TOKEN (or
HAIRFAST_SPACE) is what turns real hair transfer back on.
14 tests pass.
Files changed (2)
- core/head_swap.py+30-12
- web_app.py+9-1
121 unmodified lines122123124125126125126127128129130131132133134135136127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157121 unmodified lines return full x1, y1, x2, y2 = [int(v) for v in bbox] bw, bh = x2 - x1, y2 - y1
def _matte(up_, down_, side_): ex1 = max(0, x1 - int(bw * side)); ey1 = max(0, y1 - int(bh * up)) ex2 = min(w, x2 + int(bw * side)); ey2 = min(h, y2 + int(bh * down)) crop = image[ey1:ey2, ex1:ex2] if crop.size == 0: return full inp = cv2.resize(crop, (512, 512)) rgb = cv2.cvtColor(inp, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0 rgb = (rgb - 0.5) / 0.5 t = torch.from_numpy(rgb.transpose(2, 0, 1)).unsqueeze(0).float().to(_device()) with torch.no_grad(): prob = torch.softmax(parser(t)[0], dim=1)[0, 17].cpu().numpy().astype(np.float32) full[ey1:ey2, ex1:ex2] = cv2.resize(prob, (ex2 - ex1, ey2 - ey1)) ex1 = max(0, x1 - int(bw * side_)); ey1 = max(0, y1 - int(bh * up_)) ex2 = min(w, x2 + int(bw * side_)); ey2 = min(h, y2 + int(bh * down_)) crop = image[ey1:ey2, ex1:ex2] if crop.size == 0: return None inp = cv2.resize(crop, (512, 512)) rgb = cv2.cvtColor(inp, cv2.COLOR_BGR2RGB).astype(np.float32) / 255.0 rgb = (rgb - 0.5) / 0.5 t = torch.from_numpy(rgb.transpose(2, 0, 1)).unsqueeze(0).float().to(_device()) with torch.no_grad(): prob = torch.softmax(parser(t)[0], dim=1)[0, 17].cpu().numpy().astype(np.float32) out = np.zeros((h, w), np.float32) out[ey1:ey2, ex1:ex2] = cv2.resize(prob, (ex2 - ex1, ey2 - ey1)) return out
# The wide crop exists so long hair isn't cut off before parsing, but it # also shrinks the head inside the 512x512 parse input, and BiSeNet's hair # confidence falls with it. Measured on a 66x77 face: the wide crop peaks at # 0.341, leaving NOTHING above the 0.4 that swap_hair thresholds at, so the # hair mask came out empty and the transfer silently did nothing. So keep # the wide crop when it is confident, and tighten only when it is not. for factor in (1.0, 0.6, 0.38, 0.25): m = _matte(up * max(factor, 0.6), down * factor, side * factor) if m is None: continue full = m if m.max() >= 0.5: break return full
921 unmodified lines922923924925925926927928929930931932933934935936921 unmodified lines cv2.BORDER_CONSTANT, value=(127, 127, 127)) swapped = swap_hair(swapped, hf_padded, swapped, include_face=False) else: swapped = swap_hair(swapped, source, target, include_face=False) # No crude fallback. Warping the source's hair straight into # the scene composites a smeared, semi-transparent blob over # the head — visibly worse than simply keeping the target's # hair, which is what the clean face swap already gives. # Real hair transfer needs HairFastGAN: set HF_TOKEN on the # Space (the anonymous ZeroGPU quota is what fails here), or # HAIRFAST_SPACE for your own GPU Space. print("[swap] hair transfer unavailable (HairFastGAN) — " "keeping the target's hair") except Exception as e: print(f"[swap] hair compose error: {e}")