merge.py 1.37 KB
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from shutil import copy
import sys
import os
import json
import argparse
from typing import Mapping
from data import get_dataset_desc_path, get_data_path

sys.path.append(os.path.abspath(sys.path[0] + '/../../'))

parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, nargs='+')
parser.add_argument('output', type=str)
args = parser.parse_args()


input = [get_dataset_desc_path(path) for path in args.input]
output = get_dataset_desc_path(args.output)


def copy_images(src_path, dst_path, n, offset=0):
    os.makedirs(os.path.dirname(dst_path), exist_ok=True)
    for i in range(n):
        copy(src_path % i, dst_path % (i + offset))


with open(input[0], 'r') as fp:
    dataset_desc: Mapping = json.load(fp)

n_views = 0
for i in range(len(input)):
    if i == 0:
        input_desc = dataset_desc
    else:
        with open(input[i], 'r') as fp:
            input_desc: Mapping = json.load(fp)
        dataset_desc['view_centers'] += input_desc['view_centers']
        dataset_desc['view_rots'] += input_desc['view_rots']
Nianchen Deng's avatar
sync    
Nianchen Deng committed
39
40
    copy_images(get_data_path(input[i], input_desc['color_file']),
                get_data_path(output, dataset_desc['color_file']),
Nianchen Deng's avatar
sync    
Nianchen Deng committed
41
42
43
44
45
46
47
                len(input_desc['view_centers']), n_views)
    n_views += len(input_desc['view_centers'])

dataset_desc['samples'] = [n_views]

with open(output, 'w') as fp:
    json.dump(dataset_desc, fp, indent=4)