Skip to content

Commit

Permalink
Merge pull request #3 from kba/port-to-v3-return-object
Browse files Browse the repository at this point in the history
update ocrd-cis-binarize to be compatible with bertsky/core#8
  • Loading branch information
MehmedGIT authored Aug 16, 2024
2 parents 0ba6839 + 6b06e88 commit 6b19f35
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 8 additions & 7 deletions ocrd_cis/ocropy/binarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def setup(self):
self.logger.critical(f'Requested method {method} does not support grayscale normalized output')
raise ValueError('only method=ocropy allows grayscale=true')

def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: str = None) -> OcrdPageResult:
def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: Optional[str] = None) -> OcrdPageResult:
"""Binarize (and optionally deskew/despeckle) the pages/regions/lines of the workspace.
Iterate over the PAGE-XML element hierarchy down to the requested
Expand All @@ -91,16 +91,17 @@ def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: str = No
self.logger.debug(f'Level of operation: "{level}"')

pcgts = input_pcgts[0]
assert pcgts
page = pcgts.get_Page()
assert page

page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='binarized')
zoom = determine_zoom(self.logger, page_id, self.parameter['dpi'], page_image_info)

ret = OcrdPageResult(pcgts)
result = OcrdPageResult(pcgts)
if level == 'page':
try:
ret.images.append(self.process_page(page, page_image, page_xywh, zoom, page_id))
result.images.append(self.process_page(page, page_image, page_xywh, zoom, page_id))
except ValueError as e:
self.logger.error(e)
else:
Expand All @@ -115,7 +116,7 @@ def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: str = No
region, page_image, page_xywh, feature_filter='binarized')
if level == 'region':
try:
ret.images.append(self.process_region(region, region_image, region_xywh, zoom, region.id))
result.images.append(self.process_region(region, region_image, region_xywh, zoom, region.id))
continue
except ValueError as e:
self.logger.error(e)
Expand All @@ -126,10 +127,10 @@ def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: str = No
line_image, line_xywh = self.workspace.image_from_segment(
line, region_image, region_xywh, feature_filter='binarized')
try:
ret.images.append(self.process_line(line, line_image, line_xywh, zoom, page_id, region.id))
result.images.append(self.process_line(line, line_image, line_xywh, zoom, page_id, region.id))
except ValueError as e:
self.logger.error(e)
return ret
return result

def process_page(self, page, page_image, page_xywh, zoom, page_id) -> OcrdPageResultImage:
if not page_image.width or not page_image.height:
Expand Down Expand Up @@ -209,7 +210,7 @@ def process_region(self, region, region_image, region_xywh, zoom, page_id) -> Oc
orientation = -region_xywh['angle']
orientation = 180 - (180 - orientation) % 360 # map to [-179.999,180]
region.set_orientation(orientation)
suffix = region.id
suffix = f'{region.id}'
if self.parameter['grayscale']:
suffix += '.IMG-NRM'
features += ',grayscale_normalized'
Expand Down
3 changes: 2 additions & 1 deletion ocrd_cis/ocropy/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from typing import Optional

import warnings
import logging
Expand Down Expand Up @@ -2103,7 +2104,7 @@ def find_topological():
# DSAVE('rlabels_closed', rlabels)
return rlabels

def determine_zoom(logger: logging.Logger, page_id: str, dpi: float, page_image_info: OcrdExif) -> float:
def determine_zoom(logger: logging.Logger, page_id: Optional[str], dpi: float, page_image_info: OcrdExif) -> float:
if dpi > 0:
zoom = 300.0/dpi
elif page_image_info.resolution != 1:
Expand Down

0 comments on commit 6b19f35

Please sign in to comment.