Example #1
0
def main():
    params = {
        'seq_paths': '',
        'root_dir': '',
        'class_names_path':
        '../labelling_tool/data//predefined_classes_orig.txt',
        'save_file_name': '',
        'csv_file_name': '',
        'map_folder': '',
        'load_path': '',
        'n_classes': 4,
        'sources_to_include': [],
        'img_ext': 'png',
        'batch_size': 1,
        'show_img': 0,
        'save_video': 1,
        'enable_mask': 0,
        'n_frames': 0,
        'codec': 'H264',
        'fps': 20,
    }
    processArguments(sys.argv[1:], params)
    seq_paths = params['seq_paths']
    root_dir = params['root_dir']
    sources_to_include = params['sources_to_include']
    enable_mask = params['enable_mask']

    if seq_paths:
        if os.path.isfile(seq_paths):
            seq_paths = [
                x.strip() for x in open(seq_paths).readlines() if x.strip()
            ]
        else:
            seq_paths = seq_paths.split(',')
        if root_dir:
            seq_paths = [os.path.join(root_dir, name) for name in seq_paths]

    elif root_dir:
        seq_paths = [
            os.path.join(root_dir, name) for name in os.listdir(root_dir)
            if os.path.isdir(os.path.join(root_dir, name))
        ]
        seq_paths.sort(key=sortKey)
    else:
        raise IOError('Either seq_paths or root_dir must be provided')

    for seq_path in seq_paths:
        voc_path = os.path.join(seq_path, 'annotations')
        saveBoxesCSV(seq_path, voc_path, sources_to_include, enable_mask)
Example #2
0
def main():
    params = {
        'img_paths': '',
        'img_root_dir': '',
        'mask_paths': '',
        'mask_root_dir': '',

        'save_file_name': '',
        'csv_file_name': '',
        'map_folder': '',
        'load_path': '',
        'n_classes': 4,
        'out_border': 0,
        'fixed_ar': 0.0,
        'img_ext': 'jpg',
        'mask_ext': 'png',
        'batch_size': 1,
        'show_img': 1,
        'out_dir': '',
        'out_ext': 'mkv',
        'out_size': '1280x720',
        'n_frames': 0,
        'codec': 'MPEG',
        'fps': 30,
        'save_video': 1,
        'map_to_bbox': 0,
        'border': 0,
        'include_orig': 1,
        'include_binary': 1,
        'show_bbox': 0,
        'img_subdir': '',
        'mask_subdir': '',
        'apply_contour': 0,
        'write_text': 1,
        'combine': 0,
        'raw_mask': 0,
    }
    processArguments(sys.argv[1:], params)

    img_root_dir = params['img_root_dir']
    img_paths = params['img_paths']
    img_subdir = params['img_subdir']

    mask_root_dir = params['mask_root_dir']
    mask_paths = params['mask_paths']
    mask_subdir = params['mask_subdir']

    combine = params['combine']

    if img_paths:
        if os.path.isfile(img_paths):
            img_paths = [x.strip() for x in open(img_paths).readlines() if x.strip()]
        else:
            img_paths = img_paths.split(',')
        if img_root_dir:
            img_paths = [os.path.join(img_root_dir, name) for name in img_paths]
    else:
        img_paths = [os.path.join(img_root_dir, name) for name in os.listdir(img_root_dir) if
                     os.path.isdir(os.path.join(img_root_dir, name))]
    img_paths.sort(key=sortKey)
    if img_subdir:
        img_paths = [os.path.join(name, img_subdir) for name in img_paths]

    if mask_paths:
        if os.path.isfile(mask_paths):
            mask_paths = [x.strip() for x in open(mask_paths).readlines() if x.strip()]
        else:
            mask_paths = mask_paths.split(',')
        if mask_root_dir:
            mask_paths = [os.path.join(mask_root_dir, name) for name in mask_paths]
    elif mask_root_dir:
        mask_paths = [os.path.join(mask_root_dir, name) for name in os.listdir(mask_root_dir) if
                      os.path.isdir(os.path.join(mask_root_dir, name))]
    else:
        mask_paths = [os.path.dirname(src_path) if img_dir else src_path for src_path in src_paths]

    if mask_subdir:
        mask_paths = [os.path.join(name, mask_subdir) for name in mask_paths]

    mask_paths.sort(key=sortKey)

    print('mask_paths:')
    pprint(mask_paths)

    csv_path = ''

    n_img_paths, n_mask_paths = len(img_paths), len(mask_paths)
    if n_img_paths != n_mask_paths:
        print('img_paths: ', img_paths)
        print('mask_paths: ', mask_paths)
        raise IOError('Mismatch between n_img_paths: {} and n_mask_paths: {}'.format(
            n_img_paths, n_mask_paths))

    args, varargs, varkw, defaults = inspect.getargspec(fixMasks)
    params = {k: params[k] for k in params if k in args}
    params['writer'] = None
    pprint(params)

    if combine:
        print('Combining all output sequences into one video')

    # print('args: ', args)
    # print('varargs: ', varargs)
    # print('varkw: ', varkw)

    n_seq = len(img_paths)

    for seq_id in range(n_img_paths):
        img_path, mask_path = img_paths[seq_id], mask_paths[seq_id]
        print('Processing sequence {}/{}'.format(seq_id + 1, n_img_paths))
        writer, out_size = fixMasks(img_path, mask_path, csv_path, **params, )
        if not combine:
            writer.release()
            params['writer'] = None
        else:
            params['writer'] = writer
            params['out_size'] = out_size

        if _exit:
            break

    if combine:
        writer.release()
