fix: clamp heavy blur kernel sizes, allow tilde in paths

This commit is contained in:
fiatcode 2026-02-28 10:53:33 +07:00
parent 00aaadf1ea
commit a16abf2ada
2 changed files with 9 additions and 8 deletions

View file

@ -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):",

View file

@ -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}")