1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| import cv2 import numpy as np
class ImageAnonymizer: """图像匿名化工具""" def __init__(self, face_detector=None, plate_detector=None): self.face_detector = face_detector or self._load_face_detector() self.plate_detector = plate_detector def anonymize(self, image: np.ndarray, method: str = 'blur') -> np.ndarray: """匿名化图像 Args: image: BGR 图像 method: 'blur' | 'pixelate' | 'solid' | 'silhouette' Returns: 匿名化后的图像 """ result = image.copy() faces = self._detect_faces(image) for (x, y, w, h) in faces: if method == 'blur': result = self._blur_region(result, x, y, w, h) elif method == 'pixelate': result = self._pixelate_region(result, x, y, w, h) elif method == 'solid': result = self._solid_region(result, x, y, w, h) elif method == 'silhouette': result = self._silhouette_region(result, x, y, w, h) return result def _detect_faces(self, image: np.ndarray) -> list: """检测人脸""" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = self.face_detector.detectMultiScale(gray, 1.1, 4) return faces def _blur_region(self, image, x, y, w, h, ksize: int = 51) -> np.ndarray: """模糊区域""" roi = image[y:y+h, x:x+w] blurred = cv2.GaussianBlur(roi, (ksize, ksize), 0) image[y:y+h, x:x+w] = blurred return image def _pixelate_region(self, image, x, y, w, h, block_size: int = 10) -> np.ndarray: """像素化区域""" roi = image[y:y+h, x:x+w] h_blocks = max(1, h // block_size) w_blocks = max(1, w // block_size) small = cv2.resize(roi, (w_blocks, h_blocks), interpolation=cv2.INTER_LINEAR) pixelated = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST) image[y:y+h, x:x+w] = pixelated return image def _solid_region(self, image, x, y, w, h, color=(0, 0, 0)) -> np.ndarray: """实心填充""" cv2.rectangle(image, (x, y), (x+w, y+h), color, -1) return image def _silhouette_region(self, image, x, y, w, h, color=(128, 128, 128)) -> np.ndarray: """剪影填充""" cv2.ellipse(image, (x+w//2, y+h//2), (w//2, h//2), 0, 0, 360, color, -1) return image def _load_face_detector(self): """加载人脸检测器""" return cv2.CascadeClassifier( cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' )
class BatchAnonymizer: """批量匿名化工具""" def __init__(self, input_dir: str, output_dir: str): self.input_dir = input_dir self.output_dir = output_dir self.anonymizer = ImageAnonymizer() def process_all(self, method: str = 'blur'): """处理所有图像""" import os from pathlib import Path input_path = Path(self.input_dir) output_path = Path(self.output_dir) output_path.mkdir(parents=True, exist_ok=True) for ext in ['*.jpg', '*.png', '*.jpeg']: for img_file in input_path.glob(ext): image = cv2.imread(str(img_file)) anonymized = self.anonymizer.anonymize(image, method) output_file = output_path / img_file.name cv2.imwrite(str(output_file), anonymized) print(f"Anonymized: {img_file.name}")
|