Skip to content

Commit

Permalink
easyvolcap: switching back to ujson due to api mismatch & adding easy…
Browse files Browse the repository at this point in the history
…volcap_to_nerf camera parameter conversion
  • Loading branch information
dendenxu committed Jan 23, 2024
1 parent 4a66c7e commit 0716af0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 7 deletions.
4 changes: 2 additions & 2 deletions easyvolcap/utils/console_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Colors:
essential_packages = [
'pdbr', # will also install rich
'tqdm',
'orjson',
'ujson',
'ruamel.yaml',
]

Expand Down Expand Up @@ -57,7 +57,7 @@ class Colors:
import warnings
import readline # if you need to update stuff from input
import numpy as np
import orjson as json
import ujson as json
from bdb import BdbQuit
from pdbr import RichPdb
from ruamel.yaml import YAML
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ipdb
pdbr
ninja
lpips
orjson
ujson
pandas

# for unwrapping to get StVK properly
Expand Down
2 changes: 1 addition & 1 deletion scripts/instant-ngp/easymocap_to_colmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
import collections
import numpy as np
import orjson as json
import ujson as json

from tqdm import tqdm
from os.path import join
Expand Down
2 changes: 1 addition & 1 deletion scripts/instant-ngp/merge_optimized_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import msgpack
import argparse
import numpy as np
import orjson as json
import ujson as json
import torch.nn.functional as F

from tqdm import tqdm
Expand Down
102 changes: 102 additions & 0 deletions scripts/nerf/easyvolcap_to_nerf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 将单帧数据转换为Nerf格式
import os
import cv2
import math
import argparse
import numpy as np
from glob import glob

from easyvolcap.utils.console_utils import *
from easyvolcap.utils.easy_utils import read_camera, write_camera


def convert_K(K, D, H, W):
fl_x = K[0, 0]
fl_y = K[1, 1]
cx = K[0, 2]
cy = K[1, 2]

k1, k2, p1, p2, p3 = D[0]

angle_x = math.atan(W / (fl_x * 2)) * 2
angle_y = math.atan(H / (fl_y * 2)) * 2
# fovx = angle_x * 180 / math.pi
# fovy = angle_y * 180 / math.pi

out = {
"camera_angle_x": angle_x,
"camera_angle_y": angle_y,
"fl_x": fl_x,
"fl_y": fl_y,
"k1": k1,
"k2": k2,
"p1": p1,
"p2": p2,
"p3": p3,
"cx": cx,
"cy": cy,
"h": H,
'w': W,
}
return out


def convertRT(RT0):
RT = np.eye(4)
RT[:3] = RT0
c2w = np.linalg.inv(RT)
c2w[0:3, 1] *= -1
c2w[0:3, 2] *= -1 # flip the y and z axis
# c2w = c2w[[1, 0, 2, 3], :] # swap y and z
# c2w[2, :] *= -1 # flip whole world upside down
return c2w


@catch_throw
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--data_root', type=str, default='/mnt/data/home/xuzhen/projects/large_gaussian/demo/cameras/00')
parser.add_argument('--out_file', type=str, default='/mnt/data/home/xuzhen/projects/large_gaussian/demo/trajectories/path0.json')
parser.add_argument('--images_dir', type=str, default='images')
parser.add_argument('--img_ext', type=str, default='.png', help='Only used when not using a multi_frame_dataset for contructing file_path')
parser.add_argument('--image_file', type=str, default='000000.jpg', help='Only used when --multi_frame_dataset for contructing file_path')
parser.add_argument('--multi_frame_dataset', action='store_true')
args = parser.parse_args()

os.makedirs(dirname(args.out_file), exist_ok=True)
cameras = read_camera(args.data_root)

camera_names = list(cameras.keys())
annots = {'frames': []}

K = cameras[camera_names[0]]['K']
D = cameras[camera_names[0]]['D']
H = cameras[camera_names[0]]['H']
W = cameras[camera_names[0]]['W']
annots.update(convert_K(K, D, H, W))

for camera_name in tqdm(camera_names):
camera = cameras[camera_name]

if args.multi_frame_dataset:
filename = f'{args.images_dir}/{camera_name}{args.img_ext}'
else:
filename = f'{args.images_dir}/{camera_name}/{args.image_file}'

# convert RT
c2w = convertRT(camera['RT'])
info = {
'file_path': filename,
'transform_matrix': c2w.tolist(),
}
info.update(convert_K(camera['K'], camera['D'], camera['H'], camera['W']))
annots['frames'].append(info)

with open(args.out_file, 'w') as f:
json.dump(annots, f, indent=4)

log(yellow(f'Saved converted camera path to: {blue(args.out_file)}'))


if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion scripts/nerf/nerf_to_easyvolcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def main():
parser.add_argument('--transforms_file', type=str, default='transforms_train.json')
parser.add_argument('--intri_file', type=str, default='cameras_train/00/intri.yml')
parser.add_argument('--extri_file', type=str, default='cameras_train/00/extri.yml')

parser.add_argument('--images_dir', type=str, default='images_train/00')
parser.add_argument('--no_organize_images', action='store_false', dest='organize_images')
parser.add_argument('--no_convert_test', action='store_false', dest='convert_test')
Expand Down Expand Up @@ -65,7 +66,7 @@ def main():
)
log(yellow(f'Converted cameras saved to {blue(join(args.volcap_root, f"{{{args.intri_file},{args.extri_file}}}"))}'))

if args.organize_images:
if args.organize_images and len(transforms.frames) and exists(transforms.frames[0].file_path):
run(f'python scripts/nerf/organize_images.py --nerf_root {args.nerf_root} --volcap_root {args.volcap_root} --transforms_file {args.transforms_file} --images_dir {args.images_dir}')

if args.transforms_file.endswith('_train.json') and \
Expand Down
2 changes: 1 addition & 1 deletion scripts/torchxpbd/register_smplh_semantically.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import torch
import argparse
import numpy as np
import orjson as json
import ujson as json

from os.path import join
from torch.optim import Adam
Expand Down

0 comments on commit 0716af0

Please sign in to comment.