Datasets:
ArXiv:
License:
Upload vis_data.py
Browse files- vis_data.py +130 -0
vis_data.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib as mpl
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from matplotlib.patches import Patch
|
| 6 |
+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
class CameraPoseVisualizer:
|
| 10 |
+
def __init__(self, xlim, ylim, zlim):
|
| 11 |
+
self.fig = plt.figure(figsize=(18, 7))
|
| 12 |
+
self.ax = self.fig.add_subplot(projection='3d')
|
| 13 |
+
self.plotly_data = None
|
| 14 |
+
self.ax.set_aspect("auto")
|
| 15 |
+
self.ax.set_xlim(xlim)
|
| 16 |
+
self.ax.set_ylim(ylim)
|
| 17 |
+
self.ax.set_zlim(zlim)
|
| 18 |
+
self.ax.set_xlabel('x')
|
| 19 |
+
self.ax.set_ylabel('y')
|
| 20 |
+
self.ax.set_zlabel('z')
|
| 21 |
+
print('initialize camera pose visualizer')
|
| 22 |
+
|
| 23 |
+
def extrinsic2pyramid(self, extrinsic, color_map='red', hw_ratio=9/16, base_xval=1, zval=3):
|
| 24 |
+
vertex_std = np.array([[0, 0, 0, 1],
|
| 25 |
+
[base_xval, -base_xval * hw_ratio, zval, 1],
|
| 26 |
+
[base_xval, base_xval * hw_ratio, zval, 1],
|
| 27 |
+
[-base_xval, base_xval * hw_ratio, zval, 1],
|
| 28 |
+
[-base_xval, -base_xval * hw_ratio, zval, 1]])
|
| 29 |
+
vertex_transformed = vertex_std @ extrinsic.T
|
| 30 |
+
meshes = [[vertex_transformed[0, :-1], vertex_transformed[1][:-1], vertex_transformed[2, :-1]],
|
| 31 |
+
[vertex_transformed[0, :-1], vertex_transformed[2, :-1], vertex_transformed[3, :-1]],
|
| 32 |
+
[vertex_transformed[0, :-1], vertex_transformed[3, :-1], vertex_transformed[4, :-1]],
|
| 33 |
+
[vertex_transformed[0, :-1], vertex_transformed[4, :-1], vertex_transformed[1, :-1]],
|
| 34 |
+
[vertex_transformed[1, :-1], vertex_transformed[2, :-1], vertex_transformed[3, :-1], vertex_transformed[4, :-1]]]
|
| 35 |
+
|
| 36 |
+
color = color_map if isinstance(color_map, str) else plt.cm.rainbow(color_map)
|
| 37 |
+
|
| 38 |
+
self.ax.add_collection3d(
|
| 39 |
+
Poly3DCollection(meshes, facecolors=color, linewidths=0.3, edgecolors=color, alpha=0.35))
|
| 40 |
+
|
| 41 |
+
def customize_legend(self, list_label):
|
| 42 |
+
list_handle = []
|
| 43 |
+
for idx, label in enumerate(list_label):
|
| 44 |
+
color = plt.cm.viridis(idx / len(list_label))
|
| 45 |
+
patch = Patch(color=color, label=label)
|
| 46 |
+
list_handle.append(patch)
|
| 47 |
+
plt.legend(loc='right', bbox_to_anchor=(1.8, 0.5), handles=list_handle)
|
| 48 |
+
|
| 49 |
+
def colorbar(self, max_frame_length):
|
| 50 |
+
cmap = mpl.cm.rainbow
|
| 51 |
+
norm = mpl.colors.Normalize(vmin=0, vmax=max_frame_length)
|
| 52 |
+
self.fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap), ax=self.ax, orientation='vertical', label='Frame Number')
|
| 53 |
+
|
| 54 |
+
def show(self):
|
| 55 |
+
plt.title('Extrinsic Parameters')
|
| 56 |
+
plt.savefig('extrinsic_parameters.jpg', format='jpg', dpi=300)
|
| 57 |
+
plt.show()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_args():
|
| 61 |
+
parser = argparse.ArgumentParser()
|
| 62 |
+
parser.add_argument('--pose_file_path', default='/Users/baijianhong/Documents/python_scripts/camera_extrinsics (1).json', type=str, help='the path of the pose file')
|
| 63 |
+
parser.add_argument('--hw_ratio', default=9/16, type=float, help='the height over width of the film plane')
|
| 64 |
+
parser.add_argument('--total_frame', type=int, default=81)
|
| 65 |
+
parser.add_argument('--stride', type=int, default=4)
|
| 66 |
+
parser.add_argument('--cam_idx', type=str, default="05")
|
| 67 |
+
parser.add_argument('--base_xval', type=float, default=0.08)
|
| 68 |
+
parser.add_argument('--zval', type=float, default=0.15)
|
| 69 |
+
parser.add_argument('--x_min', type=float, default=-2)
|
| 70 |
+
parser.add_argument('--x_max', type=float, default=2)
|
| 71 |
+
parser.add_argument('--y_min', type=float, default=-2)
|
| 72 |
+
parser.add_argument('--y_max', type=float, default=2)
|
| 73 |
+
parser.add_argument('--z_min', type=float, default=-1.)
|
| 74 |
+
parser.add_argument('--z_max', type=float, default=1)
|
| 75 |
+
return parser.parse_args()
|
| 76 |
+
|
| 77 |
+
def get_c2w(w2cs, transform_matrix, relative_c2w=True):
|
| 78 |
+
if relative_c2w:
|
| 79 |
+
target_cam_c2w = np.array([
|
| 80 |
+
[1, 0, 0, 0],
|
| 81 |
+
[0, 1, 0, 0],
|
| 82 |
+
[0, 0, 1, 0],
|
| 83 |
+
[0, 0, 0, 1]
|
| 84 |
+
])
|
| 85 |
+
abs2rel = target_cam_c2w @ w2cs[0]
|
| 86 |
+
ret_poses = [target_cam_c2w, ] + [abs2rel @ np.linalg.inv(w2c) for w2c in w2cs[1:]]
|
| 87 |
+
else:
|
| 88 |
+
ret_poses = [np.linalg.inv(w2c) for w2c in w2cs]
|
| 89 |
+
ret_poses = [transform_matrix @ x for x in ret_poses]
|
| 90 |
+
return np.array(ret_poses, dtype=np.float32)
|
| 91 |
+
|
| 92 |
+
def parse_matrix(matrix_str):
|
| 93 |
+
rows = matrix_str.strip().split('] [')
|
| 94 |
+
matrix = []
|
| 95 |
+
for row in rows:
|
| 96 |
+
row = row.replace('[', '').replace(']', '')
|
| 97 |
+
if len((list(map(float, row.split())))) == 3:
|
| 98 |
+
matrix.append((list(map(float, row.split()))) +[0.])
|
| 99 |
+
else:
|
| 100 |
+
matrix.append(list(map(float, row.split())))
|
| 101 |
+
return np.array(matrix)
|
| 102 |
+
|
| 103 |
+
if __name__ == '__main__':
|
| 104 |
+
args = get_args()
|
| 105 |
+
|
| 106 |
+
with open(args.pose_file_path, 'r') as file:
|
| 107 |
+
data = json.load(file)
|
| 108 |
+
cameras = [parse_matrix(data[f"frame{i}"][f"cam{args.cam_idx}"]) for i in range(0, args.total_frame, args.stride)]
|
| 109 |
+
cameras = np.transpose(np.stack(cameras), (0, 2, 1))
|
| 110 |
+
|
| 111 |
+
w2cs = []
|
| 112 |
+
for cam in cameras:
|
| 113 |
+
if cam.shape[0] == 3:
|
| 114 |
+
cam = np.vstack((cam, np.array([[0, 0, 0, 1]])))
|
| 115 |
+
cam = cam[:, [1, 2, 0, 3]]
|
| 116 |
+
cam[:3, 1] *= -1.
|
| 117 |
+
w2cs.append(np.linalg.inv(cam))
|
| 118 |
+
transform_matrix = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0], [0, 0, 0, 1]])
|
| 119 |
+
c2ws = get_c2w(w2cs, transform_matrix, True)
|
| 120 |
+
scale = max(max(abs(c2w[:3, 3])) for c2w in c2ws)
|
| 121 |
+
if scale > 1e-3: # otherwise, pan or tilt
|
| 122 |
+
for c2w in c2ws:
|
| 123 |
+
c2w[:3, 3] /= scale
|
| 124 |
+
|
| 125 |
+
visualizer = CameraPoseVisualizer([args.x_min, args.x_max], [args.y_min, args.y_max], [args.z_min, args.z_max])
|
| 126 |
+
for frame_idx, c2w in enumerate(c2ws):
|
| 127 |
+
visualizer.extrinsic2pyramid(c2w, frame_idx / len(cameras), hw_ratio=args.hw_ratio, base_xval=args.base_xval,
|
| 128 |
+
zval=(args.zval))
|
| 129 |
+
visualizer.colorbar(len(cameras))
|
| 130 |
+
visualizer.show()
|