Exemple #1
0
def construct_lm(path, config_path='', logger=logging):
    try:
        lm = language_model.torchscript_import(compose_path(path, config_path))
    except language_model.UnreadableModelError as e:
        logger.warning(
            'WARNING: Failed to load model as TorchScript file, original error:\n'
        )
        logger.warning(f'{e}\n')
        logger.warning(
            'WARNING: Attempting to load as a plain torch-pickled model...\n')
        lm = torch.load(compose_path(path, config_path),
                        map_location=torch.device('cpu'))

    lm._unused_prefix_len = 2

    return lm
Exemple #2
0
 def __init__(self, config, config_path=''):
     json_file = compose_path(config['OCR_JSON'], config_path)
     if 'METHOD' in config and config['METHOD'] == 'pytorch_ocr':
         from pero_ocr.ocr_engine.pytorch_ocr_engine import PytorchEngineLineOCR
         self.ocr_engine = PytorchEngineLineOCR(json_file, gpu_id=0)
     else:
         self.ocr_engine = line_ocr_engine.EngineLineOCR(json_file,
                                                         gpu_id=0)
Exemple #3
0
    def __init__(self, config, config_path):
        self.filter_directions = config.getboolean('FILTER_DIRECTIONS')
        self.filter_incomplete_pages = config.getboolean(
            'FILTER_INCOMPLETE_PAGES')

        if self.filter_directions:
            self.engine = LineFilterEngine(
                model_path=compose_path(config['MODEL_PATH'], config_path),
                gpu_fraction=config.getfloat('GPU_FRACTION'))
Exemple #4
0
def page_decoder_factory(config, config_path=''):
    from pero_ocr.decoding import decoding_itf
    ocr_chars = decoding_itf.get_ocr_charset(
        compose_path(config['OCR']['OCR_JSON'], config_path))
    decoder = decoding_itf.decoder_factory(config['DECODER'],
                                           ocr_chars,
                                           allow_no_decoder=False,
                                           use_gpu=True,
                                           config_path=config_path)
    confidence_threshold = config['DECODER'].getfloat('CONFIDENCE_THRESHOLD',
                                                      fallback=math.inf)
    return PageDecoder(decoder, line_confidence_threshold=confidence_threshold)
Exemple #5
0
    def __init__(self, config, config_path):
        self.filter_directions = config.getboolean('FILTER_DIRECTIONS')
        self.filter_incomplete_pages = config.getboolean(
            'FILTER_INCOMPLETE_PAGES')
        self.filter_pages_with_short_lines = config.getboolean(
            'FILTER_PAGES_WITH_SHORT_LINES')
        self.length_threshold = config.getint('LENGTH_THRESHOLD')

        if self.filter_directions:
            self.engine = LineFilterEngine(
                model_path=compose_path(config['MODEL_PATH'], config_path),
                framework=config['FRAMEWORK'],
                gpu_fraction=config.getfloat('GPU_FRACTION'),
                use_cpu=config.getboolean('USE_CPU'))
Exemple #6
0
 def __init__(self, config, config_path=''):
     self.detect_regions = config.getboolean('DETECT_REGIONS')
     self.detect_lines = config.getboolean('DETECT_LINES')
     self.merge_lines = config.getboolean('MERGE_LINES')
     self.adjust_lines = config.getboolean('REFINE_LINES')
     self.multi_orientation = config.getboolean('MULTI_ORIENTATION')
     self.engine = LayoutEngine(
         model_path=compose_path(config['MODEL_PATH'], config_path),
         downsample=config.getint('DOWNSAMPLE'),
         pad=config.getint('PAD'),
         use_cpu=config.getboolean('USE_CPU'),
         detection_threshold=config.getfloat('DETECTION_THRESHOLD'),
         max_mp=config.getfloat('MAX_MEGAPIXELS'),
         gpu_fraction=config.getfloat('GPU_FRACTION'))
     self.pool = Pool()
Exemple #7
0
 def __init__(self, config, config_path=''):
     self.detect_regions = config.getboolean('DETECT_REGIONS')
     self.detect_lines = config.getboolean('DETECT_LINES')
     self.detect_straight_lines_in_regions = config.getboolean(
         'DETECT_STRAIGHT_LINES_IN_REGIONS')
     self.merge_lines = config.getboolean('MERGE_LINES')
     self.adjust_heights = config.getboolean('ADJUST_HEIGHTS')
     self.multi_orientation = config.getboolean('MULTI_ORIENTATION')
     self.adjust_baselines = config.getboolean('ADJUST_BASELINES')
     self.engine = LayoutEngine(
         framework=config.get('FRAMEWORK', fallback='tf'),
         model_path=compose_path(config['MODEL_PATH'], config_path),
         downsample=config.getint('DOWNSAMPLE'),
         adaptive_downsample=config.getboolean('ADAPTIVE_DOWNSAMPLE',
                                               fallback=True),
         pad=config.getint('PAD'),
         use_cpu=config.getboolean('USE_CPU'),
         detection_threshold=config.getfloat('DETECTION_THRESHOLD'),
         max_mp=config.getfloat('MAX_MEGAPIXELS'),
         gpu_fraction=config.getfloat('GPU_FRACTION'))
     self.pool = Pool()
Exemple #8
0
if __name__ == '__main__':
    args = parseargs()

    config_path = args.config_path
    lmdb_db = args.lmdb
    input_file_path = args.input_file_path
    output_file_path = args.output_file_path
    batch_size = 32

    config = configparser.ConfigParser()
    config.read(config_path)

    setGPU()

    ocr_chars = decoding_itf.get_ocr_charset(
        compose_path(config['OCR']['OCR_JSON'], config_path))
    decoder = decoding_itf.decoder_factory(config['DECODER'],
                                           ocr_chars,
                                           allow_no_decoder=False,
                                           use_gpu=True,
                                           config_path=config_path)
    confidence_threshold = config['DECODER'].getfloat('CONFIDENCE_THRESHOLD',
                                                      fallback=math.inf)

    json_file = compose_path(config['OCR']['OCR_JSON'], config_path)
    ocr_engine = PytorchEngineLineOCR(json_file, gpu_id=0)

    env = lmdb.open(lmdb_db)
    txn = env.begin()

    f = open(input_file_path, "r")
Exemple #9
0
def construct_lm(path, config_path=''):
    lm = torch.load(compose_path(path, config_path),
                    map_location=torch.device('cpu'))
    lm._unused_prefix_len = 2

    return lm