From a16abf2ada052979f1cea2bde93654b917fd5f9b Mon Sep 17 00:00:00 2001 From: fiatcode Date: Sat, 28 Feb 2026 10:53:33 +0700 Subject: [PATCH] fix: clamp heavy blur kernel sizes, allow tilde in paths --- src/faceblur/app.py | 4 ++-- src/faceblur/blur.py | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/faceblur/app.py b/src/faceblur/app.py index 5c727fb..2c130ed 100644 --- a/src/faceblur/app.py +++ b/src/faceblur/app.py @@ -67,13 +67,13 @@ def run() -> None: # 1. Input gathering video_str = questionary.path( "Enter path to video file:", - validate=lambda p: Path(p).is_file() or "File does not exist", + validate=lambda p: Path(p).expanduser().is_file() or "File does not exist", ).ask() if not video_str: return - video_path = Path(video_str) + video_path = Path(video_str).expanduser() interval_str = questionary.text( "Frame interval for face detection (default: 30):", diff --git a/src/faceblur/blur.py b/src/faceblur/blur.py index b3e985d..e1b5557 100644 --- a/src/faceblur/blur.py +++ b/src/faceblur/blur.py @@ -40,8 +40,9 @@ def apply_blur( face_region = image[y1:y2, x1:x2] if method == "gaussian": - ksize = int(face_region.shape[0] * strength) | 1 # Must be odd - ksize = max(ksize, 3) + # Calculate kernel size based on face size, but clamp to max 99 to save CPU + ksize = int(face_region.shape[0] * (strength / 10.0)) | 1 + ksize = max(3, min(ksize, 99)) blurred = cv2.GaussianBlur(face_region, (ksize, ksize), 0) elif method == "pixelate": ph = max(1, int(face_region.shape[0] / (strength * 2))) @@ -55,8 +56,8 @@ def apply_blur( elif method == "blackout": blurred = np.zeros_like(face_region) elif method == "elliptical": - ksize = int(face_region.shape[0] * strength) | 1 - ksize = max(ksize, 3) + ksize = int(face_region.shape[0] * (strength / 10.0)) | 1 + ksize = max(3, min(ksize, 99)) full_blur = cv2.GaussianBlur(face_region, (ksize, ksize), 0) mask = np.zeros(face_region.shape[:2], dtype=np.uint8) center = (face_region.shape[1] // 2, face_region.shape[0] // 2) @@ -65,8 +66,8 @@ def apply_blur( mask_3ch = cv2.merge([mask, mask, mask]) blurred = np.where(mask_3ch > 0, full_blur, face_region) elif method == "median": - ksize = int(face_region.shape[0] * strength) | 1 - ksize = max(ksize, 3) + ksize = int(face_region.shape[0] * (strength / 10.0)) | 1 + ksize = max(3, min(ksize, 99)) blurred = cv2.medianBlur(face_region, ksize) else: raise ValueError(f"Unknown blur method: {method}")