Example #1
0
def detect_chessboard(path, out, pattern, gridSize):
    create_chessboard(path, pattern, gridSize)
    dataset = ImageFolder(path, annot='chessboard')
    dataset.isTmp = False
    for i in tqdm(range(len(dataset))):
        imgname, annotname = dataset[i]
        # detect the 2d chessboard
        img = cv2.imread(imgname)
        annots = read_json(annotname)
        show = findChessboardCorners(img, annots, pattern)
        save_json(annotname, annots)
        if show is None:
            continue
        outname = join(out, imgname.replace(path + '/images/', ''))
        os.makedirs(os.path.dirname(outname), exist_ok=True)
        cv2.imwrite(outname, show)
Example #2
0
def detect_chessboard_sequence(path, out, pattern, gridSize, args):
    create_chessboard(path, pattern, gridSize, ext=args.ext)
    subs = sorted(os.listdir(join(path, 'images')))
    for sub in subs:
        dataset = ImageFolder(path, sub=sub, annot='chessboard', ext=args.ext)
        dataset.isTmp = False
        nFrames = len(dataset)
        found = np.zeros(nFrames, dtype=np.bool)
        visited = np.zeros(nFrames, dtype=np.bool)
        proposals = []
        init_step = args.max_step
        min_step = args.min_step
        for nf in range(0, nFrames, init_step):
            if nf + init_step < len(dataset):
                proposals.append([nf, nf + init_step])
        while len(proposals) > 0:
            left, right = proposals.pop(0)
            print('Check [{}, {}]'.format(left, right))
            for nf in [left, right]:
                if not visited[nf]:
                    visited[nf] = True
                    imgname, annotname = dataset[nf]
                    # detect the 2d chessboard
                    img = cv2.imread(imgname)
                    annots = read_json(annotname)
                    show = findChessboardCorners(img, annots, pattern)
                    save_json(annotname, annots)
                    if show is None:
                        if args.debug:
                            print('Cannot find {}'.format(imgname))
                        found[nf] = False
                        continue
                    found[nf] = True
                    outname = join(out, imgname.replace(path + '/images/', ''))
                    os.makedirs(os.path.dirname(outname), exist_ok=True)
                    cv2.imwrite(outname, show)
            if not found[left] and not found[right]:
                continue
            mid = (left + right) // 2
            if mid == left or mid == right:
                continue
            if mid - left > min_step:
                proposals.append((left, mid))
            if right - mid > min_step:
                proposals.append((mid, right))
Example #3
0
def create_chessboard(path, pattern, gridSize, ext):
    print('Create chessboard {}'.format(pattern))
    keypoints3d = getChessboard3d(pattern, gridSize=gridSize)
    keypoints2d = np.zeros((keypoints3d.shape[0], 3))
    imgnames = getFileList(path, ext=ext)
    template = {
        'keypoints3d': keypoints3d.tolist(),
        'keypoints2d': keypoints2d.tolist(),
        'visited': False
    }
    for imgname in tqdm(imgnames, desc='create template chessboard'):
        annname = imgname.replace('images', 'chessboard').replace(ext, '.json')
        annname = join(path, annname)
        if os.path.exists(annname):
            # 覆盖keypoints3d
            data = read_json(annname)
            data['keypoints3d'] = template['keypoints3d']
            save_json(annname, data)
        else:
            save_json(annname, template)
Example #4
0
def detect_chessboard(path, out, pattern, gridSize, args):
    create_chessboard(path, pattern, gridSize, ext=args.ext)
    dataset = ImageFolder(path, annot='chessboard', ext=args.ext)
    dataset.isTmp = False
    if args.silent:
        trange = range(len(dataset))
    else:
        trange = tqdm(range(len(dataset)))
    for i in trange:
        imgname, annotname = dataset[i]
        # detect the 2d chessboard
        img = cv2.imread(imgname)
        annots = read_json(annotname)
        show = findChessboardCorners(img, annots, pattern)
        save_json(annotname, annots)
        if show is None:
            if args.debug:
                print('Cannot find {}'.format(imgname))
            continue
        outname = join(out, imgname.replace(path + '/images/', ''))
        os.makedirs(os.path.dirname(outname), exist_ok=True)
        cv2.imwrite(outname, show)
Example #5
0
            ' '.join(['{:7.2f}'.format(i) for i in row]) for row in annots['K']
        ]))
        return 0
    else:
        print('K is not caculated')
    while annotator.isOpen:
        annotator.run()
    for key in ['vanish_line', 'vanish_point']:
        edges_cache[key] = annotator.param['annots'][key]


if __name__ == "__main__":
    from easymocap.annotator import load_parser, parse_parser
    parser = load_parser()
    parser.add_argument('--skip', action='store_true')
    args = parse_parser(parser)
    for sub in args.sub:
        if False:
            from glob import glob
            from os.path import join
            annnames = sorted(glob(join(args.path, args.annot, sub, '*.json')))
            for annname in annnames:
                ann = read_json(annname)
                for key in ['vanish_line', 'vanish_points']:
                    if key in ann.keys():
                        val = ann[key]
                        val = [val[1], val[0], val[2]]
                        ann[key] = val
                save_annot(annname, ann)
        else:
            annot_example(args.path, annot=args.annot, sub=sub)