File size: 3,266 Bytes
20fa7e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
"""
Extract human poses from video using PromptHMR

Usage:
    python scripts/extract_pose.py --project data/video_001
    python scripts/extract_pose.py --project data/video_001 --static-camera
"""

import argparse
import sys
import subprocess
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from video2robot.config import PROMPTHMR_DIR
from video2robot.pose.extractor import convert_all_prompthmr_tracks_to_smplx
from video2robot.utils import emit_progress


def run_prompthmr(video_path: Path, output_dir: Path, static_camera: bool = False) -> Path:
    """Run PromptHMR pipeline via subprocess
    
    Args:
        video_path: Input video path
        output_dir: Output directory (project folder)
        static_camera: Assume static camera (skip SLAM)
    
    Returns:
        Path to results directory
    """
    script_path = PROMPTHMR_DIR / "scripts" / "run_pipeline.py"
    output_folder = str(output_dir.absolute())
    
    cmd = [
        "python", str(script_path),
        "--input_video", str(video_path.absolute()),
        "--output_folder", output_folder,
    ]
    
    if static_camera:
        cmd.append("--static_camera")
    
    print(f"[PromptHMR] Running pipeline...")
    print(f"[PromptHMR] Output folder: {output_folder}")
    print(f"[PromptHMR] Command: {' '.join(cmd)}")
    emit_progress("init", 0.05, "Starting pipeline")
    print()
    
    result = subprocess.run(cmd, cwd=str(PROMPTHMR_DIR))
    
    if result.returncode != 0:
        raise RuntimeError(f"PromptHMR failed with return code {result.returncode}")
    
    if not output_dir.exists():
        raise FileNotFoundError(f"Results not found: {output_dir}")
    
    return output_dir


def convert_prompthmr_results(project_dir: Path, output_path: Path, video_path: Path):
    """Convert PromptHMR results to SMPL-X and export every tracked person."""
    emit_progress("smplx", 0.90, "SMPL-X conversion")
    convert_all_prompthmr_tracks_to_smplx(
        results_dir=project_dir,
        output_path=output_path,
        video_path=video_path,
    )


def main():
    parser = argparse.ArgumentParser(description="Extract poses from video using PromptHMR")
    parser.add_argument("--project", "-p", required=True, 
                        help="Project folder path (e.g., data/video_001)")
    parser.add_argument("--static-camera", action="store_true", 
                        help="Assume static camera (skip SLAM)")

    args = parser.parse_args()

    project_dir = Path(args.project)
    if not project_dir.exists():
        parser.error(f"Project not found: {project_dir}")
    
    video_path = project_dir / "original.mp4"
    if not video_path.exists():
        parser.error(f"Video not found: {video_path}")
    
    output_path = project_dir / "smplx.npz"
    
    print(f"[Project] {project_dir}")
    print(f"[Input]   {video_path}")
    print(f"[Output]  {output_path}")
    
    run_prompthmr(video_path, project_dir, args.static_camera)
    convert_prompthmr_results(project_dir, output_path, video_path)

    emit_progress("done", 1.0, "Done")
    print(f"\nDone!")
    print(f"  Project: {project_dir}")
    print(f"  SMPL-X:  {output_path}")


if __name__ == "__main__":
    main()