26 lines
913 B
Python
26 lines
913 B
Python
import cv2
|
||
import numpy as np
|
||
|
||
from obfuscation.base_obfuscation_technique import ObfuscationTechnique
|
||
|
||
|
||
class Pixelation(ObfuscationTechnique):
|
||
"""
|
||
Downscale then upscale the patch to create square “pixels”.
|
||
pixel_size: int – edge length of each mosaic block in output pixels.
|
||
"""
|
||
|
||
def __init__(self, pixel_size: int = 16):
|
||
self.pixel_size = max(2, pixel_size)
|
||
|
||
def obfuscate(self, face_roi_img_bgr: np.ndarray) -> np.ndarray:
|
||
h, w = face_roi_img_bgr.shape[:2]
|
||
# Compute new size that is an integer multiple of pixel_size
|
||
w_small = max(1, w // self.pixel_size)
|
||
h_small = max(1, h // self.pixel_size)
|
||
# Downscale → upscale
|
||
tmp = cv2.resize(face_roi_img_bgr, (w_small, h_small), interpolation=cv2.INTER_LINEAR)
|
||
return cv2.resize(tmp, (w, h), interpolation=cv2.INTER_NEAREST)
|
||
|
||
def name(self):
|
||
return "Pixel" |