Commit 567f33fd authored by nviolante's avatar nviolante
Browse files

remove unused files

parent 929c8b62
import polyscope as ps
import numpy as np
from PIL import Image
def add_coords(name, basis, origin):
coord_sys = ps.register_point_cloud(f"{name} sys", origin, radius=0.02, color=(1, 1, 1))
coord_sys.add_vector_quantity("X", basis[0][None, :] - origin, length=0.1, enabled=True, radius=0.01, color=(1, 0, 0))
coord_sys.add_vector_quantity("Y", basis[1][None, :] - origin, length=0.1, enabled=True, radius=0.01, color=(0, 1, 0))
coord_sys.add_vector_quantity("Z", basis[2][None, :] - origin, length=0.1, enabled=True, radius=0.01, color=(0, 0, 1))
if __name__ == "__main__":
#------------------------------------------------------------------------------------------------
# EG3D coordinate system
#------------------------------------------------------------------------------------------------
origin = np.array([[0, 0, 0]])
object_basis = np.array([[-1, 0, 0],
[0, 1, 0],
[0, 0, 1]
])
#------------------------------------------------------------------------------------------------
# Cats
#------------------------------------------------------------------------------------------------
transform_cat2 = np.array([0.9817331031140984, 0.03807360008272328, -0.18641490076360254, 0.5033202320617269,
0.040125203602249686, -0.9991684038591816, 0.007243532658403638, -0.019557538177689824,
-0.18598409148591583, -0.014591151641875204, -0.9824444086094246, 2.652599903245447,
0.0, 0.0, 0.0, 1.0]).reshape(4, 4)
transform_cat3 = np.array([0.9817331031140984, -0.03807360008272328, 0.18641490076360254, -0.5033202320617269,
-0.040125203602249686, -0.9991684038591816, 0.007243532658403638, -0.019557538177689824,
0.18598409148591583, -0.014591151641875204, -0.9824444086094246, 2.652599903245447,
0.0, 0.0, 0.0, 1.0]).reshape(4, 4)
x_range = np.linspace(1, -1, 512)
z_range = np.linspace(1, -1, 512)
xx, zz = np.meshgrid(x_range, z_range)
yy = np.zeros_like(xx)
points_image = np.block([[xx.flatten()], [yy.flatten()], [zz.flatten()]]).T
cat2color = np.array(Image.open('./camera_calib/cats/img00000002.png')).reshape(-1, 3, order='C') / 255.0
cat3color = np.array(Image.open('./camera_calib/cats/img00000003.png')).reshape(-1, 3, order='C') / 255.0
#------------------------------------------------------------------------------------------------
# Polyscope
#------------------------------------------------------------------------------------------------
ps.init()
ps.set_up_dir('z_up')
# ps.register_surface_mesh("canonical", create_canonical_box(prediction=record1134['kpts_3d_pred'][0]), faces, color=(1,0,0))
add_coords("Object sys", object_basis, origin)
# add_coords('EG3D sys', eg3d_basis, origin)
# Cats ------------------
cat2_cloud = ps.register_point_cloud("cat 2 image", points_image, enabled=True)
cat2_cloud.add_color_quantity("color", cat2color, enabled=True)
cat2_points = (transform_cat2 @ np.block([points_image, np.ones((points_image.shape[0], 1))]).T).T
cat2_points = cat2_points[:, :3]
cat2_cloud = ps.register_point_cloud("cat 2 image asd", cat2_points, enabled=True)
cat2_cloud.add_color_quantity("color", cat2color, enabled=True)
# cat3_points = ( egonet2eg3d @ transform_cat3 @ np.block([points_image, np.ones((points_image.shape[0], 1))]).T).T
# cat3_points = cat3_points[:, :3]
# cat3_cloud = ps.register_point_cloud("cat 3 image", cat3_points, enabled=True)
# cat3_cloud.add_color_quantity("color image", cat3color, enabled=True)
# cat3_cloud = ps.register_point_cloud("cat 3 image", points_image, enabled=True)
# cat3_cloud.add_color_quantity("color", cat3color, enabled=True)
# cat3_points = (transform_cat3 @ np.block([points_image, np.ones((points_image.shape[0], 1))]).T).T
# cat3_points = cat3_points[:, :3]
# cat3_cloud = ps.register_point_cloud("cat 3 image asd", cat3_points, enabled=True)
# cat3_cloud.add_color_quantity("color", cat3color, enabled=True)
ps.show()
\ No newline at end of file
This diff is collapsed.
import polyscope as ps
import numpy as np
from collections import namedtuple
from scipy.spatial.transform import Rotation
interp_dict = {
'bbox12':(np.array([1,3,5,7,# h direction
1,2,3,4,# l direction
1,2,5,6]), # w direction
np.array([2,4,6,8,
5,6,7,8,
3,4,7,8])
),
'bbox12l':(np.array([1,2,3,4,]), # w direction
np.array([5,6,7,8])
),
'bbox12h':(np.array([1,3,5,7]), # w direction
np.array([2,4,6,8])
),
'bbox12w':(np.array([1,2,5,6]), # w direction
np.array([3,4,7,8])
),
}
ObjectBox = namedtuple('ObjectBox', ['coord_system', 'vertices', 'faces', 'name'])
CoordSystem = namedtuple('CoordSystem', ['basis', 'origin'])
faces = np.array([[3, 2, 0],
[3, 1, 0],
[3, 2, 6],
[3, 7, 6],
[1, 0, 4],
[1, 5, 4],
[7, 5, 4],
[7, 6, 4],
[7, 5, 1],
[7, 3, 1],
[6, 2, 0],
[6, 4, 0]
])
def transform_coord_system(coord_system: CoordSystem, transformation):
new_origin = (transformation @ np.block([coord_system.origin, 1.0]).T)
new_origin /= new_origin[-1]
new_origin = new_origin[:3].T
new_basis = (transformation @ np.block([coord_system.basis, np.ones((coord_system.basis.shape[0], 1))]).T).T
new_basis = new_basis[:, :3] - new_origin
return CoordSystem(new_basis, new_origin)
def create_canonical_box(l=2, h=1, w=1, prediction=None):
if prediction is not None:
parents = prediction[interp_dict['bbox12'][0] - 1]
children = prediction[interp_dict['bbox12'][1] - 1]
lines = parents - children
lines = np.sqrt(np.sum(lines**2, axis=1))
# averaged over the four parallel line segments
h, l, w = np.sum(lines[:4])/4, np.sum(lines[4:8])/4, np.sum(lines[8:])/4
x_corners = [l, l, l, l, 0, 0, 0, 0]
y_corners = [0, h, 0, h, 0, h, 0, h]
z_corners = [w, w, 0, 0, w, w, 0, 0]
x_corners -= np.float32(l) / 2.0
y_corners -= np.float32(h)
z_corners -= np.float32(w) / 2.0
corners_3d = np.array([x_corners, y_corners, z_corners])
return corners_3d.T
def create_box(transformation, coord_system, vertices, faces, name):
new_vertices = (transformation @ np.block([vertices, np.ones((vertices.shape[0], 1))]).T).T
new_vertices = new_vertices[:, :3]
new_coord_system = transform_coord_system(coord_system, transformation)
return ObjectBox(new_coord_system, new_vertices, faces, name)
def add_box(box: ObjectBox, color):
ps.register_surface_mesh(box.name, box.vertices, box.faces, color=color, transparency=0.6)
add_coords(box.name, box.coord_system)
def add_coords(name: str, coord_system: CoordSystem):
coord_sys = ps.register_point_cloud(f"{name} sys", coord_system.origin, radius=0.02, color=(1, 1, 1))
coord_sys.add_vector_quantity("X", coord_system.basis[0][None, :], length=0.1, enabled=True, radius=0.01, color=(1, 0, 0))
coord_sys.add_vector_quantity("Y", coord_system.basis[1][None, :], length=0.1, enabled=True, radius=0.01, color=(0, 1, 0))
coord_sys.add_vector_quantity("Z", coord_system.basis[2][None, :], length=0.1, enabled=True, radius=0.01, color=(0, 0, 1))
def create_egonet2eg3d():
egonet2eg3d = np.zeros((4, 4), dtype=np.float32)
roty = np.zeros((4, 4), dtype=np.float32)
rotx = np.zeros((4, 4), dtype=np.float32)
roty[:3, :3] = Rotation.from_rotvec([0, 90, 0], degrees=True).as_matrix()
roty[3, 3] = 1.0
rotx[:3, :3] = Rotation.from_rotvec([-90, 0, 0], degrees=True).as_matrix()
rotx[3, 3] = 1.0
egonet2eg3d = rotx @ roty
return egonet2eg3d
def transform_points(object_sys, points, transformation):
to_world = np.eye(4)
to_world[:3, -1] = - object_sys.origin
to_sys_center = np.eye(4)
to_sys_center[:3, -1] = object_sys.origin
algin_axes_to_world = np.eye(4)
algin_axes_to_world[:3, :3] = object_sys.basis
M = to_sys_center @ np.linalg.inv(algin_axes_to_world) @ transformation @ algin_axes_to_world @ to_world
new_points = (M @ np.block([points, np.ones((points.shape[0], 1))]).T).T
new_points = new_points[:, :3]
return new_points
if __name__ == "__main__":
#------------------------------------------------------------------------------------------------
# EgoNet and EG3D coordinate system
#------------------------------------------------------------------------------------------------
egonet_sys = CoordSystem(basis=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
origin=np.array([[0, 0, 0]])
)
egonet2eg3d = create_egonet2eg3d()
eg3d_sys = transform_coord_system(egonet_sys, egonet2eg3d)
#------------------------------------------------------------------------------------------------
# Cars
#------------------------------------------------------------------------------------------------
record1075 = np.load("./camera_calib/results/img00001075.npy", allow_pickle=True).item()
record1134= np.load("./camera_calib/results/img00001134.npy", allow_pickle=True).item()
E1075 = record1075["extrinsics"].reshape(4, 4)
E1134 = record1134["extrinsics"].reshape(4, 4)
translation = np.eye(4, dtype=np.float32)
translation[2, -1] = 2.7
box1075 = create_box(translation @ E1075,
egonet_sys,
create_canonical_box(2, 1, 1),
faces,
"car 1075")
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)
offset[1, -1] = -0.5
object_sys = transform_coord_system(
egonet_sys,
translation @ E1075
)
eg3d_object_origin = transform_points(object_sys, object_sys.origin, offset @inv_z @ rot_x)
eg3d_object_basis = transform_points(object_sys, object_sys.basis + object_sys.origin, offset @ inv_z @ rot_x) - eg3d_object_origin
eg3d_object_sys = CoordSystem(basis=eg3d_object_basis, origin=eg3d_object_origin)
#------------------------------------------------------------------------------------------------
# Polyscope
#------------------------------------------------------------------------------------------------
ps.init()
ps.set_ground_plane_mode("none")
ps.set_up_dir('neg_y_up')
add_coords("EgoNet", egonet_sys)
add_coords('EG3D', eg3d_sys)
add_coords("EG3D object system", eg3d_object_sys)
add_box(box1075, color=(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