Spaces:
Running
Confine scene grain to the parsed head instead of a box around it
Browse filesharmonize_to_scene built its mask from a rectangle around the face bbox, so the
grain meant for the GAN-smooth face and hair was sprayed over everything sharing
those bounds — background, bookshelves, clothing — dirtying the scene and
leaving the box's own feathered edge visible in the result.
It now parses the head/hair/neck shape (clamped to a generous head box so stray
specks elsewhere can't pick up grain) and feathers it narrowly, just enough to
fade at the hairline. Class 18 (hat) is included here only: the parser labels
part of the hair as hat, and dropping it left an ungrained hole in the top of
the head. _HEAD_CLASSES still excludes it for hair transfer, where it misfires.
Measured on a self-swap of the Library target (768x1024, CPU), where the output
should barely differ from the input: the mean pixel change from this step falls
from 6.46 to 0.33, and the masked region from roughly half the frame to 2.4%.
Background, shirt and shelves now come through untouched.
528 unmodified lines529530531532533534535536537538534535536537538539539540541542543542543544545546547548549550551552553554555556557558559560561562563564565566528 unmodified lines h, w = result.shape[:2] res = result.astype(np.float32)
# Grain must land on the HEAD, not on a rectangle around it. A box sprays # noise over whatever shares its bounds — background, shelves, clothing — # which dirties the scene and leaves the box's own feathered edge visible. # So parse the actual head/hair/neck shape and clamp it to a generous head # box, which keeps stray parse specks elsewhere from picking up grain. x1, y1, x2, y2 = [int(v) for v in face_bbox] bw, bh = x2 - x1, y2 - y1 mask = np.zeros((h, w), np.float32) # Down reach covers hair past the shoulders (~mid-torso). At the old 1.2 the # lower half of long transplanted hair fell outside the mask and kept the # GAN-smooth look this function exists to remove; swap_hair itself clamps # hair as far as tbh*5.0, but masking that far mostly grains background in # wide shots, so this stops short of it. box = np.zeros((h, w), np.float32) rx1 = max(0, x1 - int(bw * 1.25)); ry1 = max(0, y1 - int(bh * 1.6)) rx2 = min(w, x2 + int(bw * 1.25)); ry2 = min(h, y2 + int(bh * 3.0)) mask[ry1:ry2, rx1:rx2] = 1.0 mask = cv2.GaussianBlur(mask, (0, 0), max(2.0, min(h, w) * 0.02)) box[ry1:ry2, rx1:rx2] = 1.0
mask = None try: # Same generous crop as the hair matte so long hair is parsed, not cut. # Class 18 (hat) is included HERE only: the parser labels part of the # hair as hat, and while that misfire matters when transferring hair # (_HEAD_CLASSES rightly drops it), leaving it out here punches an # ungrained hole in the top of the head. parsed = _parse_region_mask(result, face_bbox, _HEAD_CLASSES | _NECK_ONLY_CLASSES | {18}, up=1.0, down=2.6, side=1.4) if parsed.max() > 0: mask = parsed * box except Exception as e: print(f"[head_swap] harmonize parse failed, falling back to head box: {e}") if mask is None or mask.max() <= 0: mask = box
# Small feather: enough to fade at the hairline, not so wide it bleeds the # grain back out into the scene the way the old box-sized blur did. mask = cv2.GaussianBlur(mask, (0, 0), max(1.5, min(h, w) * 0.006)) m3 = mask[..., None]
# (1) match the scene's grain (estimated from the target's high-freq luminance)