def generate_db(dataPath='./thor_DB.json', categoryPath='./categories.json', newDataPath='./thor_DB_msdn.json'):
    print('-- generate thor_DB file --')
    with open(dataPath, 'r') as f:
        DB = json.load(f)
    with open(categoryPath, 'r') as f:
        categories = json.load(f)
    print('data read complete')
    print('DB len:', len(DB))
    new_DB = []
    for (idx, data) in enumerate(DB):
        if (len(data['visible_object']) == 0):
            continue
        if ((idx % 10000) == 0):
            print('data preprocessing... ({}/{})'.format(idx, len(DB)))
        new_data = {}
        new_data['height'] = 600
        new_data['width'] = 600
        new_data['scene_name'] = data['scene']
        new_data['id'] = data['data_id']
        new_data['path'] = data['image_file']
        new_data['depth_path'] = data['depth_file']
        agent = {}
        global_rotation = data['agent']['global_rotation']['y']
        if (global_rotation == 0):
            global_rotation = 0
        elif (global_rotation == 90):
            global_rotation = 1
        elif (global_rotation == 180):
            global_rotation = 2
        elif (global_rotation == 270):
            global_rotation = 3
        agent['global_rotation'] = global_rotation
        agent['global_position'] = [data['agent']['global_position']['x'], data['agent']['global_position']['y'], data['agent']['global_position']['z']]
        new_data['agent'] = agent
        objects = []
        obj_g2l = {}
        for (idx, obj) in enumerate(data['visible_object']):
            b_3d = obj['global_bounds3D']
            size_3d = [(b_3d[3] - b_3d[0]), (b_3d[4] - b_3d[1]), (b_3d[5] - b_3d[2])]
            objects.append({'class': categories['object'][(obj['obj_class'] - 1)], 'box': obj['bounding_box'], 'global_position': [obj['global_position']['x'], obj['global_position']['y'], obj['global_position']['z']], 'size_3d': size_3d, 'distance': obj['distance'], 'color': categories['color'][obj['color']], 'open_state': categories['open_state'][obj['open_state']]})
            obj_g2l[obj['id']] = idx
        new_data['objects'] = objects
        relationships = []
        for rel in data['relation']:
            relationships.append({'sub_id': obj_g2l[rel['subject_id']], 'predicate': categories['predicate'][(rel['rel_class'] - 1)], 'obj_id': obj_g2l[rel['object_id']]})
        new_data['relationships'] = relationships
        global_relationships = []
        for rel in data['global_relation']:
            global_relationships.append({'sub_id': obj_g2l[rel['subject_id']], 'predicate': categories['predicate'][(rel['rel_class'] - 1)], 'obj_id': obj_g2l[rel['object_id']]})
        new_data['global_relationships'] = global_relationships
        new_DB.append(new_data)
    print('data preprocessing complete')
    print('new_DB len:', len(new_DB))
    print('data dumpping...')
    with open(newDataPath, 'w') as f:
        json.dump(new_DB, f, indent='\t')
    print('data dumpping complete')
    print('file path:', newDataPath)
def generate_db(dataPath='./thor_DB.json', categoryPath='./categories.json', newDataPath='./thor_DB_msdn.json'):
    print('-- generate thor_DB file --')
    with open(dataPath, 'r') as f:
        DB = json.load(f)
    with open(categoryPath, 'r') as f:
        categories = json.load(f)
    print('data read complete')
    print('DB len:', len(DB))
    new_DB = []
    for (idx, data) in enumerate(DB):
        if (len(data['visible_object']) == 0):
            continue
        if ((idx % 10000) == 0):
            print('data preprocessing... ({}/{})'.format(idx, len(DB)))
        new_data = {}
        new_data['height'] = 600
        new_data['width'] = 600
        new_data['scene_name'] = data['scene']
        new_data['id'] = data['data_id']
        new_data['path'] = data['image_file']
        new_data['depth_path'] = data['depth_file']
        agent = {}
        global_rotation = data['agent']['global_rotation']['y']
        if (global_rotation == 0):
            global_rotation = 0
        elif (global_rotation == 90):
            global_rotation = 1
        elif (global_rotation == 180):
            global_rotation = 2
        elif (global_rotation == 270):
            global_rotation = 3
        agent['global_rotation'] = global_rotation
        agent['global_position'] = [data['agent']['global_position']['x'], data['agent']['global_position']['y'], data['agent']['global_position']['z']]
        new_data['agent'] = agent
        objects = []
        obj_g2l = {}
        for (idx, obj) in enumerate(data['visible_object']):
            b_3d = obj['global_bounds3D']
            size_3d = [(b_3d[3] - b_3d[0]), (b_3d[4] - b_3d[1]), (b_3d[5] - b_3d[2])]
            objects.append({'class': categories['object'][(obj['obj_class'] - 1)], 'box': obj['bounding_box'], 'global_position': [obj['global_position']['x'], obj['global_position']['y'], obj['global_position']['z']], 'size_3d': size_3d, 'distance': obj['distance'], 'color': categories['color'][obj['color']], 'open_state': categories['open_state'][obj['open_state']]})
            obj_g2l[obj['id']] = idx
        new_data['objects'] = objects
        relationships = []
        for rel in data['relation']:
            relationships.append({'sub_id': obj_g2l[rel['subject_id']], 'predicate': categories['predicate'][(rel['rel_class'] - 1)], 'obj_id': obj_g2l[rel['object_id']]})
        new_data['relationships'] = relationships
        global_relationships = []
        for rel in data['global_relation']:
            global_relationships.append({'sub_id': obj_g2l[rel['subject_id']], 'predicate': categories['predicate'][(rel['rel_class'] - 1)], 'obj_id': obj_g2l[rel['object_id']]})
        new_data['global_relationships'] = global_relationships
        new_DB.append(new_data)
    print('data preprocessing complete')
    print('new_DB len:', len(new_DB))
    print('data dumpping...')
    with open(newDataPath, 'w') as f:
        json.dump(new_DB, f, indent='\t')
    print('data dumpping complete')
    print('file path:', newDataPath)