28 lines
885 B
Python
28 lines
885 B
Python
import cv2
|
|
import numpy as np
|
|
|
|
from obfuscation.base_obfuscation_technique import ObfuscationTechnique
|
|
|
|
|
|
|
|
class Pixelation(ObfuscationTechnique):
|
|
"""
|
|
Pixelate the face ROI, but keep only the masked area pixelated.
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.blocks = 8
|
|
|
|
def obfuscate(self, face_roi_img_bgr: np.ndarray, face_mask) -> np.ndarray:
|
|
# 1) copy original
|
|
obf = face_roi_img_bgr.copy()
|
|
# 2) create a pixelated version of the ROI
|
|
h, w = face_roi_img_bgr.shape[:2]
|
|
tmp = cv2.resize(face_roi_img_bgr, (self.blocks, self.blocks), interpolation=cv2.INTER_LINEAR)
|
|
pixelated = cv2.resize(tmp, (w, h), interpolation=cv2.INTER_NEAREST)
|
|
# 3) apply pixelated only inside mask
|
|
obf[face_mask.astype(bool)] = pixelated[face_mask.astype(bool)]
|
|
return obf
|
|
|
|
def name(self) -> str:
|
|
return "Pixel" |