Skip to content

Commit

Permalink
adapt to bertsky/core#8
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Aug 14, 2024
1 parent 9ea80c7 commit c0c1eb7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
14 changes: 8 additions & 6 deletions ocrd_kraken/binarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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)
7 changes: 4 additions & 3 deletions ocrd_kraken/recognize.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions ocrd_kraken/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "")
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit c0c1eb7

Please sign in to comment.