mirror of
https://github.com/qurator-spk/eynollah.git
synced 2025-12-01 08:44:13 +01:00
drop obsolete multi-model binarization
This commit is contained in:
parent
82266f8234
commit
e503c1a0b7
3 changed files with 29 additions and 74 deletions
|
|
@ -45,42 +45,6 @@ DEFAULT_MODEL_SPECS = EynollahModelSpecSet([
|
||||||
type='Keras',
|
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(
|
EynollahModelSpec(
|
||||||
category="col_classifier",
|
category="col_classifier",
|
||||||
variant='',
|
variant='',
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ class SbbBinarizeProcessor(Processor):
|
||||||
# resolve relative path via OCR-D ResourceManager
|
# resolve relative path via OCR-D ResourceManager
|
||||||
assert isinstance(self.parameter, frozendict)
|
assert isinstance(self.parameter, frozendict)
|
||||||
model_zoo = EynollahModelZoo(basedir=self.parameter['model'])
|
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:
|
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))
|
line_image_bin = cv2pil(self.binarizer.run(image=pil2cv(line_image), use_patches=True))
|
||||||
# update PAGE (reference the image file):
|
# update PAGE (reference the image file):
|
||||||
line_image_ref = AlternativeImageType(comments=line_xywh['features'] + ',binarized')
|
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))
|
result.images.append(OcrdPageResultImage(line_image_bin, line.id + '.IMG-BIN', line_image_ref))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
|
|
@ -33,12 +33,10 @@ class SbbBinarizer:
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
model_zoo: EynollahModelZoo,
|
model_zoo: EynollahModelZoo,
|
||||||
mode: str,
|
|
||||||
logger: Optional[logging.Logger] = None,
|
logger: Optional[logging.Logger] = None,
|
||||||
):
|
):
|
||||||
self.logger = logger if logger else logging.getLogger('eynollah.binarization')
|
self.logger = logger if logger else logging.getLogger('eynollah.binarization')
|
||||||
self.model_zoo = model_zoo
|
self.models = (model_zoo.model_path('binarization'), model_zoo.load_model('binarization'))
|
||||||
self.models = self.setup_models(mode)
|
|
||||||
self.session = self.start_new_session()
|
self.session = self.start_new_session()
|
||||||
|
|
||||||
def start_new_session(self):
|
def start_new_session(self):
|
||||||
|
|
@ -49,12 +47,6 @@ class SbbBinarizer:
|
||||||
tensorflow_backend.set_session(session)
|
tensorflow_backend.set_session(session)
|
||||||
return 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):
|
def end_session(self):
|
||||||
tensorflow_backend.clear_session()
|
tensorflow_backend.clear_session()
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|
@ -330,21 +322,21 @@ class SbbBinarizer:
|
||||||
if image_path is not None:
|
if image_path is not None:
|
||||||
image = cv2.imread(image_path)
|
image = cv2.imread(image_path)
|
||||||
img_last = 0
|
img_last = 0
|
||||||
for n, (model_file, model) in enumerate(self.models.items()):
|
model_file, model = self.models
|
||||||
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()))
|
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))
|
img_fin = np.zeros((res.shape[0], res.shape[1], 3))
|
||||||
res[:, :][res[:, :] == 0] = 2
|
res[:, :][res[:, :] == 0] = 2
|
||||||
res = res - 1
|
res = res - 1
|
||||||
res = res * 255
|
res = res * 255
|
||||||
img_fin[:, :, 0] = res
|
img_fin[:, :, 0] = res
|
||||||
img_fin[:, :, 1] = res
|
img_fin[:, :, 1] = res
|
||||||
img_fin[:, :, 2] = res
|
img_fin[:, :, 2] = res
|
||||||
|
|
||||||
img_fin = img_fin.astype(np.uint8)
|
img_fin = img_fin.astype(np.uint8)
|
||||||
img_fin = (res[:, :] == 0) * 255
|
img_fin = (res[:, :] == 0) * 255
|
||||||
img_last = img_last + img_fin
|
img_last = img_last + img_fin
|
||||||
|
|
||||||
kernel = np.ones((5, 5), np.uint8)
|
kernel = np.ones((5, 5), np.uint8)
|
||||||
img_last[:, :][img_last[:, :] > 0] = 255
|
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)
|
self.logger.info('Binarizing [%3d/%d] %s', i + 1, len(ls_imgs), image_name)
|
||||||
image = cv2.imread(os.path.join(dir_in,image_name) )
|
image = cv2.imread(os.path.join(dir_in,image_name) )
|
||||||
img_last = 0
|
img_last = 0
|
||||||
for n, (model_file, model) in enumerate(self.models.items()):
|
model_file, model = self.models
|
||||||
self.logger.info('Predicting %s with model %s [%s/%s]', image_name, model_file, n + 1, len(self.models.keys()))
|
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))
|
img_fin = img_fin.astype(np.uint8)
|
||||||
res[:, :][res[:, :] == 0] = 2
|
img_fin = (res[:, :] == 0) * 255
|
||||||
res = res - 1
|
img_last = img_last + img_fin
|
||||||
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
|
|
||||||
|
|
||||||
kernel = np.ones((5, 5), np.uint8)
|
kernel = np.ones((5, 5), np.uint8)
|
||||||
img_last[:, :][img_last[:, :] > 0] = 255
|
img_last[:, :][img_last[:, :] > 0] = 255
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue