Files
BGfilter/bgfilter/cli.py
T
lhk229 62f175948e Auto-detect the background colour (default); drop the green heuristic
Replace the hardcoded green-screen auto-detection with a general flat-colour
detector. When screen_color is null the background colour is now detected from the
image border (dominant colour of the border strip) and used directly as the chroma
model, with a failure gate that raises when the border is not one clean flat colour
(gradient / texture / subject filling the frame).

- chroma: add detect_background_color + _background_border_cluster; the null case of
  estimate_background_model samples the detected cluster directly (no seed search);
  compute_bg_confidence is now always perceptual (Lab/RGB distance). Removed the green
  heuristic (initial_green_candidates), the green multiplicative gating, the now-unused
  _smoothstep, and five green-only ChromaSettings fields.
- pipeline: reuse the detected colour for de-spill in the auto case.
- settings / default.yaml: add detect_* tuning fields; refresh screen_color docs.
- README: document auto-detection + the failure gate, trimap modes, birefnet config.

A supplied --screen-color hex still uses the seed-search refinement. Validated:
detects green / pastel / text samples correctly, raises on gradient / two-colour, CLI
exits 1 on failure, green and pastel mattes unchanged in quality.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 20:34:31 +08:00

81 lines
3.9 KiB
Python

from __future__ import annotations
from pathlib import Path
import typer
from rich import print
from .config import load_settings, override_settings
from .pipeline import MattingPipeline, run_image
app = typer.Typer(help="Offline flat-background character matting.")
@app.command()
def main(
input: Path | None = typer.Option(None, "--input", "-i", exists=True, file_okay=True, dir_okay=False),
output: Path | None = typer.Option(None, "--output", "-o", file_okay=True, dir_okay=False),
input_dir: Path | None = typer.Option(None, "--input-dir", exists=True, file_okay=False, dir_okay=True),
output_dir: Path | None = typer.Option(None, "--output-dir", file_okay=False, dir_okay=True),
debug_dir: Path | None = typer.Option(None, "--debug-dir", file_okay=False, dir_okay=True),
config: Path | None = typer.Option(None, "--config", exists=True, file_okay=True, dir_okay=False),
model_name: str | None = typer.Option(None, "--model-name"),
device: str | None = typer.Option(None, "--device"),
screen_color: str | None = typer.Option(None, "--screen-color", help="Background colour prior as #RRGGBB (default: auto-detect the flat background colour)"),
matting_method: str | None = typer.Option(None, "--matting-method"),
fallback_to_chroma_alpha: bool | None = typer.Option(None, "--fallback-to-chroma-alpha/--no-fallback-to-chroma-alpha"),
sure_bg_threshold: float | None = typer.Option(None, "--sure-bg-threshold", min=0.0, max=1.0),
sure_fg_threshold: float | None = typer.Option(None, "--sure-fg-threshold", min=0.0, max=1.0),
unknown_radius_ratio: float | None = typer.Option(None, "--unknown-radius-ratio", min=0.0),
fg_safe_radius_ratio: float | None = typer.Option(None, "--fg-safe-radius-ratio", min=0.0),
despill: bool | None = typer.Option(None, "--despill/--no-despill"),
trimap_mode: str | None = typer.Option(None, "--trimap-mode", help="Trimap mode (segmentation pipeline): directional | seg | directional-hard-bg"),
) -> None:
try:
settings = override_settings(
load_settings(config),
model_name=model_name,
device=device,
screen_color=screen_color,
matting_method=matting_method,
fallback_to_chroma_alpha=fallback_to_chroma_alpha,
sure_bg_threshold=sure_bg_threshold,
sure_fg_threshold=sure_fg_threshold,
unknown_radius_ratio=unknown_radius_ratio,
fg_safe_radius_ratio=fg_safe_radius_ratio,
despill=despill,
trimap_mode=trimap_mode,
)
if input_dir is not None:
if output_dir is None:
raise typer.BadParameter("--output-dir is required when --input-dir is used")
images = sorted(
p for p in input_dir.iterdir() if p.suffix.lower() in {".png", ".jpg", ".jpeg", ".webp"}
)
if not images:
raise typer.BadParameter(f"No supported images found in {input_dir}")
pipeline = MattingPipeline(settings)
for image_path in images:
out_path = output_dir / f"{image_path.stem}_rgba.png"
dbg = None if debug_dir is None else debug_dir / image_path.stem
result = pipeline.run_image(image_path, out_path, dbg)
print(f"[green]wrote[/green] {result['output']}")
return
if input is None or output is None:
raise typer.BadParameter("Use either --input/--output or --input-dir/--output-dir")
result = run_image(input, output, debug_dir, settings)
print(f"[green]wrote[/green] {result['output']}")
except RuntimeError as exc:
print(f"[red]error:[/red] {exc}")
raise typer.Exit(code=1) from exc
except ValueError as exc:
print(f"[red]config error:[/red] {exc}")
raise typer.Exit(code=1) from exc
if __name__ == "__main__":
app()