From e503c1a0b79cee2371e20dc30ed310cf38ff6e36 Mon Sep 17 00:00:00 2001 From: kba Date: Wed, 26 Nov 2025 18:19:03 +0100 Subject: [PATCH] drop obsolete multi-model binarization --- src/eynollah/model_zoo/default_specs.py | 36 -------------- src/eynollah/ocrd_cli_binarization.py | 4 +- src/eynollah/sbb_binarize.py | 63 +++++++++++-------------- 3 files changed, 29 insertions(+), 74 deletions(-) diff --git a/src/eynollah/model_zoo/default_specs.py b/src/eynollah/model_zoo/default_specs.py index 2bbbf15..a720fa0 100644 --- a/src/eynollah/model_zoo/default_specs.py +++ b/src/eynollah/model_zoo/default_specs.py @@ -45,42 +45,6 @@ DEFAULT_MODEL_SPECS = EynollahModelSpecSet([ type='Keras', ), - EynollahModelSpec( - category="binarization_multi_1", - variant='', - filename="models_eynollah/eynollah-binarization-multi_2020_01_16/model_bin1", - dist_url=dist_url("binarization"), - dists=['binarization'], - type='Keras', - ), - - EynollahModelSpec( - category="binarization_multi_2", - variant='', - filename="models_eynollah/eynollah-binarization-multi_2020_01_16/model_bin2", - dist_url=dist_url("binarization"), - dists=['binarization'], - type='Keras', - ), - - EynollahModelSpec( - category="binarization_multi_3", - variant='', - filename="models_eynollah/eynollah-binarization-multi_2020_01_16/model_bin3", - dist_url=dist_url("binarization"), - dists=['binarization'], - type='Keras', - ), - - EynollahModelSpec( - category="binarization_multi_4", - variant='', - filename="models_eynollah/eynollah-binarization-multi_2020_01_16/model_bin4", - dist_url=dist_url("binarization"), - dists=['binarization'], - type='Keras', - ), - EynollahModelSpec( category="col_classifier", variant='', diff --git a/src/eynollah/ocrd_cli_binarization.py b/src/eynollah/ocrd_cli_binarization.py index a199e72..f234520 100644 --- a/src/eynollah/ocrd_cli_binarization.py +++ b/src/eynollah/ocrd_cli_binarization.py @@ -40,7 +40,7 @@ class SbbBinarizeProcessor(Processor): # resolve relative path via OCR-D ResourceManager assert isinstance(self.parameter, frozendict) model_zoo = EynollahModelZoo(basedir=self.parameter['model']) - self.binarizer = SbbBinarizer(model_zoo=model_zoo, mode='single', logger=self.logger) + self.binarizer = SbbBinarizer(model_zoo=model_zoo, logger=self.logger) def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: Optional[str] = None) -> OcrdPageResult: """ @@ -103,7 +103,7 @@ class SbbBinarizeProcessor(Processor): line_image_bin = cv2pil(self.binarizer.run(image=pil2cv(line_image), use_patches=True)) # update PAGE (reference the image file): line_image_ref = AlternativeImageType(comments=line_xywh['features'] + ',binarized') - line.add_AlternativeImage(region_image_ref) + line.add_AlternativeImage(line_image_ref) result.images.append(OcrdPageResultImage(line_image_bin, line.id + '.IMG-BIN', line_image_ref)) return result diff --git a/src/eynollah/sbb_binarize.py b/src/eynollah/sbb_binarize.py index 77741e9..28dc84a 100644 --- a/src/eynollah/sbb_binarize.py +++ b/src/eynollah/sbb_binarize.py @@ -33,12 +33,10 @@ class SbbBinarizer: self, *, model_zoo: EynollahModelZoo, - mode: str, logger: Optional[logging.Logger] = None, ): self.logger = logger if logger else logging.getLogger('eynollah.binarization') - self.model_zoo = model_zoo - self.models = self.setup_models(mode) + self.models = (model_zoo.model_path('binarization'), model_zoo.load_model('binarization')) self.session = self.start_new_session() def start_new_session(self): @@ -49,12 +47,6 @@ class SbbBinarizer: tensorflow_backend.set_session(session) return session - def setup_models(self, mode: str) -> Dict[Path, AnyModel]: - return { - self.model_zoo.model_path(v): self.model_zoo.load_model(v) - for v in (['binarization'] if mode == 'single' else [f'binarization_multi_{i}' for i in range(1, 5)]) - } - def end_session(self): tensorflow_backend.clear_session() self.session.close() @@ -330,21 +322,21 @@ class SbbBinarizer: if image_path is not None: image = cv2.imread(image_path) img_last = 0 - for n, (model_file, model) in enumerate(self.models.items()): - self.logger.info('Predicting %s with model %s [%s/%s]', image_path if image_path else '[image]', model_file, n + 1, len(self.models.keys())) - res = self.predict(model, image, use_patches) + model_file, model = self.models + self.logger.info('Predicting %s with model %s [%s/%s]', image_path if image_path else '[image]', model_file) + res = self.predict(model, image, use_patches) - img_fin = np.zeros((res.shape[0], res.shape[1], 3)) - res[:, :][res[:, :] == 0] = 2 - res = res - 1 - res = res * 255 - img_fin[:, :, 0] = res - img_fin[:, :, 1] = res - img_fin[:, :, 2] = res + img_fin = np.zeros((res.shape[0], res.shape[1], 3)) + res[:, :][res[:, :] == 0] = 2 + res = res - 1 + res = res * 255 + img_fin[:, :, 0] = res + img_fin[:, :, 1] = res + img_fin[:, :, 2] = res - img_fin = img_fin.astype(np.uint8) - img_fin = (res[:, :] == 0) * 255 - img_last = img_last + img_fin + img_fin = img_fin.astype(np.uint8) + img_fin = (res[:, :] == 0) * 255 + img_last = img_last + img_fin kernel = np.ones((5, 5), np.uint8) img_last[:, :][img_last[:, :] > 0] = 255 @@ -361,22 +353,21 @@ class SbbBinarizer: self.logger.info('Binarizing [%3d/%d] %s', i + 1, len(ls_imgs), image_name) image = cv2.imread(os.path.join(dir_in,image_name) ) img_last = 0 - for n, (model_file, model) in enumerate(self.models.items()): - self.logger.info('Predicting %s with model %s [%s/%s]', image_name, model_file, n + 1, len(self.models.keys())) + model_file, model = self.models + self.logger.info('Predicting %s with model %s [%s/%s]', image_path if image_path else '[image]', model_file) + res = self.predict(model, image, use_patches) - res = self.predict(model, image, use_patches) + img_fin = np.zeros((res.shape[0], res.shape[1], 3)) + res[:, :][res[:, :] == 0] = 2 + res = res - 1 + res = res * 255 + img_fin[:, :, 0] = res + img_fin[:, :, 1] = res + img_fin[:, :, 2] = res - img_fin = np.zeros((res.shape[0], res.shape[1], 3)) - res[:, :][res[:, :] == 0] = 2 - res = res - 1 - res = res * 255 - img_fin[:, :, 0] = res - img_fin[:, :, 1] = res - img_fin[:, :, 2] = res - - img_fin = img_fin.astype(np.uint8) - img_fin = (res[:, :] == 0) * 255 - img_last = img_last + img_fin + img_fin = img_fin.astype(np.uint8) + img_fin = (res[:, :] == 0) * 255 + img_last = img_last + img_fin kernel = np.ones((5, 5), np.uint8) img_last[:, :][img_last[:, :] > 0] = 255