2021-11-01 07:12:02 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
2023-04-11 09:12:58 +00:00
|
|
|
import re
|
|
|
|
from glob import glob
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
def raw_camparam_from_xml(path, pose="lookAt"):
|
|
|
|
import xml.etree.ElementTree as ET
|
2023-04-11 09:12:58 +00:00
|
|
|
|
2021-11-01 07:12:02 +00:00
|
|
|
tree = ET.parse(path)
|
|
|
|
elm = tree.find("./sensor/transform/" + pose)
|
|
|
|
camparam = elm.attrib
|
2023-04-11 09:12:58 +00:00
|
|
|
origin = np.fromstring(camparam["origin"], dtype=np.float32, sep=",")
|
|
|
|
target = np.fromstring(camparam["target"], dtype=np.float32, sep=",")
|
|
|
|
up = np.fromstring(camparam["up"], dtype=np.float32, sep=",")
|
|
|
|
height = int(tree.find("./sensor/film/integer[@name='height']").attrib["value"])
|
|
|
|
width = int(tree.find("./sensor/film/integer[@name='width']").attrib["value"])
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
camparam = dict()
|
2023-04-11 09:12:58 +00:00
|
|
|
camparam["origin"] = origin
|
|
|
|
camparam["up"] = up
|
|
|
|
camparam["target"] = target
|
|
|
|
camparam["height"] = height
|
|
|
|
camparam["width"] = width
|
2021-11-01 07:12:02 +00:00
|
|
|
return camparam
|
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
|
2021-11-01 07:12:02 +00:00
|
|
|
def get_cam_pos(origin, target, up):
|
|
|
|
inward = origin - target
|
|
|
|
right = np.cross(up, inward)
|
|
|
|
up = np.cross(inward, right)
|
|
|
|
rx = np.cross(up, inward)
|
|
|
|
ry = np.array(up)
|
|
|
|
rz = np.array(inward)
|
|
|
|
rx /= np.linalg.norm(rx)
|
|
|
|
ry /= np.linalg.norm(ry)
|
|
|
|
rz /= np.linalg.norm(rz)
|
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
rot = np.stack([rx, ry, -rz], axis=0)
|
2021-11-01 07:12:02 +00:00
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
aff = np.concatenate([np.eye(3), -origin[:, None]], axis=1)
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
ext = np.matmul(rot, aff)
|
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
result = np.concatenate([ext, np.array([[0, 0, 0, 1]])], axis=0)
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def convert_cam_params_all_views(datapoint_dir, dataroot, camera_param_dir):
|
2023-04-11 09:12:58 +00:00
|
|
|
depths = sorted(glob(os.path.join(datapoint_dir, "*depth.png")))
|
|
|
|
cam_ext = [
|
|
|
|
"_".join(re.sub(dataroot.strip("/"), camera_param_dir.strip("/"), f).split("_")[:-1]) + ".xml" for f in depths
|
|
|
|
]
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
for i, (f, pth) in enumerate(zip(cam_ext, depths)):
|
|
|
|
if not os.path.exists(f):
|
|
|
|
continue
|
2023-04-11 09:12:58 +00:00
|
|
|
params = raw_camparam_from_xml(f)
|
|
|
|
origin, target, up, width, height = (
|
|
|
|
params["origin"],
|
|
|
|
params["target"],
|
|
|
|
params["up"],
|
|
|
|
params["width"],
|
|
|
|
params["height"],
|
|
|
|
)
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
ext_matrix = get_cam_pos(origin, target, up)
|
|
|
|
|
|
|
|
#####
|
2023-04-11 09:12:58 +00:00
|
|
|
diag = (0.036**2 + 0.024**2) ** 0.5
|
2021-11-01 07:12:02 +00:00
|
|
|
focal_length = 0.05
|
|
|
|
res = [480, 480]
|
2023-04-11 09:12:58 +00:00
|
|
|
h_relative = res[1] / res[0]
|
|
|
|
sensor_width = np.sqrt(diag**2 / (1 + h_relative**2))
|
2021-11-01 07:12:02 +00:00
|
|
|
pix_size = sensor_width / res[0]
|
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
K = np.array(
|
|
|
|
[
|
|
|
|
[focal_length / pix_size, 0, (sensor_width / pix_size - 1) / 2],
|
|
|
|
[0, -focal_length / pix_size, (sensor_width * (res[1] / res[0]) / pix_size - 1) / 2],
|
|
|
|
[0, 0, 1],
|
|
|
|
]
|
|
|
|
)
|
2021-11-01 07:12:02 +00:00
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
np.savez(pth.split("depth.png")[0] + "cam_params.npz", extr=ext_matrix, intr=K)
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(opt):
|
|
|
|
dataroot_dir = Path(opt.dataroot)
|
|
|
|
|
|
|
|
leaf_subdirs = []
|
|
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(dataroot_dir):
|
|
|
|
if (not dirnames) and opt.mitsuba_xml_root not in dirpath:
|
|
|
|
leaf_subdirs.append(dirpath)
|
|
|
|
|
|
|
|
for k, dir_ in enumerate(leaf_subdirs):
|
2023-04-11 09:12:58 +00:00
|
|
|
print("Processing dir {}/{}: {}".format(k, len(leaf_subdirs), dir_))
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
convert_cam_params_all_views(dir_, opt.dataroot, opt.mitsuba_xml_root)
|
|
|
|
|
|
|
|
|
2023-04-11 09:12:58 +00:00
|
|
|
if __name__ == "__main__":
|
2021-11-01 07:12:02 +00:00
|
|
|
args = argparse.ArgumentParser()
|
2023-04-11 09:12:58 +00:00
|
|
|
args.add_argument("--dataroot", type=str, default="GenReData/")
|
|
|
|
args.add_argument("--mitsuba_xml_root", type=str, default="GenReData/genre-xml_v2")
|
2021-11-01 07:12:02 +00:00
|
|
|
|
|
|
|
opt = args.parse_args()
|
|
|
|
|
|
|
|
main(opt)
|