예제 #1
0
def main(table_like_format):
    # make data list
    for m in models:
        print(m)
    data_set = prepare_data()
    prev_model_root = None

    if table_like_format:
        print('model\tweights\tkey\t'
              'precision\trecall\tf1\t'
              'precision_c\trecall_C\tf1_c\t'
              'd_by_doc\td_by_char\td_by_char_avg')
    for model_root, model_weights in models:
        if model_root != prev_model_root:
            if not table_like_format:
                print('model: ', model_root)
            else:
                print()
            prev_model_root = model_root
        if verbose:
            print('evaluating weights: ', model_weights)
        params_fn = Path(local_config.data_path) / model_root / 'param.txt'
        if not params_fn.is_file():
            params_fn = Path(local_config.data_path) / (
                model_root + '.param.txt')  # старый вариант
            assert params_fn.is_file(), str(params_fn)
        recognizer = infer_retinanet.BrailleInference(
            params_fn=params_fn,
            model_weights_fn=os.path.join(local_config.data_path, model_root,
                                          model_weights),
            create_script=None,
            inference_width=inference_width,
            verbose=verbose)
        for key, data_list in data_set.items():
            res = validate_model(recognizer,
                                 data_list,
                                 do_filter_lonely_rects=do_filter_lonely_rects,
                                 metrics_for_lines=metrics_for_lines)
            # print('{model_weights} {key} precision: {res[precision]:.4}, recall: {res[recall]:.4} f1: {res[f1]:.4} '
            #       'precision_r: {res[precision_r]:.4}, recall_r: {res[recall_r]:.4} f1_r: {res[f1_r]:.4} '
            #       'd_by_doc: {res[d_by_doc]:.4} d_by_char: {res[d_by_char]:.4} '
            #       'd_by_char_avg: {res[d_by_char_avg]:.4}'.format(model_weights=model_weights, key=key, res=res))
            if table_like_format:
                print(
                    '{model}\t{weights}\t{key}\t'
                    '{res[precision_r]:.4}\t{res[recall_r]:.4}\t{res[f1_r]:.4}\t'
                    '{res[precision_c]:.4}\t{res[recall_c]:.4}\t{res[f1_c]:.4}\t'
                    '{res[d_by_doc]:.4}\t{res[d_by_char]:.4}\t'
                    '{res[d_by_char_avg]:.4}'.format(model=model_root,
                                                     weights=model_weights,
                                                     key=key,
                                                     res=res))
            else:
                print(
                    '{model_weights} {key} '
                    'precision_r: {res[precision_r]:.4}, recall_r: {res[recall_r]:.4} f1_r: {res[f1_r]:.4} '
                    'd_by_doc: {res[d_by_doc]:.4} d_by_char: {res[d_by_char]:.4} '
                    'd_by_char_avg: {res[d_by_char_avg]:.4}'.format(
                        model_weights=model_weights, key=key, res=res))
 def get_recognizer(self):
     global recognizer
     if recognizer is None:
         print("infer_retinanet.BrailleInference()")
         t = timeit.default_timer()
         recognizer = infer_retinanet.BrailleInference(verbose=1,
             params_fn=os.path.join(MODEL_PATH, 'weights', 'param.txt'),
             model_weights_fn=os.path.join(MODEL_PATH, 'weights', MODEL_WEIGHTS),
             create_script=None)
         print(timeit.default_timer() - t)
     return recognizer
예제 #3
0
def evaluate_accuracy(params_fn,
                      model,
                      device,
                      data_list,
                      do_filter_lonely_rects=False,
                      metrics_for_lines=True):
    """
    :param recognizer: infer_retinanet.BrailleInference instance
    :param data_list:  list of (image filename, groundtruth pseudotext)
    :return: (<distance> avg. by documents, <distance> avg. by char, <<distance> avg. by char> avg. by documents>)
    """
    # по символам
    recognizer = infer_retinanet.BrailleInference(
        params_fn=params_fn,
        model_weights_fn=model,
        create_script=None,
        inference_width=inference_width,
        device=device,
        verbose=verbose)

    tp_c = 0
    fp_c = 0
    fn_c = 0
    for gt_dict in data_list:
        img_fn, gt_text, gt_rects = gt_dict['image_fn'], gt_dict[
            'gt_text'], gt_dict['gt_rects']
        res_dict = recognizer.run(
            img_fn,
            lang=lang,
            draw_refined=infer_retinanet.BrailleInference.DRAW_NONE,
            find_orientation=False,
            process_2_sides=False,
            align_results=False,
            repeat_on_aligned=False,
            gt_rects=gt_rects)
        lines = res_dict['lines']
        if do_filter_lonely_rects:
            lines, filtered_chars = postprocess.filter_lonely_rects_for_lines(
                lines)
        if metrics_for_lines:
            boxes = []
            labels = []
            for ln in lines:
                boxes += [ch.refined_box for ch in ln.chars]
                labels += [ch.label for ch in ln.chars]
        else:
            boxes = res_dict['boxes']
            labels = res_dict['labels']
        tpi, fpi, fni = char_metrics_rects(
            boxes=boxes,
            labels=labels,
            gt_rects=res_dict['gt_rects'],
            image_wh=(res_dict['labeled_image'].width,
                      res_dict['labeled_image'].height),
            img=None,
            do_filter_lonely_rects=do_filter_lonely_rects)
        tp_c += tpi
        fp_c += fpi
        fn_c += fni
    precision_c = tp_c / (tp_c + fp_c) if tp_c + fp_c != 0 else 0.
    recall_c = tp_c / (tp_c + fn_c) if tp_c + fn_c != 0 else 0.
    return {
        'precision':
        precision_c,
        'recall':
        recall_c,
        'f1':
        2 * precision_c * recall_c /
        (precision_c + recall_c) if precision_c + recall_c != 0 else 0.,
    }
예제 #4
0
parser = argparse.ArgumentParser(description='Angelina Braille Reader: optical Braille text recognizer .')

parser.add_argument('input', type=str, help='File(s) to be processed: image, pdf or zip file or directory name')
parser.add_argument('results_dir', nargs='?', type=str, help='(Optional) output directory. If not specified, results are placed at input location')
parser.add_argument('-l', '--lang', type=str, default='RU', help='(Optional) Document language (RU, EN, LV, GR or UZ). If not specified, is RU')
parser.add_argument('-o', '--orient', action='store_false', help="Don't find orientation, use original file orientation")
parser.add_argument('-2', dest='two', action='store_true', help="Process 2 sides")

args = parser.parse_args()

if not Path(args.input).exists():
    print('input file/path does not exist: ' + args.input)
    exit()

recognizer = infer_retinanet.BrailleInference(
    params_fn=os.path.join(local_config.data_path, 'weights', 'param.txt'),
    model_weights_fn=os.path.join(local_config.data_path, 'weights', model_weights),
    create_script=None)

if Path(args.input).is_dir():
    results_dir = args.results_dir or args.input
    recognizer.process_dir_and_save(str(Path(args.input)/'**'/'*.*'), results_dir,
                                    lang=args.lang, extra_info=None,
                                    draw_refined=recognizer.DRAW_NONE,
                                    remove_labeled_from_filename=False,
                                    find_orientation=args.orient,
                                    align_results=True,
                                    process_2_sides=args.two,
                                    repeat_on_aligned=False,
                                    save_development_info=False)
else:
    results_dir = args.results_dir or Path(args.input).parent