From 51380df41765c4cced7ea55c478033d031ff3c74 Mon Sep 17 00:00:00 2001 From: David Chatel Date: Sun, 16 Feb 2025 18:38:12 +0100 Subject: [PATCH] wrapped warpAffine with clip(x,0,1) to ensure the image is still a proper image --- nodes.py | 8 ++++---- utils.py | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/nodes.py b/nodes.py index a9b1870..d3723c8 100644 --- a/nodes.py +++ b/nodes.py @@ -170,18 +170,18 @@ class WarpFaceBack: else: values = groups[i] crop, mask, warp = list(zip(*[x[1:] for x in values])) - warped_masks = [cv2.warpAffine(single_mask.numpy(), + warped_masks = [np.clip(cv2.warpAffine(single_mask.numpy(), cv2.invertAffineTransform(single_warp), image.shape[1::-1], - flags=cv2.INTER_LANCZOS4) + flags=cv2.INTER_LANCZOS4), 0, 1) for single_warp, single_mask in zip(warp, mask)] full_mask = np.add.reduce(warped_masks, axis=0)[...,None] swapped = np.add.reduce([ - cv2.warpAffine(single_crop.cpu().numpy(), + np.clip(cv2.warpAffine(single_crop.cpu().numpy(), cv2.invertAffineTransform(single_warp), image.shape[1::-1], flags=cv2.INTER_LANCZOS4 - ) * single_mask[..., None] + ), 0, 1) * single_mask[..., None] for single_crop, single_mask, single_warp in zip(crop, warped_masks, warp) ], axis=0) / np.maximum(1, full_mask) full_mask = np.minimum(1, full_mask) diff --git a/utils.py b/utils.py index 5895642..b6b3b65 100644 --- a/utils.py +++ b/utils.py @@ -121,6 +121,7 @@ class Face: N = N @ T4 @ S @ T3 crop = cv2.warpAffine(self.img.numpy(), N, (size, size), flags=cv2.INTER_LANCZOS4) crop = torch.from_numpy(crop)[None] + crop = torch.clip(crop, 0, 1) return N, crop