Commit bcbbf321 authored by VIOLANTE Nicolas's avatar VIOLANTE Nicolas
Browse files

add proper names to cameras in ps

parent 6181d1c8
......@@ -4,10 +4,10 @@ from scipy.spatial.transform import Rotation
from collections import namedtuple
import polyscope as ps
ObjectBox = namedtuple("ObjectBox", ["coord_system", "vertices", "faces", "name"])
CoordSystem = namedtuple("CoordSystem", ["basis", "origin"])
interp_coef=[0.332, 0.667]
faces = np.array(
[
[3, 2, 0],
......@@ -73,39 +73,6 @@ def get_transform_in_sys(object_sys, transformation):
)
def to_eg3d(extrinsics, height):
eg3d_camera_sys = CoordSystem(
basis=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
origin=np.array([[0, 0, 0]]),
)
translation = np.eye(4, dtype=np.float32)
translation[2, -1] = 2.7
object_origin = transform_points(
eg3d_camera_sys, eg3d_camera_sys.origin, translation @ extrinsics
)
object_basis = (
transform_points(
eg3d_camera_sys,
eg3d_camera_sys.basis + eg3d_camera_sys.origin,
translation @ extrinsics,
)
- object_origin
)
object_sys = CoordSystem(basis=object_basis, origin=object_origin)
# Object system to EG3D object system
rot_x = np.eye(4, dtype=np.float32)
rot_x[:3, :3] = Rotation.from_rotvec([90, 0, 0], degrees=True).as_matrix()
inv_z = np.eye(4, dtype=np.float32)
inv_z[2, 2] = -1
offset = np.eye(4, dtype=np.float32)
offset[1, -1] = -0.5 * height
M = get_transform_in_sys(object_sys, offset @ inv_z @ rot_x)
cam2world = M @ translation @ extrinsics
return cam2world
def transform_coord_system(coord_system: CoordSystem, transformation):
new_origin = transformation @ np.block([coord_system.origin, 1.0]).T
new_origin /= new_origin[-1]
......@@ -207,29 +174,22 @@ def transform_points(object_sys, points, transformation):
return new_points
def generate_cam2world(rotation):
rot_vec = cv.Rodrigues(rotation[:3, :3])[0]
rot_vec[1] += np.pi / 2.0 + 30 * np.pi / 180
rotation = np.eye(4)
rotation[:3, :3] = cv.Rodrigues(rot_vec)[0]
# convert eg3d object sys to egd camera sys
eg3d_camera = eg3d_object_sys
# apply rotation in eg3d camera sys
t = np.eye(4)
t[2, 3] = -2.7
rotation_eg3d = get_transform_in_sys(eg3d_camera, rotation)
eg3d_camera = transform_coord_system(eg3d_camera, rotation_eg3d)
def generate_eg3d_cam2world(points_2d, points_3d, intrinsics):
translation_eg3d = get_transform_in_sys(eg3d_camera, t)
eg3d_camera = transform_coord_system(eg3d_camera, translation_eg3d)
world2cam = np.eye(4)
_, rvec, tvec = cv.solvePnP(points_3d, points_2d, intrinsics, None)
rvec[1] *= -1
world2cam[:3, :3] = cv.Rodrigues(rvec)[0]
world2cam[:3, -1] = tvec[:, 0]
cam2world = np.linalg.inv(world2cam)
eg3d_camera = transform_coord_system(eg3d_object_sys, cam2world)
align_axes = np.eye(4)
align_axes[:3, :3] = cv.Rodrigues(np.array([0, 0, np.pi]))[0]
align_axes = np.eye(4, dtype=np.float32)
align_axes[2, 2] = -1
align_axes[0, 0] = -1
align_axes = get_transform_in_sys(eg3d_camera, align_axes)
cam2world = align_axes @ translation_eg3d @ rotation_eg3d
cam2world = align_axes @ cam2world
return cam2world
......@@ -240,7 +200,7 @@ eg3d_object_sys = CoordSystem(
if __name__ == "__main__":
nums = [1046, 1052, 1063, 1071, 1075, 1132, 1133, 1134]
cams = []
cams = {}
# Load cars annoations
for num in nums:
record = np.load(
......@@ -248,10 +208,10 @@ if __name__ == "__main__":
).item()
cam2world = np.array(record['cam2world']).reshape(4, 4)
cam = transform_coord_system(eg3d_object_sys, cam2world)
cams.append(cam)
cams[num] = cam
# Load reference cat annotations
canonical_box = ObjectBox(eg3d_object_sys, create_canonical_box(3, 1.5, 1.5), faces, "box")
canonical_box = ObjectBox(eg3d_object_sys, create_canonical_box(3/5, 1.5/5, 1.5/5), faces, "box")
cam2world = np.array(
[
0.9931507213317242,
......@@ -274,7 +234,7 @@ if __name__ == "__main__":
).reshape(4, 4)
cam = transform_coord_system(eg3d_object_sys, cam2world)
cams.append(cam)
cams['right-cat'] = cam
cam2world = np.array(
[
......@@ -297,13 +257,13 @@ if __name__ == "__main__":
]
).reshape(4, 4)
cam = transform_coord_system(eg3d_object_sys, cam2world)
cams.append(cam)
cams['left-cat']= cam
ps.init()
ps.set_ground_plane_mode("none")
ps.set_up_dir("y_up")
add_coords("EG3D", eg3d_object_sys)
for i, cam in enumerate(cams):
add_coords(f"cam {i}", cam)
for name, cam in cams.items():
add_coords(f"cam {name}", cam)
add_box(canonical_box, (0, 1, 0))
ps.show()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment