Obfuscation code adapted to be usable with server
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
*.tar
|
||||
stuff/*
|
||||
stuff/*
|
||||
image_cache/*
|
||||
@@ -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:
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
Binary file not shown.
Reference in New Issue
Block a user