Files
obfuscation_platform/project/obfuscation/blurring.py
T

32 lines
1.0 KiB
Python

from obfuscation.base_obfuscation_technique import ObfuscationTechnique
import numpy as np
import cv2
class Blurring(ObfuscationTechnique):
"""
Gaussian-blur the face ROI, but only show the blurred pixels inside the mask.
"""
def __init__(self):
self.strength = 0.5 # 50% of smaller dim (w/h)
@staticmethod
def _calc_kernel(h, w, strength) -> int:
k = int(round(strength * min(h, w)))
k = k | 1 # make odd
k = max(3, k)
return k
def obfuscate(self, face_roi_img_bgr: np.ndarray, face_mask) -> np.ndarray:
# 1) copy original
obf = face_roi_img_bgr.copy()
# 2) compute full-image blur
h, w = face_roi_img_bgr.shape[:2]
k = self._calc_kernel(h, w, self.strength)
blurred = cv2.GaussianBlur(face_roi_img_bgr, ksize=(k, k), sigmaX=0, sigmaY=0)
# 3) blend only mask
obf[face_mask.astype(bool)] = blurred[face_mask.astype(bool)]
return obf
def name(self) -> str:
return "Blur"