"""Normalize pose JSON to a single schema (keypoints + conf + joint index).""" from __future__ import annotations import json import shutil from pathlib import Path from typing import Any, Optional def unify_pose_record(raw: dict[str, Any], image_size: tuple[int, int]) -> dict[str, Any]: cand = raw.get("candidate") if not cand: return {"format": "empty", "image_size": list(image_size), "keypoints": []} kps = [] for row in cand: if len(row) >= 4: kps.append([float(row[0]), float(row[1]), float(row[2]), int(row[3])]) return { "format": "equifashion_keypoints_v1", "image_size": list(image_size), "keypoints": kps, } def copy_or_unify_pose( src_json: Optional[Path], dst_json: Path, image_size: tuple[int, int], ) -> bool: if src_json is None or not src_json.is_file(): return False with open(src_json, encoding="utf-8") as f: raw = json.load(f) out = unify_pose_record(raw, image_size) dst_json.parent.mkdir(parents=True, exist_ok=True) with open(dst_json, "w", encoding="utf-8") as f: json.dump(out, f, ensure_ascii=False, indent=2) return True def copy_raw_pose(src: Path, dst: Path) -> None: dst.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(src, dst)