Add sample smoke check and refine foreground despill
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(REPO_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
from bgfilter.pipeline import MattingPipeline
|
||||
from bgfilter.quality import assert_quality, measure_rgba
|
||||
from bgfilter.settings import (
|
||||
AlphaPostSettings,
|
||||
ChromaSettings,
|
||||
DespillSettings,
|
||||
ModelSettings,
|
||||
PipelineSettings,
|
||||
TrimapSettings,
|
||||
)
|
||||
|
||||
|
||||
REQUIRED_DEBUG_FILES = {
|
||||
"bg_confidence.png",
|
||||
"trimap.png",
|
||||
"alpha.png",
|
||||
"despill_mask.png",
|
||||
"preview_black.png",
|
||||
"preview_white.png",
|
||||
"preview_gray.png",
|
||||
"preview_red.png",
|
||||
"preview_blue.png",
|
||||
"qa_grid.png",
|
||||
"metadata.json",
|
||||
}
|
||||
|
||||
|
||||
def build_settings(args: argparse.Namespace) -> PipelineSettings:
|
||||
return PipelineSettings(
|
||||
chroma=ChromaSettings(),
|
||||
trimap=TrimapSettings(),
|
||||
alpha_post=AlphaPostSettings(),
|
||||
despill=DespillSettings(),
|
||||
model=ModelSettings(
|
||||
model_name=args.model_name,
|
||||
device=args.device,
|
||||
matting_method=args.matting_method,
|
||||
fallback_to_chroma_alpha=args.fallback_to_chroma_alpha,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def check_debug_files(debug_dir: Path) -> list[str]:
|
||||
missing = []
|
||||
for filename in sorted(REQUIRED_DEBUG_FILES):
|
||||
path = debug_dir / filename
|
||||
if not path.exists() or path.stat().st_size <= 0:
|
||||
missing.append(filename)
|
||||
return missing
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Run sample-image smoke checks.")
|
||||
parser.add_argument("--samples-dir", type=Path, default=Path("Samples"))
|
||||
parser.add_argument("--output-dir", type=Path, default=Path("Outputs/smoke_samples"))
|
||||
parser.add_argument("--device", default="cpu")
|
||||
parser.add_argument("--matting-method", default="vitmatte", choices=["vitmatte", "chroma"])
|
||||
parser.add_argument("--model-name", default="hustvl/vitmatte-small-composition-1k")
|
||||
parser.add_argument("--fallback-to-chroma-alpha", action="store_true")
|
||||
parser.add_argument("--max-edge-green-excess-p95", type=float, default=0.50)
|
||||
args = parser.parse_args()
|
||||
|
||||
images = sorted(args.samples_dir.glob("*.png"))
|
||||
if not images:
|
||||
raise SystemExit(f"No PNG samples found in {args.samples_dir}")
|
||||
|
||||
pipeline = MattingPipeline(build_settings(args))
|
||||
failed = False
|
||||
for image_path in images:
|
||||
output_path = args.output_dir / f"{image_path.stem}_rgba.png"
|
||||
debug_dir = args.output_dir / f"{image_path.stem}_debug"
|
||||
result = pipeline.run_image(image_path, output_path, debug_dir)
|
||||
metrics = measure_rgba(output_path)
|
||||
failures = assert_quality(metrics, args.max_edge_green_excess_p95)
|
||||
missing_debug = check_debug_files(debug_dir)
|
||||
if missing_debug:
|
||||
failures.append(f"missing debug files: {', '.join(missing_debug)}")
|
||||
|
||||
print(
|
||||
f"{image_path.name}: alpha_source={result['alpha_source']} "
|
||||
f"edge_green_p95={metrics.edge_green_excess_p95:.4f}"
|
||||
)
|
||||
for failure in failures:
|
||||
print(f" FAIL {failure}")
|
||||
failed = failed or bool(failures)
|
||||
|
||||
return 1 if failed else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user