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

port processor to core v3 #130

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0a3f525
port processor to core v3
kba Aug 23, 2024
4a13781
class Eynollah: add typing, consistent interface in CLI and OCR-D CLI
kba Aug 23, 2024
9ce02a5
ocrd-tool: add "allow_enhancement" parameter
kba Aug 23, 2024
0d83db7
update processor to the latest change in bertsky/core#14
kba Aug 24, 2024
87adc4b
ocrd interface: add light_mode parameter
kba Aug 24, 2024
39b16e5
ocrd interface: add textline_light
kba Aug 24, 2024
ddcc019
ocrd interface: add right_to_left
kba Aug 24, 2024
d7caeb2
ocrd interface: add ignore_page_extraction
kba Aug 24, 2024
8dfecb7
adapt to ocrd>=2.54 url vs local_filename
bertsky Jan 19, 2024
3381e5a
adapt to OcrdFile.local_filename now :Path
bertsky Jan 24, 2024
49c1a8f
fix namespace pkg setup
bertsky May 24, 2024
c37d95d
non-legacy namespace package
bertsky May 23, 2024
61bcb43
processor: reuse loaded models across pages, use derived images
bertsky Jun 11, 2023
d98fa2a
check_dpi: fix Pillow type detection
bertsky May 28, 2024
ecd202e
processor.py: Simplify import
kba Aug 26, 2024
d26079d
procesor.py: simplify imports further
kba Aug 26, 2024
7b92620
processor: no more DPI info lost
kba Aug 26, 2024
aef46a4
require ocrd >= 3.0.0b1
kba Aug 26, 2024
dfc4ac2
setuptools: fix (packages.find.where prevented finding namespace qura…
bertsky Aug 30, 2024
1e90257
undo customizing metadata_filename (not correct with namespace pkg su…
bertsky Sep 1, 2024
17eafc1
adapt tool json to v3
bertsky Sep 1, 2024
9b274dc
Merge pull request #134 from bertsky/v3-api
kba Sep 2, 2024
f9c2d85
Merge branch 'main' into v3-api
kba Sep 2, 2024
fdedae2
require ocrd>=3.0.0b4
kba Sep 2, 2024
c6e0e05
Merge branch 'main' into v3-api
kba Sep 2, 2024
869110f
merge main
kba Jan 20, 2025
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,5 +1,5 @@
# ocrd includes opencv, numpy, shapely, click
ocrd >= 2.23.3
ocrd >= 3.0.0b4
numpy <1.24.0
scikit-learn >= 0.23.2
tensorflow < 2.13
Expand Down
5 changes: 3 additions & 2 deletions src/eynollah/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import click
from ocrd_utils import initLogging, setOverrideLogLevel
from ocrd_utils import initLogging, setOverrideLogLevel, getLogger
from eynollah.eynollah import Eynollah


Expand Down Expand Up @@ -186,10 +186,11 @@ def main(
print('Error: You used -eoi which can not be enabled alongside light_version -light or allow_scaling -as or allow_enhancement -ae or curved_line -cl or textline_light -tll or full_layout -fl or tables -tab or right2left -r2l or headers_off -ho')
sys.exit(1)
eynollah = Eynollah(
model,
getLogger('Eynollah'),
image_filename=image,
dir_out=out,
dir_in=dir_in,
dir_models=model,
dir_of_cropped_images=save_images,
extract_only_images=extract_only_images,
dir_of_layout=save_layout,
Expand Down
60 changes: 32 additions & 28 deletions src/eynollah/eynollah.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
document layout analysis (segmentation) with output in PAGE-XML
"""

from logging import Logger
import math
import os
import sys
import time
from typing import Optional
import warnings
from pathlib import Path
from multiprocessing import Process, Queue, cpu_count
import gc
from PIL.Image import Image
from ocrd import OcrdPage
from ocrd_utils import getLogger
import cv2
import numpy as np
Expand Down Expand Up @@ -142,33 +146,33 @@ def get_config(self):
class Eynollah:
def __init__(
self,
dir_models,
image_filename=None,
image_pil=None,
image_filename_stem=None,
dir_out=None,
dir_in=None,
dir_of_cropped_images=None,
extract_only_images=False,
dir_of_layout=None,
dir_of_deskewed=None,
dir_of_all=None,
dir_save_page=None,
enable_plotting=False,
allow_enhancement=False,
curved_line=False,
textline_light=False,
full_layout=False,
tables=False,
right2left=False,
input_binary=False,
allow_scaling=False,
headers_off=False,
light_version=False,
ignore_page_extraction=False,
override_dpi=None,
logger=None,
pcgts=None,
dir_models : str,
logger : Logger,
image_filename : Optional[str] = None,
image_pil : Optional[Image] = None,
image_filename_stem : Optional[str] = None,
dir_out : Optional[str] = None,
dir_in : Optional[str] = None,
dir_of_cropped_images : Optional[str] = None,
extract_only_images : bool =False,
dir_of_layout : Optional[str] = None,
dir_of_deskewed : Optional[str] = None,
dir_of_all : Optional[str] = None,
dir_save_page : Optional[str] = None,
enable_plotting : bool = False,
allow_enhancement : bool = False,
curved_line : bool = False,
textline_light : bool = False,
full_layout : bool = False,
tables : bool = False,
right2left : bool = False,
input_binary : bool = False,
allow_scaling : bool = False,
headers_off : bool = False,
light_version : bool = False,
ignore_page_extraction : bool = False,
override_dpi : Optional[int] = None,
pcgts : Optional[OcrdPage] = None,
):
if not dir_in:
if image_pil:
Expand Down Expand Up @@ -215,7 +219,7 @@ def __init__(
curved_line=self.curved_line,
textline_light = self.textline_light,
pcgts=pcgts)
self.logger = logger if logger else getLogger('eynollah')
self.logger = logger
self.dir_models = dir_models

self.model_dir_of_enhancement = dir_models + "/eynollah-enhancement_20210425"
Expand Down
55 changes: 40 additions & 15 deletions src/eynollah/ocrd-tool.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"executable": "ocrd-eynollah-segment",
"categories": ["Layout analysis"],
"description": "Segment page into regions and lines and do reading order detection with eynollah",
"input_file_grp": ["OCR-D-IMG", "OCR-D-SEG-PAGE", "OCR-D-GT-SEG-PAGE"],
"output_file_grp": ["OCR-D-SEG-LINE"],
"input_file_grp_cardinality": 1,
"output_file_grp_cardinality": 1,
"steps": ["layout/segmentation/region", "layout/segmentation/line"],
"parameters": {
"models": {
Expand All @@ -29,36 +29,61 @@
"default": true,
"description": "Try to detect all element subtypes, including drop-caps and headings"
},
"tables": {
"type": "boolean",
"default": false,
"description": "Try to detect table regions"
},
"tables": {
"type": "boolean",
"default": false,
"description": "Try to detect table regions"
},
"curved_line": {
"type": "boolean",
"default": false,
"description": "try to return contour of textlines instead of just rectangle bounding box. Needs more processing time"
},
"ignore_page_extraction": {
"type": "boolean",
"default": false,
"description": "if this parameter set to true, this tool would ignore page extraction"
},
"allow_scaling": {
"type": "boolean",
"default": false,
"description": "check the resolution against the number of detected columns and if needed, scale the image up or down during layout detection (heuristic to improve quality and performance)"
},
"allow_enhancement": {
"type": "boolean",
"default": false,
"description": "if this parameter set to true, this tool would check that input image need resizing and enhancement or not."
},
"light_mode": {
"type": "boolean",
"default": false,
"description": "lighter and faster but simpler method for main region detection and deskewing"
},
"textline_light": {
"type": "boolean",
"default": false,
"description": "if this parameter set to true, this tool will try to return contoure of textlines instead of rectangle bounding box of textline with a faster method."
},
"right_to_left": {
"type": "boolean",
"default": false,
"description": "if this parameter set to true, this tool will extract right-to-left reading order."
},
"headers_off": {
"type": "boolean",
"default": false,
"description": "ignore the special role of headings during reading order detection"
}
},
"resources": [
{
"description": "models for eynollah (TensorFlow SavedModel format)",
"url": "https://github.com/qurator-spk/eynollah/releases/download/v0.3.1/models_eynollah.tar.gz",
"name": "default",
"size": 1894627041,
"type": "archive",
"path_in_archive": "models_eynollah"
}
{
"description": "models for eynollah (TensorFlow SavedModel format)",
"url": "https://github.com/qurator-spk/eynollah/releases/download/v0.3.1/models_eynollah.tar.gz",
"name": "default",
"size": 1894627041,
"type": "archive",
"path_in_archive": "models_eynollah"
}
]
}
}
Expand Down
111 changes: 49 additions & 62 deletions src/eynollah/processor.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,55 @@
from json import loads
from pkg_resources import resource_string
from tempfile import NamedTemporaryFile
from pathlib import Path
from os.path import join

from PIL import Image

from ocrd import Processor
from ocrd_modelfactory import page_from_file, exif_from_filename
from ocrd_models import OcrdFile, OcrdExif
from ocrd_models.ocrd_page import to_xml
from ocrd_utils import (
getLogger,
MIMETYPE_PAGE,
assert_file_grp_cardinality,
make_file_id
)
from typing import Optional
from ocrd_models import OcrdPage
from ocrd import Processor, OcrdPageResult

from .eynollah import Eynollah
from .utils.pil_cv2 import pil2cv

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

class EynollahProcessor(Processor):

def __init__(self, *args, **kwargs):
kwargs['ocrd_tool'] = OCRD_TOOL['tools']['ocrd-eynollah-segment']
kwargs['version'] = OCRD_TOOL['version']
super().__init__(*args, **kwargs)
def setup(self) -> None:
# for caching models
self.models = None
if self.parameter['textline_light'] and not self.parameter['light_mode']:
raise ValueError("Error: You set parameter 'textline_light' to enable light textline detection but parameter 'light_mode' is not enabled")

def process(self):
LOG = getLogger('eynollah')
assert_file_grp_cardinality(self.input_file_grp, 1)
assert_file_grp_cardinality(self.output_file_grp, 1)
for n, input_file in enumerate(self.input_files):
page_id = input_file.pageId or input_file.ID
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))
LOG.debug('width %s height %s', pcgts.get_Page().imageWidth, pcgts.get_Page().imageHeight)
self.add_metadata(pcgts)
page = pcgts.get_Page()
# XXX loses DPI information
# page_image, _, _ = self.workspace.image_from_page(page, page_id, feature_filter='binarized')
image_filename = self.workspace.download_file(next(self.workspace.mets.find_files(local_filename=page.imageFilename))).local_filename
eynollah_kwargs = {
'dir_models': self.resolve_resource(self.parameter['models']),
'allow_enhancement': False,
'curved_line': self.parameter['curved_line'],
'full_layout': self.parameter['full_layout'],
'allow_scaling': self.parameter['allow_scaling'],
'headers_off': self.parameter['headers_off'],
'tables': self.parameter['tables'],
'override_dpi': self.parameter['dpi'],
'logger': LOG,
'pcgts': pcgts,
'image_filename': image_filename
}
Eynollah(**eynollah_kwargs).run()
file_id = make_file_id(input_file, self.output_file_grp)
pcgts.set_pcGtsId(file_id)
self.workspace.add_file(
ID=file_id,
file_grp=self.output_file_grp,
pageId=page_id,
mimetype=MIMETYPE_PAGE,
local_filename=join(self.output_file_grp, file_id) + '.xml',
content=to_xml(pcgts))
def process_page_pcgts(self, *input_pcgts: Optional[OcrdPage], page_id: Optional[str] = None) -> OcrdPageResult:
assert input_pcgts
assert input_pcgts[0]
assert self.parameter
pcgts = input_pcgts[0]
page = pcgts.get_Page()
# if not('://' in page.imageFilename):
# image_filename = next(self.workspace.mets.find_files(local_filename=page.imageFilename)).local_filename
# else:
# # could be a URL with file:// or truly remote
# image_filename = self.workspace.download_file(next(self.workspace.mets.find_files(url=page.imageFilename))).local_filename
page_image, _, _ = self.workspace.image_from_page(
page, page_id,
# avoid any features that would change the coordinate system: cropped,deskewed
# (the PAGE builder merely adds regions, so afterwards we would not know which to transform)
# also avoid binarization as models usually fare better on grayscale/RGB
feature_filter='cropped,deskewed,binarized')
eynollah = Eynollah(
self.resolve_resource(self.parameter['models']),
self.logger,
allow_enhancement=self.parameter['allow_enhancement'],
curved_line=self.parameter['curved_line'],
light_version=self.parameter['light_mode'],
right2left=self.parameter['right_to_left'],
ignore_page_extraction=self.parameter['ignore_page_extraction'],
textline_light=self.parameter['textline_light'],
full_layout=self.parameter['full_layout'],
allow_scaling=self.parameter['allow_scaling'],
headers_off=self.parameter['headers_off'],
tables=self.parameter['tables'],
override_dpi=self.parameter['dpi'],
pcgts=pcgts,
image_filename=page.imageFilename,
image_pil=page_image
)
if self.models is not None:
# reuse loaded models from previous page
eynollah.models = self.models
eynollah.run()
self.models = eynollah.models
return OcrdPageResult(pcgts)
Loading