def _linemod_to_coco(cls, split):
    data_root = 'data/linemod'
    model_path = os.path.join(data_root, cls,
                              cls + '.ply')  # 'data/linemod/cat/cat.ply'

    renderer = OpenGLRenderer(model_path)
    K = linemod_K  #

    model = renderer.model['pts'] / 1000  # (94404, 3)
    corner_3d = get_model_corners(model)  # (8, 3)
    center_3d = (np.max(corner_3d, 0) + np.min(corner_3d, 0)) / 2  # (3,)
    fps_3d = np.loadtxt(os.path.join(data_root, cls, 'farthest.txt'))

    model_meta = {
        'K': K,
        'corner_3d': corner_3d,
        'center_3d': center_3d,
        'fps_3d': fps_3d,
        'data_root': data_root,
        'cls': cls,
        'split': split
    }

    img_id = 0
    ann_id = 0
    images = []
    annotations = []

    if split == 'occ':
        img_id, ann_id = record_occ_ann(model_meta, img_id, ann_id, images,
                                        annotations)
    elif split == 'train' or split == 'test':
        img_id, ann_id = record_real_ann(model_meta, img_id, ann_id, images,
                                         annotations)

    if split == 'train':
        img_id, ann_id = record_fuse_ann(model_meta, img_id, ann_id, images,
                                         annotations)
        img_id, ann_id = record_render_ann(model_meta, img_id, ann_id, images,
                                           annotations)

    categories = [{'supercategory': 'none', 'id': 1, 'name': cls}]
    instance = {
        'images': images,
        'annotations': annotations,
        'categories': categories
    }

    anno_path = os.path.join(data_root, cls, split + '.json')
    with open(anno_path, 'w') as f:
        json.dump(instance, f)  # 'data/linemod/cat/train.json'
def _tless_train_to_coco(obj_id):
    data_root = 'data/tless'

    model_path = os.path.join(data_root, 'models_cad',
                              'obj_{:03}.ply'.format(obj_id))
    renderer = OpenGLRenderer(model_path)

    model = renderer.model['pts'] / 1000
    corner_3d = get_model_corners(model)
    center_3d = (np.max(corner_3d, 0) + np.min(corner_3d, 0)) / 2
    fps_3d = np.loadtxt(
        os.path.join(data_root, 'farthest',
                     'farthest_{:02}.txt'.format(obj_id)))

    model_meta = {
        'corner_3d': corner_3d,
        'center_3d': center_3d,
        'fps_3d': fps_3d,
        'data_root': data_root,
        'obj_id': obj_id,
    }

    img_id = 0
    ann_id = 0
    images = []
    annotations = []

    img_id, ann_id = record_real_ann(model_meta, img_id, ann_id, images,
                                     annotations)
    img_id, ann_id = record_render_ann(model_meta, img_id, ann_id, images,
                                       annotations)

    categories = [{'supercategory': 'none', 'id': 1, 'name': obj_id}]
    instance = {
        'images': images,
        'annotations': annotations,
        'categories': categories
    }

    data_cache_dir = 'data/cache/tless_pose/'
    obj_cache_dir = os.path.join(data_cache_dir, '{:02}'.format(obj_id))
    os.system('mkdir -p {}'.format(obj_cache_dir))
    anno_path = os.path.join(obj_cache_dir, 'train.json')
    with open(anno_path, 'w') as f:
        json.dump(instance, f)
Beispiel #3
0
def custom_to_coco(data_root):
    model_path = os.path.join(data_root, 'model.ply')

    cls_type = input("On which class do you want to train? ")

    renderer = OpenGLRenderer(model_path)
    K = np.loadtxt(os.path.join(data_root, 'camera.txt'))

    model = renderer.model['pts'] / 1000
    corner_3d = get_model_corners(model)
    print("corner_3d:")
    print(corner_3d)
    center_3d = (np.max(corner_3d, 0) + np.min(corner_3d, 0)) / 2
    print("center_3d:")
    print(center_3d)
    fps_3d = np.loadtxt(os.path.join(data_root, 'fps.txt'))

    model_meta = {
        'K': K,
        'corner_3d': corner_3d,
        'center_3d': center_3d,
        'fps_3d': fps_3d,
        'data_root': data_root,
    }

    img_id = 0
    ann_id = 0
    images = []
    annotations = []

    img_id, ann_id = record_ann(model_meta, img_id, ann_id, images,
                                annotations, cls_type)

    categories = [{'supercategory': 'none', 'id': 1, 'name': cls_type}]
    instance = {
        'images': images,
        'annotations': annotations,
        'categories': categories
    }

    anno_path = os.path.join(data_root, 'train.json')
    with open(anno_path, 'w') as f:
        json.dump(instance, f)