From c0c1eb7f5ca2e4248f1a824f3428fb566ed5cf28 Mon Sep 17 00:00:00 2001 From: kba Date: Wed, 14 Aug 2024 19:49:26 +0200 Subject: [PATCH] adapt to bertsky/core#8 --- ocrd_kraken/binarize.py | 14 ++++++++------ ocrd_kraken/recognize.py | 7 ++++--- ocrd_kraken/segment.py | 6 ++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ocrd_kraken/binarize.py b/ocrd_kraken/binarize.py index 809a919..08038c9 100644 --- a/ocrd_kraken/binarize.py +++ b/ocrd_kraken/binarize.py @@ -2,6 +2,8 @@ import os from os.path import join from typing import Optional + +from ocrd_models.ocrd_process_result import OcrdProcessResult import kraken.binarization from ocrd import Processor from ocrd_utils import assert_file_grp_cardinality, getLogger, make_file_id, MIMETYPE_PAGE @@ -22,7 +24,7 @@ def setup(self): assert_file_grp_cardinality(self.input_file_grp, 1) assert_file_grp_cardinality(self.output_file_grp, 1) - def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[str] = None, page_id : Optional[str] = None) -> OcrdPage: + def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdProcessResult: """Binarize the pages/regions/lines with Kraken. Iterate over the input PAGE element hierarchy down to the requested @@ -50,14 +52,14 @@ def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[ assert page page_image, page_xywh, _ = self.workspace.image_from_page( page, page_id, feature_filter='binarized') - ret = [pcgts] + images = [] if self.parameter['level-of-operation'] == 'page': self.logger.info("Binarizing page '%s'", page_id) bin_image = kraken.binarization.nlbin(page_image) bin_image_id = f'{output_file_id}.IMG-BIN' bin_image_path = join(self.output_file_grp, f'{bin_image_id}.png') page.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments=f'{page_xywh["features"]},binarized')) - ret.append((bin_image, bin_image_id, bin_image_path)) + images.append((bin_image, bin_image_id, bin_image_path)) else: for region in page.get_AllRegions(classes=['Text']): region_image, region_xywh = self.workspace.image_from_segment( @@ -68,7 +70,7 @@ def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[ bin_image_id = f'{output_file_id}_{region.id}.IMG-BIN' bin_image_path = join(self.output_file_grp, f'{bin_image_id}.png') region.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments=f'{region_xywh["features"]},binarized')) - ret.append((bin_image, bin_image_id, bin_image_path)) + images.append((bin_image, bin_image_id, bin_image_path)) else: for line in region.get_TextLine(): line_image, line_xywh = self.workspace.image_from_segment( @@ -78,5 +80,5 @@ def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[ bin_image_id = f'{output_file_id}_{region.id}_{line.id}.IMG-BIN' bin_image_path = join(self.output_file_grp, f'{bin_image_id}.png') line.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments=f'{page_xywh["features"]},binarized')) - ret.append((bin_image, bin_image_id, bin_image_path)) - return ret + images.append((bin_image, bin_image_id, bin_image_path)) + return OcrdProcessResult(pcgts, images) diff --git a/ocrd_kraken/recognize.py b/ocrd_kraken/recognize.py index a0773d1..8635551 100644 --- a/ocrd_kraken/recognize.py +++ b/ocrd_kraken/recognize.py @@ -1,5 +1,6 @@ from os.path import join -from typing import Union +from typing import Optional, Union +from ocrd_models import OcrdProcessResult import regex import itertools import numpy as np @@ -77,7 +78,7 @@ def predict(page_image, segmentation): self.parameter['bidi_reordering']) self.predict = predict - def process_page_pcgts(self, *input_pcgts, output_file_id: str = None, page_id: str = None) -> OcrdPage: + def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdProcessResult: """Recognize text on lines with Kraken. Open the parsed PAGE-XML file, then iterate over the element hierarchy @@ -223,7 +224,7 @@ def process_page_pcgts(self, *input_pcgts, output_file_id: str = None, page_id: page_update_higher_textequiv_levels('line', pcgts) self.logger.info("Finished recognition, serializing") - return pcgts + return OcrdProcessResult(pcgts) # zzz should go into core ocrd_utils def baseline_of_segment(segment, coords): diff --git a/ocrd_kraken/segment.py b/ocrd_kraken/segment.py index bdce58f..3b29926 100644 --- a/ocrd_kraken/segment.py +++ b/ocrd_kraken/segment.py @@ -3,6 +3,7 @@ from os.path import join from ocrd import Processor +from ocrd_models import OcrdProcessResult from ocrd_utils import ( getLogger, assert_file_grp_cardinality, @@ -72,7 +73,7 @@ def segmenter(img, mask=None): return segment(img, mask=mask, **kwargs) self.segmenter = segmenter - def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[str] = None, page_id : Optional[str] = None) -> OcrdPage: + def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdProcessResult: """Segment into (regions and) lines with Kraken. Iterate over the element hierarchy of the PAGE-XML down to the @@ -100,6 +101,7 @@ def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[ pcgts = input_pcgts[0] page = pcgts.get_Page() + assert page page_image, page_coords, page_info = self.workspace.image_from_page( page, page_id, feature_selector="binarized" if self.use_legacy else "") @@ -142,7 +144,7 @@ def process_page_pcgts(self, *input_pcgts : OcrdPage, output_file_id : Optional[ self.logger.warning('Keeping %d lines in region "%s"', len(region.TextLine or []), region.id) self._process_region(page_image, page_coords, region, zoom) - return pcgts + return OcrdProcessResult(pcgts) def _process_page(self, page_image, page_coords, page, zoom=1.0): def getmask():