Skip to content

Commit

Permalink
update to latest OcrdPageResult and process_page_pcgts
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Aug 15, 2024
1 parent e76d708 commit 2832722
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
27 changes: 11 additions & 16 deletions ocrd_kraken/binarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def executable(self):
def setup(self):
self.logger = getLogger('processor.KrakenBinarize')

def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdPageResult:
def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: Optional[str] = None) -> OcrdPageResult:
"""Binarize the pages/regions/lines with Kraken.
Iterate over the input PAGE element hierarchy down to the requested
Expand All @@ -47,37 +47,32 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
self.logger.debug('Level of operation: "%s"', self.parameter['level-of-operation'])

pcgts = input_pcgts[0]
assert pcgts
page = pcgts.get_Page()
assert page
page_image, page_xywh, _ = self.workspace.image_from_page(
page, page_id, feature_filter='binarized')
result = OcrdPageResult(pcgts)
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'))
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
alternative_image = AlternativeImageType(comments=f'{page_xywh["features"]},binarized')
page.add_AlternativeImage(alternative_image)
result.images.append(OcrdPageResultImage(kraken.binarization.nlbin(page_image), '.IMG-BIN', alternative_image))
else:
for region in page.get_AllRegions(classes=['Text']):
region_image, region_xywh = self.workspace.image_from_segment(
region, page_image, page_xywh, feature_filter='binarized')
if self.parameter['level-of-operation'] == 'region':
self.logger.info("Binarizing region '%s'", region.id)
bin_image = kraken.binarization.nlbin(region_image)
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'))
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
alternative_image = AlternativeImageType(comments=f'{region_xywh["features"]},binarized')
region.add_AlternativeImage(alternative_image)
result.images.append(OcrdPageResultImage(kraken.binarization.nlbin(region_image), f'{region.id}.IMG-BIN', alternative_image))
else:
for line in region.get_TextLine():
line_image, line_xywh = self.workspace.image_from_segment(
line, region_image, region_xywh, feature_filter='binarized')
self.logger.info("Binarizing line '%s'", line.id)
bin_image = kraken.binarization.nlbin(line_image)
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'))
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
alternative_image = AlternativeImageType(comments=f'{line_xywh["features"]},binarized')
line.add_AlternativeImage(alternative_image)
result.images.append(OcrdPageResultImage(kraken.binarization.nlbin(line_image), f'{region.id}_{line.id}.IMG-BIN', alternative_image))
return result
4 changes: 3 additions & 1 deletion ocrd_kraken/recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def predict(page_image, segmentation):
self.parameter['bidi_reordering'])
self.predict = predict

def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdPageResult:
def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: Optional[str] = None) -> OcrdPageResult:
"""Recognize text on lines with Kraken.
Open the parsed PAGE-XML file, then iterate over the element hierarchy
Expand All @@ -92,7 +92,9 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
from kraken.containers import Segmentation, BaselineLine, BBoxLine

pcgts = input_pcgts[0]
assert pcgts
page = pcgts.get_Page()
assert page
page_image, page_coords, _ = self.workspace.image_from_page(
page, page_id,
feature_selector="binarized"
Expand Down
3 changes: 2 additions & 1 deletion ocrd_kraken/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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) -> OcrdPageResult:
def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: Optional[str] = None) -> OcrdPageResult:
"""Segment into (regions and) lines with Kraken.
Iterate over the element hierarchy of the PAGE-XML down to the
Expand All @@ -89,6 +89,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
"""

pcgts = input_pcgts[0]
assert pcgts
page = pcgts.get_Page()
assert page
page_image, page_coords, page_info = self.workspace.image_from_page(
Expand Down

0 comments on commit 2832722

Please sign in to comment.