Added some code (not finished) to extract face mask for obfuscation

This commit is contained in:
2025-08-01 12:34:16 +02:00
parent 7d6a3e63ec
commit 2bbc3945b3
13 changed files with 55 additions and 32 deletions
@@ -0,0 +1,16 @@
import base64
import cv2
import numpy as np
def mask_to_png_base64(mask: np.ndarray) -> str:
if mask.dtype != np.uint8:
mask = (mask > 0).astype(np.uint8) * 255
ok, buf = cv2.imencode(".png", mask)
if not ok:
raise RuntimeError("PNG encoding failed")
return base64.b64encode(buf).decode("ascii")
def png_base64_to_mask(png_b64: str) -> np.ndarray:
buf = np.frombuffer(base64.b64decode(png_b64), dtype=np.uint8)
m = cv2.imdecode(buf, cv2.IMREAD_GRAYSCALE)
return (m > 0).astype(np.uint8)