clean up of features
This commit is contained in:
@ -175,30 +175,40 @@ def _remove_with_mediapipe(
|
||||
raise RuntimeError(f"MediaPipe background removal failed: {e}")
|
||||
|
||||
|
||||
|
||||
def _remove_with_ffmpeg_portrait(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
replacement: str = "blur",
|
||||
replacement_value: str = "",
|
||||
) -> str:
|
||||
"""Fallback: use FFmpeg's colorkey + chromakey for basic background removal.
|
||||
"""Fallback: basic FFmpeg-only background blur.
|
||||
|
||||
This is a crude approximation. For best results, install mediapipe + opencv-python.
|
||||
Uses a strong gaussian blur as a crude background replacement.
|
||||
For proper person segmentation (color/image replacement), install:
|
||||
pip install mediapipe opencv-python
|
||||
"""
|
||||
ffmpeg = "ffmpeg"
|
||||
|
||||
# Use a simple chromakey-based approach with a neutral background
|
||||
# This won't work well for most real videos but provides a fallback
|
||||
if replacement == "color":
|
||||
if replacement == "blur":
|
||||
filter_complex = "gblur=sigma=30"
|
||||
elif replacement == "color":
|
||||
color = replacement_value or "00FF00"
|
||||
filter_complex = f"colorkey=0x{color}:0.3:0.1,chromakey=0x{color}:0.3:0.1"
|
||||
elif replacement == "blur":
|
||||
filter_complex = "gblur=sigma=20:enable='gt(scene,0.01)'"
|
||||
filter_complex = (
|
||||
f"split[fg][bg];"
|
||||
f"[bg]colorkey=0x{color}:0.3:0.1[bg_key];"
|
||||
f"[fg][bg_key]overlay"
|
||||
)
|
||||
elif replacement == "image" and replacement_value:
|
||||
escaped = replacement_value.replace("\\", "/").replace(":", "\\:")
|
||||
filter_complex = (
|
||||
f"movie='{escaped}':loop=0,scale=iw:ih[bg];"
|
||||
f"[0:v][bg]overlay=0:0:shortest=1"
|
||||
)
|
||||
else:
|
||||
filter_complex = "null"
|
||||
|
||||
if filter_complex == "null":
|
||||
# No-op, copy input to output
|
||||
cmd = [ffmpeg, "-y", "-i", input_path, "-c", "copy", output_path]
|
||||
else:
|
||||
cmd = [
|
||||
@ -215,5 +225,8 @@ def _remove_with_ffmpeg_portrait(
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"FFmpeg background removal failed: {result.stderr[-500:]}")
|
||||
|
||||
logger.info("FFmpeg portait background removal completed -> %s", output_path)
|
||||
logger.warning(
|
||||
"FFmpeg fallback background removal used (no MediaPipe). "
|
||||
"Install 'mediapipe' and 'opencv-python' for proper person segmentation."
|
||||
)
|
||||
return output_path
|
||||
|
||||
Reference in New Issue
Block a user