#!/usr/bin/env python3
"""从本地视频中检测、跟踪行人，并导出步态序列图片目录。"""

from __future__ import annotations

import argparse
import os
from pathlib import Path

from persondet_numpy import Config, run_image, run_video


def main() -> int:
    parser = argparse.ArgumentParser(description="银河水滴步态检测跟踪示例")
    parser.add_argument("input", help="本地图片或视频路径")
    parser.add_argument("--output", default="output", help="输出目录，默认 ./output")
    parser.add_argument("--min-frames", type=int, default=20, help="保存序列的最少帧数")
    parser.add_argument("--max-frames", type=int, default=120, help="单个序列最多保存帧数")
    args = parser.parse_args()

    input_path = Path(args.input).expanduser().resolve()
    if not input_path.exists():
        raise FileNotFoundError(input_path)

    cfg = Config(min_frames=args.min_frames, max_frames=args.max_frames)
    output_dir = Path(args.output).expanduser().resolve()
    output_dir.mkdir(parents=True, exist_ok=True)

    old_cwd = Path.cwd()
    try:
        os.chdir(output_dir)
        if input_path.suffix.lower() in {".jpg", ".jpeg", ".png", ".bmp"}:
            run_image(input_path, cfg)
        else:
            run_video(input_path, cfg)
    finally:
        os.chdir(old_cwd)

    print(f"输出目录：{output_dir}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
