diff --git a/project/models/epic/.gitignore b/project/models/epic/.gitignore index e683480..c19368a 100644 --- a/project/models/epic/.gitignore +++ b/project/models/epic/.gitignore @@ -1,2 +1,3 @@ *.tar -stuff/* \ No newline at end of file +stuff/* +image_cache/* \ No newline at end of file diff --git a/project/models/epic/face_obfuscator.py b/project/models/epic/face_obfuscator.py index 9d62a46..fa53e4b 100644 --- a/project/models/epic/face_obfuscator.py +++ b/project/models/epic/face_obfuscator.py @@ -206,17 +206,69 @@ def obfuscate(swap_index, source_image, target_image, reconstruction_module, seg target_video=[resize(target_image, (256, 256))[..., :3]], reconstruction_module=reconstruction_module, segmentation_module=segmentation_module, cpu=True)[0]) - #TODO Bgr-Rgb ? + + cv2.imshow("Source", source_image) + cv2.imshow("Target", target_image) + cv2.waitKey() + cv2.imshow("TEST", model_output) - matched = match_histograms(model_output, target_image) - cv2.imshow("TEST1", model_output) - output = cv2.resize(matched, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR) + cv2.waitKey() + + #TODO entfernen + test_rescale = cv2.resize(model_output, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR) + cv2.imshow("TEST0", test_rescale) + cv2.waitKey() + ### + + + matched = match_histograms(model_output, target_image, multichannel=True) + rescaled = cv2.resize(matched, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR) + cv2.imshow("TEST1", rescaled) + cv2.waitKey() + + mask = np.full((rescaled.shape[0], rescaled.shape[1]), 255, dtype=np.uint8) #cv2.inRange(rescaled, (0,0,0), (250,250,250)) # 0 or 255 + center = (mask.shape[1] // 2, mask.shape[0] // 2) + + # 4. Blend it ------------------------------------------------------------ + output = cv2.seamlessClone( + rescaled, + target_image, + mask, # 8‑bit 1‑channel mask + center, # placement in dst + cv2.NORMAL_CLONE) # or MIXED_CLONE, MONOCHROME_TRANSFER + + cv2.imshow("TEST2", output) + cv2.waitKey() + + cv2.imshow("TEST3", binary_diff_map(rescaled, output)) + cv2.waitKey() + return output + +def binary_diff_map(img_a: np.ndarray, + img_b: np.ndarray, + thresh: int = 1) -> np.ndarray: + if img_a.shape != img_b.shape: + raise ValueError("Images must have the same shape") + + # Work in grayscale – colour difference rarely adds useful information + gray_a = cv2.cvtColor(img_a, cv2.COLOR_BGR2GRAY) if img_a.ndim == 3 else img_a + gray_b = cv2.cvtColor(img_b, cv2.COLOR_BGR2GRAY) if img_b.ndim == 3 else img_b + + # Absolute pixel-wise difference + diff = cv2.absdiff(gray_a, gray_b) + # Threshold to binary: 0 = same, 255 = changed + _, mask = cv2.threshold(diff, thresh, 255, cv2.THRESH_BINARY) + return mask + + + + class EPIC: def __init__(self, checkpoint_path, config_path, fake_face_path): self.swap_indices = [2, 3, 9, 11, 15] #Indices of face regions to swap - self.fake_face = resize(imageio.imread(fake_face_path), (256, 256))[..., :3] + self.fake_face = resize(cv2.imread(fake_face_path), (256, 256))[..., :3] self.reconstruction_module, self.segmentation_module = load_checkpoints(config_path, checkpoint_path, blend_scale=1, cpu=True) def obfuscate(self, face_roi_img_bgr: np.ndarray) -> np.ndarray: @@ -238,7 +290,7 @@ def init(): print("initiating epic") checkpoint_path = "stuff/vox-15segments.pth.tar" config_path = "config/vox-256-sem-15segments.yaml" - fake_face_path = "stuff/fake_face.jpg" + fake_face_path = "stuff/fake_face.png" global epic epic = EPIC(checkpoint_path, config_path, fake_face_path) except Exception as e: diff --git a/project/models/epic/image_cache/obfuscated_face.jpg b/project/models/epic/image_cache/obfuscated_face.jpg index c81885b..182edf5 100644 Binary files a/project/models/epic/image_cache/obfuscated_face.jpg and b/project/models/epic/image_cache/obfuscated_face.jpg differ diff --git a/project/obfuscation/__pycache__/epic.cpython-310.pyc b/project/obfuscation/__pycache__/epic.cpython-310.pyc index 803a041..aef992b 100644 Binary files a/project/obfuscation/__pycache__/epic.cpython-310.pyc and b/project/obfuscation/__pycache__/epic.cpython-310.pyc differ