#!/usr/bin/env python3 """ Step 2 — Calibration from per-image JSON feature files. 2a. Mono intrinsics per camera folder 2b. Stereo calibration: left camera vs each available partner (rc, rg, ir) with time-window pairing (default 0.1 s) """ import sys from pathlib import Path sys.path.insert(0, str(Path.home() / "Speckle-Scanner")) sys.path.insert(0, str(Path(__file__).resolve().parent)) import argparse from calibrationclasses.calibration_engine import ( run_mono_calibration, run_stereo_calibration, ) from calibrationclasses.cli_common import ( add_board_args, add_session_args, add_troubleshooting_arg, build_board_config, resolve_input_path, ) from calibrationclasses.session import STEREO_PARTNERS def main(): parser = argparse.ArgumentParser( description="Calibration step 2: mono + stereo calibration from JSON" ) add_session_args(parser) add_board_args(parser) parser.add_argument( "--step", choices=("mono", "stereo", "all"), default="all", help="Run mono intrinsics, stereo pairs, or both (default: all)", ) parser.add_argument( "--left_camera", default="lc", choices=("lc", "lc-ir", "lc_ir"), help="Left camera for stereo calibration (default: lc)", ) parser.add_argument( "--time_window", type=float, default=0.1, help="Max |t_left - t_right| in seconds for stereo pairing (default: 0.1)", ) parser.add_argument( "--partners", type=str, default="rc,rg,ir", help="Comma-separated right cameras for stereo (default: rc,rg,ir)", ) add_troubleshooting_arg(parser) args = parser.parse_args() left_camera = args.left_camera.lower().replace("_", "-") partners = tuple(p.strip() for p in args.partners.split(",") if p.strip()) board_sizes, square_sizes = build_board_config(args) input_path = resolve_input_path(args) print(f"[calibrate] session: {input_path}") mono_results = {} if args.step in ("mono", "all"): print("\n=== Step 2a: Mono intrinsics ===") mono_results = run_mono_calibration( input_path, board_sizes, square_sizes, troubleshooting=args.troubleshooting, ) if args.step in ("stereo", "all"): print("\n=== Step 2b: Stereo calibration ===") if not mono_results and args.step == "stereo": mono_results = run_mono_calibration( input_path, board_sizes, square_sizes, troubleshooting=args.troubleshooting, ) run_stereo_calibration( input_path, left_camera=left_camera, mono_results=mono_results, board_sizes=board_sizes, square_sizes=square_sizes, time_window_sec=args.time_window, partners=partners or STEREO_PARTNERS, troubleshooting=args.troubleshooting, ) print("[calibrate] done") if __name__ == "__main__": main()