def main():
    params = {
        'class_names_path':
        '../labelling_tool/data//predefined_classes_orig.txt',
        'seq_paths': '',
        'root_dir': '',
        'save_file_name': '',
        'csv_file_name': '',
        'out_root_path': '',
        'map_folder': '',
        'load_path': '',
        'n_classes': 4,
        'out_border': 0,
        'fixed_ar': 0.0,
        'img_ext': 'png',
        'batch_size': 1,
        'show_img': 0,
        'save_video': 0,
        'out_mask_size': '0x0',
        'n_frames': 0,
        'codec': 'H264',
        'fps': 20,
        'save_raw_mask': 0,
        'save_test': 1,
        'save_train': 1,
        'masks_per_seq': 0,
        'combine_seq': 0,
        'map_to_bbox': 0,
        'out_img_dir': 'images',
        'enable_out_suffix': 1,
        'enable_xml_annotations': 1,
        'allow_skipping_images': 0,
    }
    processArguments(sys.argv[1:], params)
    seq_paths = params['seq_paths']
    root_dir = params['root_dir']
    out_border = params['out_border']
    fixed_ar = params['fixed_ar']
    out_mask_size = params['out_mask_size']
    show_img = params['show_img']
    save_raw_mask = params['save_raw_mask']
    save_test = params['save_test']
    masks_per_seq = params['masks_per_seq']
    out_root_path = params['out_root_path']
    enable_out_suffix = params['enable_out_suffix']
    enable_xml_annotations = params['enable_xml_annotations']
    allow_skipping_images = params['allow_skipping_images']
    save_train = params['save_train']
    combine_seq = params['combine_seq']
    map_to_bbox = params['map_to_bbox']
    out_img_dir = params['out_img_dir']

    if combine_seq and not out_root_path:
        raise IOError('out_root_path must be provided to combine sequences')

    if seq_paths:
        if os.path.isfile(seq_paths):
            seq_paths = [
                x.strip() for x in open(seq_paths).readlines() if x.strip()
            ]
        else:
            seq_paths = seq_paths.split(',')
        if root_dir:
            seq_paths = [os.path.join(root_dir, name) for name in seq_paths]

    elif root_dir:
        seq_paths = [
            os.path.join(root_dir, name) for name in os.listdir(root_dir)
            if os.path.isdir(os.path.join(root_dir, name))
        ]
        seq_paths.sort(key=sortKey)
    else:
        raise IOError('Either seq_paths or root_dir must be provided')

    out_mask_size = [int(x) for x in out_mask_size.split('x')]
    train_fnames = {}
    test_fnames = {}
    total_n_masks = 0
    for seq_path in seq_paths:
        voc_path = os.path.join(seq_path, 'annotations')
        n_masks = saveMasks(seq_path,
                            voc_path,
                            out_mask_size,
                            out_border,
                            fixed_ar,
                            save_raw_mask,
                            show_img=show_img,
                            save_test=save_test,
                            masks_per_seq=masks_per_seq,
                            out_root_path=out_root_path,
                            save_train=save_train,
                            out_img_dir=out_img_dir,
                            train_fnames=train_fnames,
                            test_fnames=test_fnames,
                            map_to_bbox=map_to_bbox,
                            enable_out_suffix=enable_out_suffix,
                            allow_skipping_images=allow_skipping_images,
                            enable_xml_annotations=enable_xml_annotations)
        total_n_masks += n_masks

        if _exit:
            break
    print('total_n_masks: {}'.format(total_n_masks))

    if combine_seq:
        if save_train:

            out_train_img_path = os.path.join(out_root_path, 'images')
            out_train_mask_path = os.path.join(out_root_path, 'labels')

            print('Wrtiting combined training sequence to: {}, {}'.format(
                out_train_img_path, out_train_mask_path))

            if not os.path.isdir(out_train_img_path):
                os.makedirs(out_train_img_path)
            if not os.path.isdir(out_train_mask_path):
                os.makedirs(out_train_mask_path)
            combined_csv = []
            for out_seq_name in train_fnames:
                seq_fnames, seq_root, seq_csv, seq_csv_path = train_fnames[
                    out_seq_name]
                for src_img_fname, src_mask_fname in seq_fnames:
                    dst_img_fname = os.path.join(
                        out_train_img_path,
                        '{}_{}'.format(out_seq_name,
                                       os.path.basename(src_img_fname)))
                    dst_mask_fname = os.path.join(
                        out_train_mask_path,
                        '{}_{}'.format(out_seq_name,
                                       os.path.basename(src_mask_fname)))
                    shutil.move(src_img_fname, dst_img_fname)
                    shutil.move(src_mask_fname, dst_mask_fname)
                if seq_csv_path:
                    for raw_data in seq_csv:
                        raw_data['filename'] = '{}_{}'.format(
                            out_seq_name, raw_data['filename'])
                    combined_csv += seq_csv
                    os.remove(seq_csv_path)
                if isEmpty(seq_root):
                    shutil.rmtree(seq_root)
                else:
                    print('Not deleting non-empty {}'.format(seq_root))
            combined_csv_path = os.path.join(out_train_img_path,
                                             'annotations.csv')
            pd.DataFrame(combined_csv).to_csv(combined_csv_path)

        if save_test:
            out_test_img_path = os.path.join(
                out_root_path, 'test') if save_train else out_root_path
            print('Wrtiting combined test sequence to: {}'.format(
                out_test_img_path))
            if not os.path.isdir(out_test_img_path):
                os.makedirs(out_test_img_path)
            combined_csv = []
            for out_seq_name in test_fnames:
                seq_fnames, seq_root, seq_csv, seq_csv_path = test_fnames[
                    out_seq_name]
                for src_img_fname in seq_fnames:
                    dst_img_fname = os.path.join(
                        out_test_img_path,
                        '{}_{}'.format(out_seq_name,
                                       os.path.basename(src_img_fname)))
                    shutil.move(src_img_fname, dst_img_fname)
                if seq_csv_path:
                    for raw_data in seq_csv:
                        raw_data['filename'] = '{}_{}'.format(
                            out_seq_name, raw_data['filename'])
                    combined_csv += seq_csv
                    os.remove(seq_csv_path)
                if isEmpty(seq_root):
                    shutil.rmtree(seq_root)
                else:
                    print('Not deleting non-empty {}'.format(seq_root))
            combined_csv_path = os.path.join(out_test_img_path,
                                             'annotations.csv')
            pd.DataFrame(combined_csv).to_csv(combined_csv_path)
Example #4
0
    'hist_match_type': 0,
    'random_bkgs': 0,
    'aug_seq_size': 1000,
    'visualize': 0,
    'bkg_size': '1280x720',
    'bkg_iou_thresh': 0.1,
    'only_one_src_obj': 0,
    'inclue_src_in_augmented': 1,
    'load_bkg': 1,
    'src_img_per_seq': 0,
    'flip_lr_prob': 0.0,
    'sample_frg_per_bkg': 0,
    'mask_threshold': 127,
}

processArguments(sys.argv[1:], params)

src_paths = params['src_paths']
src_root_dir = params['src_root_dir']
src_postfix = params['src_postfix']
img_dir = params['img_dir']
img_ext = params['img_ext']

mask_paths = params['mask_paths']
mask_root_dir = params['mask_root_dir']
mask_postfix = params['mask_postfix']
mask_dir = params['mask_dir']
mask_ext = params['mask_ext']
mask_border = params['mask_border']

labels_path = params['labels_path']