Files
obfuscation_platform/project/obfuscation/pixelation.py
T
2025-07-07 15:19:31 +02:00

26 lines
913 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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"