File size: 3,572 Bytes
c1a4584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Convert a per-frame layout JSON from pose frame to camera frame.

Applies the same transformation as visualize_mesh_3d_video.py:
  1. Reorder translation [tx,ty,tz] -> [tz,tx,ty] and scale by --translation-scale
  2. Convert rotation and translation from pose frame (x-fwd, y-left, z-up)
     to camera frame (x-right, y-down, z-fwd)

Usage:
  python convert_layout_to_camera_frame.py \
    --input /path/to/video_dir/obj_tracking_out/<object>/combined_visualization/layout.json \
    --output /path/to/video_dir/obj_tracking_out/<object>/combined_visualization/layout_camera_frame.json
"""

import argparse
import json
import numpy as np
from scipy.spatial.transform import Rotation as R


# Pose frame -> Camera frame rotation matrix (for row vectors: v_cam = v_pose @ P)
# x_cam = -y_pose, y_cam = -z_pose, z_cam = x_pose
P = np.array([
    [0, 0, 1],
    [-1, 0, 0],
    [0, -1, 0],
], dtype=np.float64)


def convert_translation(tx, ty, tz, translation_scale):
    """Reorder and scale translation, then convert to camera frame."""
    # Reorder as done in visualize_mesh_3d_video.py
    t_pose = np.array([tz * translation_scale, tx * translation_scale, ty * translation_scale])
    # Convert to camera frame: v_cam = v_pose @ P
    t_cam = t_pose @ P
    return t_cam.tolist()


def convert_rotation(quat_wxyz):
    """Convert rotation from pose frame to camera frame."""
    # new_quat is wxyz
    quat_xyzw = [quat_wxyz[1], quat_wxyz[2], quat_wxyz[3], quat_wxyz[0]]
    R_pose = R.from_quat(quat_xyzw).as_matrix()
    # R_cam = P.T @ R_pose  (change of basis)
    R_cam = P.T @ R_pose
    quat_cam_xyzw = R.from_matrix(R_cam).as_quat()  # returns xyzw
    quat_cam_wxyz = [float(quat_cam_xyzw[3]), float(quat_cam_xyzw[0]),
                     float(quat_cam_xyzw[1]), float(quat_cam_xyzw[2])]
    return quat_cam_wxyz


def main():
    parser = argparse.ArgumentParser(
        description='Convert layout JSON poses from pose frame to camera frame.',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument('--input', type=str, required=True, help='Input layout JSON')
    parser.add_argument('--output', type=str, required=True, help='Output layout JSON')
    parser.add_argument('--scale', type=float, default=None,
                        help='Mesh scale (written to output for reference, does not affect transform)')
    parser.add_argument('--translation-scale', type=float, default=1.0,
                        help='Scale factor for translation values')
    args = parser.parse_args()

    with open(args.input) as f:
        data = json.load(f)

    for obj in data["objects"]:
        pose = obj["local_to_scene"]
        tx, ty, tz = pose["translation"]
        quat_wxyz = pose["new_quat"]

        t_cam = convert_translation(tx, ty, tz, args.translation_scale)
        q_cam = convert_rotation(quat_wxyz)

        pose["translation_camera_frame"] = t_cam
        pose["quat_wxyz_camera_frame"] = q_cam

    data["frame"] = "camera_frame"
    data["note"] = (
        "Poses converted to camera frame (x-right, y-down, z-fwd). "
        f"translation_scale={args.translation_scale}. "
        "Original pose-frame values preserved."
    )
    if args.scale is not None:
        data["mesh_scale"] = args.scale

    with open(args.output, 'w') as f:
        json.dump(data, f, indent=2)

    num = len([o for o in data["objects"] if "frame_idx" in o or "frame_index" in o])
    print(f"Converted {num} frames to camera frame -> {args.output}")


if __name__ == "__main__":
    main()