Add capture readiness quality gate
Browse files- assess_capture.py +89 -0
assess_capture.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Assess basic image quality in a calibrated capture manifest.
|
| 2 |
+
|
| 3 |
+
This is a capture-readiness diagnostic, not a reconstruction-quality metric.
|
| 4 |
+
It deliberately uses only Pillow/NumPy so it can run before model setup.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import argparse
|
| 10 |
+
import json
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
from PIL import Image
|
| 15 |
+
|
| 16 |
+
from validate_capture_manifest import validate_capture_manifest
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _view_quality(path: Path) -> dict:
|
| 20 |
+
with Image.open(path) as image:
|
| 21 |
+
gray = np.asarray(image.convert("L").resize((256, 256), Image.Resampling.BILINEAR), dtype=np.float32)
|
| 22 |
+
horizontal = np.diff(gray, axis=1)
|
| 23 |
+
vertical = np.diff(gray, axis=0)
|
| 24 |
+
mean = float(gray.mean())
|
| 25 |
+
contrast = float(gray.std())
|
| 26 |
+
sharpness = float((np.square(horizontal).mean() + np.square(vertical).mean()) / 2.0)
|
| 27 |
+
clipped_fraction = float(((gray <= 2) | (gray >= 253)).mean())
|
| 28 |
+
warnings = []
|
| 29 |
+
if mean < 20:
|
| 30 |
+
warnings.append("very_dark")
|
| 31 |
+
if mean > 235:
|
| 32 |
+
warnings.append("very_bright")
|
| 33 |
+
if contrast < 10:
|
| 34 |
+
warnings.append("low_contrast")
|
| 35 |
+
if sharpness < 8:
|
| 36 |
+
warnings.append("likely_blurred")
|
| 37 |
+
if clipped_fraction > 0.35:
|
| 38 |
+
warnings.append("heavy_clipping")
|
| 39 |
+
return {
|
| 40 |
+
"mean_luminance": round(mean, 4),
|
| 41 |
+
"luminance_std": round(contrast, 4),
|
| 42 |
+
"finite_difference_sharpness": round(sharpness, 4),
|
| 43 |
+
"clipped_fraction": round(clipped_fraction, 6),
|
| 44 |
+
"warnings": warnings,
|
| 45 |
+
"ready": not warnings,
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def assess_capture(manifest_path: str | Path) -> dict:
|
| 50 |
+
manifest_file = Path(manifest_path).resolve()
|
| 51 |
+
manifest = json.loads(manifest_file.read_text(encoding="utf-8"))
|
| 52 |
+
validation_ok, validation_errors = validate_capture_manifest(manifest_file, check_files=True)
|
| 53 |
+
if not validation_ok:
|
| 54 |
+
raise ValueError("capture manifest is invalid: " + "; ".join(validation_errors))
|
| 55 |
+
results = []
|
| 56 |
+
for view in manifest["views"]:
|
| 57 |
+
image_path = manifest_file.parent / view["image"]
|
| 58 |
+
quality = _view_quality(image_path)
|
| 59 |
+
results.append({"id": view["id"], "split": view["split"], "image": view["image"], **quality})
|
| 60 |
+
warning_count = sum(len(item["warnings"]) for item in results)
|
| 61 |
+
return {
|
| 62 |
+
"project": manifest.get("project", "Simam3D"),
|
| 63 |
+
"mode": "capture_readiness_diagnostic",
|
| 64 |
+
"manifest": str(manifest_file),
|
| 65 |
+
"view_count": len(results),
|
| 66 |
+
"held_out_view_count": sum(item["split"] == "held_out" for item in results),
|
| 67 |
+
"views_with_warnings": sum(bool(item["warnings"]) for item in results),
|
| 68 |
+
"warning_count": warning_count,
|
| 69 |
+
"ready_for_reconstruction": warning_count == 0,
|
| 70 |
+
"interpretation": "source-media diagnostic only; it does not certify calibration or reconstruction quality",
|
| 71 |
+
"views": results,
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def main(argv: list[str] | None = None) -> int:
|
| 76 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 77 |
+
parser.add_argument("manifest", type=Path)
|
| 78 |
+
parser.add_argument("--output", type=Path)
|
| 79 |
+
args = parser.parse_args(argv)
|
| 80 |
+
report = assess_capture(args.manifest)
|
| 81 |
+
destination = args.output or args.manifest.with_name("capture_quality.json")
|
| 82 |
+
destination.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
| 83 |
+
print(json.dumps(report, indent=2))
|
| 84 |
+
print(f"capture quality report: {destination}")
|
| 85 |
+
return 0
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
raise SystemExit(main())
|