Skip to content

Commit e76d708

Browse files
committed
update to OcrdPageResult change
1 parent e8ec7fe commit e76d708

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

ocrd_kraken/binarize.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import absolute_import
2-
import os
32
from os.path import join
43
from typing import Optional
54

6-
from ocrd_models.ocrd_process_result import OcrdProcessResult
5+
from ocrd.processor.base import OcrdPageResult
6+
from ocrd.processor.ocrd_page_result import OcrdPageResultImage
7+
78
import kraken.binarization
89
from ocrd import Processor
910
from ocrd_utils import assert_file_grp_cardinality, getLogger, make_file_id, MIMETYPE_PAGE
@@ -22,7 +23,7 @@ def executable(self):
2223
def setup(self):
2324
self.logger = getLogger('processor.KrakenBinarize')
2425

25-
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdProcessResult:
26+
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdPageResult:
2627
"""Binarize the pages/regions/lines with Kraken.
2728
2829
Iterate over the input PAGE element hierarchy down to the requested
@@ -50,14 +51,14 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
5051
assert page
5152
page_image, page_xywh, _ = self.workspace.image_from_page(
5253
page, page_id, feature_filter='binarized')
53-
images = []
54+
result = OcrdPageResult(pcgts)
5455
if self.parameter['level-of-operation'] == 'page':
5556
self.logger.info("Binarizing page '%s'", page_id)
5657
bin_image = kraken.binarization.nlbin(page_image)
5758
bin_image_id = f'{output_file_id}.IMG-BIN'
5859
bin_image_path = join(self.output_file_grp, f'{bin_image_id}.png')
5960
page.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments=f'{page_xywh["features"]},binarized'))
60-
images.append((bin_image, bin_image_id, bin_image_path))
61+
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
6162
else:
6263
for region in page.get_AllRegions(classes=['Text']):
6364
region_image, region_xywh = self.workspace.image_from_segment(
@@ -68,7 +69,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
6869
bin_image_id = f'{output_file_id}_{region.id}.IMG-BIN'
6970
bin_image_path = join(self.output_file_grp, f'{bin_image_id}.png')
7071
region.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments=f'{region_xywh["features"]},binarized'))
71-
images.append((bin_image, bin_image_id, bin_image_path))
72+
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
7273
else:
7374
for line in region.get_TextLine():
7475
line_image, line_xywh = self.workspace.image_from_segment(
@@ -78,5 +79,5 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
7879
bin_image_id = f'{output_file_id}_{region.id}_{line.id}.IMG-BIN'
7980
bin_image_path = join(self.output_file_grp, f'{bin_image_id}.png')
8081
line.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments=f'{page_xywh["features"]},binarized'))
81-
images.append((bin_image, bin_image_id, bin_image_path))
82-
return OcrdProcessResult(pcgts, images)
82+
result.images.append(OcrdPageResultImage(bin_image, bin_image_id, bin_image_path))
83+
return result

ocrd_kraken/recognize.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from os.path import join
21
from typing import Optional, Union
3-
from ocrd_models import OcrdProcessResult
2+
from ocrd.processor.base import OcrdPageResult
43
import regex
54
import itertools
65
import numpy as np
@@ -11,20 +10,15 @@
1110
from ocrd import Processor
1211
from ocrd_utils import (
1312
getLogger,
14-
make_file_id,
15-
assert_file_grp_cardinality,
1613
coordinates_of_segment,
1714
coordinates_for_segment,
1815
bbox_from_polygon,
1916
points_from_polygon,
2017
points_from_bbox,
2118
polygon_from_points,
2219
xywh_from_points,
23-
bbox_from_points,
2420
transform_coordinates,
25-
MIMETYPE_PAGE,
2621
)
27-
from ocrd_modelfactory import page_from_file
2822
from ocrd_models.ocrd_page import (
2923
OcrdPage,
3024
RegionRefType,
@@ -45,8 +39,6 @@
4539
TextLineOrderSimpleType
4640
)
4741

48-
from ocrd_kraken.config import OCRD_TOOL
49-
5042
class KrakenRecognize(Processor):
5143

5244
@property
@@ -76,7 +68,7 @@ def predict(page_image, segmentation):
7668
self.parameter['bidi_reordering'])
7769
self.predict = predict
7870

79-
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdProcessResult:
71+
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdPageResult:
8072
"""Recognize text on lines with Kraken.
8173
8274
Open the parsed PAGE-XML file, then iterate over the element hierarchy
@@ -222,7 +214,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
222214
page_update_higher_textequiv_levels('line', pcgts)
223215

224216
self.logger.info("Finished recognition, serializing")
225-
return OcrdProcessResult(pcgts)
217+
return OcrdPageResult(pcgts)
226218

227219
# zzz should go into core ocrd_utils
228220
def baseline_of_segment(segment, coords):

ocrd_kraken/segment.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
from typing import Optional
22
from PIL import ImageOps
3-
from os.path import join
43

54
from ocrd import Processor
6-
from ocrd_models import OcrdProcessResult
5+
from ocrd.processor.ocrd_page_result import OcrdPageResult
76
from ocrd_utils import (
87
getLogger,
9-
assert_file_grp_cardinality,
10-
make_file_id,
11-
concat_padded,
128
polygon_from_x0y0x1y1,
139
points_from_polygon,
1410
polygon_mask,
1511
coordinates_for_segment,
1612
coordinates_of_segment,
17-
MIMETYPE_PAGE
1813
)
1914
import ocrd_models.ocrd_page
2015
from ocrd_models.ocrd_page import (
@@ -25,16 +20,12 @@
2520
TextLineType,
2621
CoordsType,
2722
BaselineType,
28-
to_xml
2923
)
30-
from ocrd_modelfactory import page_from_file
3124

3225
import shapely.geometry as geom
3326
from shapely.prepared import prep as geom_prep
3427
import torch
3528

36-
from .config import OCRD_TOOL
37-
3829
class KrakenSegment(Processor):
3930

4031
@property
@@ -71,7 +62,7 @@ def segmenter(img, mask=None):
7162
return segment(img, mask=mask, **kwargs)
7263
self.segmenter = segmenter
7364

74-
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdProcessResult:
65+
def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[str] = None, page_id: Optional[str] = None) -> OcrdPageResult:
7566
"""Segment into (regions and) lines with Kraken.
7667
7768
Iterate over the element hierarchy of the PAGE-XML down to the
@@ -142,7 +133,7 @@ def process_page_pcgts(self, *input_pcgts: OcrdPage, output_file_id: Optional[st
142133
self.logger.warning('Keeping %d lines in region "%s"', len(region.TextLine or []), region.id)
143134
self._process_region(page_image, page_coords, region, zoom)
144135

145-
return OcrdProcessResult(pcgts)
136+
return OcrdPageResult(pcgts)
146137

147138
def _process_page(self, page_image, page_coords, page, zoom=1.0):
148139
def getmask():

0 commit comments

Comments
 (0)