Newer
Older
SmTIAS-Evaluation / src / config.py
"""設定ファイル(ROI 等)の読み込みユーティリティ."""

import json
from pathlib import Path


def load_roi_config(config_path: str, roi_key: str) -> dict | None:
    """ROI 設定ファイルを読み込んで指定キーの ROI を返す.

    Args:
        config_path: JSON 設定ファイルのパス.
        roi_key: ドット区切りのキー(例: "smtias.whiteboard").

    Returns:
        ROI の辞書(x, y, width, height).キーが存在しない場合は None.

    Raises:
        FileNotFoundError: 設定ファイルが存在しない場合.
    """
    config_file = Path(config_path)
    if not config_file.exists():
        raise FileNotFoundError(f"設定ファイルが見つかりません: {config_path}")

    with config_file.open(encoding="utf-8") as f:
        config = json.load(f)

    # ドット区切りでネストされたキーを辿る
    node = config
    for key in roi_key.split("."):
        if not isinstance(node, dict) or key not in node:
            return None
        node = node[key]

    return node if isinstance(node, dict) else None