Skip to content

Commit

Permalink
update to OcrdPageResult change
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Aug 15, 2024
1 parent e8ec7fe commit e76d708
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
17 changes: 9 additions & 8 deletions ocrd_kraken/binarize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import absolute_import
import os
from os.path import join
from typing import Optional

from ocrd_models.ocrd_process_result import OcrdProcessResult
from ocrd.processor.base import OcrdPageResult
from ocrd.processor.ocrd_page_result import OcrdPageResultImage

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 +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) -> OcrdProcessResult:
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, 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 Down Expand Up @@ -50,14 +51,14 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
assert page
page_image, page_xywh, _ = self.workspace.image_from_page(
page, page_id, feature_filter='binarized')
images = []
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'))
images.append((bin_image, bin_image_id, bin_image_path))
result.images.append(OcrdPageResultImage(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 +69,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
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'))
images.append((bin_image, bin_image_id, bin_image_path))
result.images.append(OcrdPageResultImage(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 +79,5 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
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'))
images.append((bin_image, bin_image_id, bin_image_path))
return OcrdProcessResult(pcgts, images)
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
return result
14 changes: 3 additions & 11 deletions ocrd_kraken/recognize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from os.path import join
from typing import Optional, Union
from ocrd_models import OcrdProcessResult
from ocrd.processor.base import OcrdPageResult
import regex
import itertools
import numpy as np
Expand All @@ -11,20 +10,15 @@
from ocrd import Processor
from ocrd_utils import (
getLogger,
make_file_id,
assert_file_grp_cardinality,
coordinates_of_segment,
coordinates_for_segment,
bbox_from_polygon,
points_from_polygon,
points_from_bbox,
polygon_from_points,
xywh_from_points,
bbox_from_points,
transform_coordinates,
MIMETYPE_PAGE,
)
from ocrd_modelfactory import page_from_file
from ocrd_models.ocrd_page import (
OcrdPage,
RegionRefType,
Expand All @@ -45,8 +39,6 @@
TextLineOrderSimpleType
)

from ocrd_kraken.config import OCRD_TOOL

class KrakenRecognize(Processor):

@property
Expand Down Expand Up @@ -76,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) -> OcrdProcessResult:
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, 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 Down Expand Up @@ -222,7 +214,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
page_update_higher_textequiv_levels('line', pcgts)

self.logger.info("Finished recognition, serializing")
return OcrdProcessResult(pcgts)
return OcrdPageResult(pcgts)

# zzz should go into core ocrd_utils
def baseline_of_segment(segment, coords):
Expand Down
15 changes: 3 additions & 12 deletions ocrd_kraken/segment.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
from typing import Optional
from PIL import ImageOps
from os.path import join

from ocrd import Processor
from ocrd_models import OcrdProcessResult
from ocrd.processor.ocrd_page_result import OcrdPageResult
from ocrd_utils import (
getLogger,
assert_file_grp_cardinality,
make_file_id,
concat_padded,
polygon_from_x0y0x1y1,
points_from_polygon,
polygon_mask,
coordinates_for_segment,
coordinates_of_segment,
MIMETYPE_PAGE
)
import ocrd_models.ocrd_page
from ocrd_models.ocrd_page import (
Expand All @@ -25,16 +20,12 @@
TextLineType,
CoordsType,
BaselineType,
to_xml
)
from ocrd_modelfactory import page_from_file

import shapely.geometry as geom
from shapely.prepared import prep as geom_prep
import torch

from .config import OCRD_TOOL

class KrakenSegment(Processor):

@property
Expand Down Expand Up @@ -71,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) -> OcrdProcessResult:
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, 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 Down Expand Up @@ -142,7 +133,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
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 OcrdProcessResult(pcgts)
return OcrdPageResult(pcgts)

def _process_page(self, page_image, page_coords, page, zoom=1.0):
def getmask():
Expand Down

0 comments on commit e76d708

Please sign in to comment.