<jupyter_start><jupyter_text>Check the generated image:<jupyter_code>image_paths = list((rgb_dir / '1x').iterdir())
Image.open(image_paths[0])<jupyter_output><empty_output><jupyter_text>Write ground truth camera<jupyter_code>from scipy.spatial.transform import Rotation as R

for cam_path in cam_dirs:
    with open(cam_path / 'metadata.json','r') as f:
        k_meta = json.load(f)

    rgb_list = list(sorted(cam_path.glob('rgba*.png')))
    image_size = imageio.imread(rgb_list[0]).shape[:2]

    for i, rgb in enumerate(rgb_list):
        meta = {}
        quat = k_meta['camera']['quaternions'][i]
        # change quaternion format from [w,x,y,z] to [x,y,z,w]
        quat = [quat[1],quat[2],quat[3],quat[0]]
        rot = R.from_quat(quat)

        meta['orientation'] = rot.as_matrix().T.tolist()
        meta['position'] = k_meta['camera']['positions'][i]
        meta['focal_length'] = k_meta['camera']['focal_length'] 
        meta['principal_point'] = [image_size[0]/2, image_size[1]/2] # check
        meta['skew'] = 0.
        meta['pixel_aspect_ratio'] = 1. # check
        meta['radial_distortion'] = [0., 0., 0.] # check
        meta['tangential_distortion'] = [0., 0.] # check
        meta['image_size'] = image_size

        with open(str(camera_gt_dir / f'{cam_path.name}_{rgb.stem}.json'), 'w') as f:
            json.dump(meta, f, indent=2)
list(sorted((rgb_dir / '1x').glob('*')))<jupyter_output><empty_output><jupyter_text>Write dataset split<jupyter_code># @title Save dataset split to `dataset.json`.

VAL_RATIO = 0.1

rgb_list = sorted((rgb_dir / '1x').glob('*'))
all_ids = list(map(lambda x: x.stem, rgb_list))
val_ids = all_ids[::int(1 / VAL_RATIO)]
train_ids = sorted(set(all_ids) - set(val_ids))

print(f'Num of train: {len(train_ids)}')
print(f'Num of val: {len(val_ids)}')

dataset_json = {
    'count': len(all_ids),
    'num_exemplars': len(train_ids),
    'ids': all_ids,
    'train_ids': train_ids,
    'val_ids': val_ids,
}

dataset_json_path = root_dir / 'dataset.json'
with dataset_json_path.open('w') as f:
    json.dump(dataset_json, f, indent=2)

print(f'Saved dataset information to {dataset_json_path}')<jupyter_output>Num of train: 180
Num of val: 20
Saved dataset information to data/kubric_car_multiview/dataset.json<jupyter_text>Save metadata info<jupyter_code>cam_rgb_num = len(train_ids) // len(cam_dirs)
id_list = list(range(cam_rgb_num)) * len(cam_dirs)
list(zip(id_list, train_ids))
# @title Save metadata information to `metadata.json`.
import bisect

metadata_json = {}

# number of renderings for each camera
cam_rgb_num = len(train_ids) // len(cam_dirs)
id_list = list(range(cam_rgb_num)) * len(cam_dirs)
for i, image_id in zip(id_list, train_ids):
  metadata_json[image_id] = {
      'warp_id': i,
      'appearance_id': i,
      'camera_id': 0, # camera id not used
  }
for i, image_id in enumerate(val_ids):
  i = bisect.bisect_left(train_ids, image_id)
  metadata_json[image_id] = {
      'warp_id': i,
      'appearance_id': i,
      'camera_id': 0, # camera id not used
  }

metadata_json_path = root_dir / 'metadata.json'
with metadata_json_path.open('w') as f:
    json.dump(metadata_json, f, indent=2)

print(f'Saved metadata information to {metadata_json_path}')<jupyter_output>Saved metadata information to data/kubric_car_multiview/metadata.json<jupyter_text>Write scene info<jupyter_code>def get_bbox_corners(points):
  lower = points.min(axis=0)
  upper = points.max(axis=0)
  return np.stack([lower, upper])

bbox_corners = get_bbox_corners(np.array(k_meta['instances'][0]['bboxes_3d'])[0])

# scene_center = np.mean(bbox_corners, axis=0)
# scene_scale = 1.0 / np.sqrt(np.sum((bbox_corners[1] - bbox_corners[0]) ** 2))
scene_center = np.array([0., 0., 0.])
scene_scale = 1.0

print(f'Scene Center: {scene_center}')
print(f'Scene Scale: {scene_scale}')

# @title Save scene information to `scene.json`.

scene_json_path = root_dir /  'scene.json'
with scene_json_path.open('w') as f:
  json.dump({
      'scale': scene_scale,
      'center': scene_center.tolist(),
      'bbox': bbox_corners.tolist(),
      'near': 0.,
      'far': bbox_corners[1].max() * scene_scale,
  }, f, indent=2)

print(f'Saved scene information to {scene_json_path}')<jupyter_output>Scene Center: [0. 0. 0.]
Scene Scale: 1.0
Saved scene information to data/kubric_car_multiview/scene.json
{'fraction_non_alphanumeric': 0.15679040811620937, 'fraction_numerical': 0.013373299515794327, 'mean_word_length': 4.540229885057471, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '390766', 'license': 'Apache-2.0', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1668, 'n_words': 317, 'path': 'kubric_meta_parser_multiview.ipynb', 'repo_name': 'ChikaYan/hypernerf', 'size': '4337'}