Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core v3 ocrd api #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ocrd >= 2.23.2
ocrd >= 3.0.0b5
lxml
langcodes >= 3.4.0 ; python_version>"3.7"
langcodes[data] >= 3.1.0 ; python_version<="3.7"
Expand Down
8 changes: 4 additions & 4 deletions src/ocrd_page_to_alto/ocrd-tool.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"version": "1.4.1",
"version": "2.0.0",
"git_url": "https://github.com/kba/page-to-alto",
"tools": {
"ocrd-page2alto-transform": {
"executable": "ocrd-page2alto-transform",
"categories": ["Layout analysis"],
"description": "Transform PAGE-XML to ALTO",
"input_file_grp": ["OBSOLETE"],
"output_file_grp": ["ALSO-OBSOLETE"],
"steps": ["whatevs"],
"input_file_grp_cardinality": 1,
"output_file_grp_cardinality": 1,
"steps": ["format conversion"],
"parameters": {
"check_border": {
"type": "boolean",
Expand Down
94 changes: 38 additions & 56 deletions src/ocrd_page_to_alto/ocrd_processor.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,51 @@
from json import loads
from ocrd_models.ocrd_page import PageType, PcGtsType
from pkg_resources import resource_string
from functools import cached_property
from typing import Optional
from os.path import join

from ocrd import Processor
from ocrd_modelfactory import page_from_file
from ocrd_models.ocrd_file import OcrdFileType
from ocrd_utils import (
getLogger,
assert_file_grp_cardinality,
make_file_id
make_file_id,
MIMETYPE_PAGE,
)

from .convert import OcrdPageAltoConverter

OCRD_TOOL = loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))

# @dataclass()
# class OcrdPageResult():
# pcgts : OcrdPage
# images : List = field(default_factory=list)

class Page2AltoProcessor(Processor):

def __init__(self, *args, **kwargs):
kwargs['ocrd_tool'] = OCRD_TOOL['tools']['ocrd-page2alto-transform']
kwargs['version'] = OCRD_TOOL['version']
super().__init__(*args, **kwargs)
self.log = getLogger('ocrd.processor.page2alto')

@cached_property
def executable(self):
return 'ocrd-page2alto-transform'

def process(self):
assert_file_grp_cardinality(self.input_file_grp, 1)
assert_file_grp_cardinality(self.output_file_grp, 1)
def process_page_file(self, *input_files: Optional[OcrdFileType]) -> None:
input_file = input_files[0]
assert input_file
assert input_file.local_filename
assert isinstance(self.parameter, dict)
for n, input_file in enumerate(self.input_files):
page_id = input_file.pageId or input_file.ID
self.log.info("INPUT FILE %s (%d/%d) ", page_id, n + 1, len(self.input_files))
pcgts = page_from_file(self.workspace.download_file(input_file))
assert isinstance(pcgts, PcGtsType)
self.log.debug('width %s height %s', pcgts.get_Page().imageWidth, pcgts.get_Page().imageHeight)
self.add_metadata(pcgts)
page = pcgts.get_Page()
converter = OcrdPageAltoConverter(
page_filename=input_file.local_filename,
alto_version=self.parameter["alto_version"].replace('v', ''),
check_words=self.parameter["check_words"],
timestamp_src=self.parameter["timestamp_src"],
check_border=self.parameter["check_border"],
skip_empty_lines=self.parameter["skip_empty_lines"],
trailing_dash_to_hyp=self.parameter["trailing_dash_to_hyp"],
dummy_textline=self.parameter["dummy_textline"],
dummy_word=self.parameter["dummy_word"],
textequiv_index=self.parameter["textequiv_index"],
textequiv_fallback_strategy=self.parameter["textequiv_fallback_strategy"],
region_order=self.parameter["region_order"],
textline_order=self.parameter["textline_order"],
)
converter.convert()
file_id = make_file_id(input_file, self.output_file_grp)
pcgts.set_pcGtsId(file_id)
self.add_metadata(pcgts)
self.workspace.add_file(
ID=file_id,
file_grp=self.output_file_grp,
pageId=page_id,
mimetype='application/alto+xml',
local_filename=join(self.output_file_grp, file_id) + '.xml',
content=str(converter))
assert input_file.mimetype == MIMETYPE_PAGE
self.logger.debug("converting file %s", input_file.local_filename)
converter = OcrdPageAltoConverter(
page_filename=input_file.local_filename,
alto_version=self.parameter["alto_version"].replace('v', ''),
check_words=self.parameter["check_words"],
timestamp_src=self.parameter["timestamp_src"],
check_border=self.parameter["check_border"],
skip_empty_lines=self.parameter["skip_empty_lines"],
trailing_dash_to_hyp=self.parameter["trailing_dash_to_hyp"],
dummy_textline=self.parameter["dummy_textline"],
dummy_word=self.parameter["dummy_word"],
textequiv_index=self.parameter["textequiv_index"],
textequiv_fallback_strategy=self.parameter["textequiv_fallback_strategy"],
region_order=self.parameter["region_order"],
textline_order=self.parameter["textline_order"],
)
converter.convert()
file_id = make_file_id(input_file, self.output_file_grp)
self.workspace.add_file(
file_id=file_id,
file_grp=self.output_file_grp,
pageId=input_file.pageId,
mimetype='application/alto+xml',
local_filename=join(self.output_file_grp, file_id) + '.xml',
content=str(converter))