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 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 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
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
5 changes: 3 additions & 2 deletions qurator/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 getLogger, initLogging, setOverrideLogLevel
from qurator.eynollah.eynollah import Eynollah


Expand Down Expand Up @@ -176,10 +176,11 @@ def main(
print('Error: You used -tll to enable light textline detection but -light is not enabled')
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,
dir_of_layout=save_layout,
dir_of_deskewed=save_deskewed,
Expand Down
58 changes: 31 additions & 27 deletions qurator/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,32 +146,32 @@ 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,
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,
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 @@ -213,7 +217,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
51 changes: 38 additions & 13 deletions qurator/eynollah/ocrd-tool.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 format)",
"url": "https://github.com/qurator-spk/eynollah/releases/download/v0.3.0/models_eynollah.tar.gz",
"name": "default",
"size": 1761991295,
"type": "archive",
"path_in_archive": "models_eynollah"
}
{
"description": "models for eynollah (TensorFlow format)",
"url": "https://github.com/qurator-spk/eynollah/releases/download/v0.3.0/models_eynollah.tar.gz",
"name": "default",
"size": 1761991295,
"type": "archive",
"path_in_archive": "models_eynollah"
}
]
}
}
Expand Down
115 changes: 54 additions & 61 deletions qurator/eynollah/processor.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,61 @@
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 typing import Optional
from ocrd.processor.ocrd_page_result import OcrdPageResult
kba marked this conversation as resolved.
Show resolved Hide resolved
from ocrd_models import OcrdPage
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 .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)
@property
def metadata_filename(self) -> str:
return 'eynollah/ocrd-tool.json'

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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

This whole effort was to ensure we can pass a working local filename, as (was) needed by Eynollah. The approach by OCR-D is Workspace.image_from_page / Workspace.image_from_segment which will search for the right original or derived image, download it if necessary and load it into memory.

I don't recall what the new behaviour of Eynollah is. If both an image filename and an image object are passed, who wins?

Assuming it's the memory object: this can be removed. (But then I wonder why we still pass the image filename at all...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, currently we have

            if image_pil:
                self._imgs = self._cache_images(image_pil=image_pil)
            else:
                self._imgs = self._cache_images(image_filename=image_filename)
[...]
     def _cache_images(self, image_filename=None, image_pil=None):
         ret = {}
         if image_filename:
             ret['img'] = cv2.imread(image_filename)
             self.dpi = check_dpi(image_filename)
         else:
             ret['img'] = pil2cv(image_pil)
             self.dpi = check_dpi(image_pil)

image_filename is (should) then only used passively, to generate filenames of plotted debug images as well as for PAGE serialization.

So I think image_pil should win but for now we need both. But as I said above, one of those things I would love to untangle in the refactoring.

# XXX loses DPI information
kba marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: that filename might not be where that image came from in workspace.image_from_page. It could well be a derived image generated by some previous processor (just not a cropped, deskewed or binarized image, because that would have changed its coordinate system).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still a bit hazy for me when image_filename is actually used. Ideally, image_pil should take preference and image_filename is only for the plotter/writer, at least in the "single image mode" we're using.

One of the aspects I hope I'll be able to improve a bit with https://github.com/qurator-spk/eynollah/tree/refactoring-2024-08/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can also re-use session across Eynollah invokations in addition to models?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, yes, but with standalone eynollah being focused on batch processing now, I am honestly not sure how/where sessions are defined for the non-dir_in option - @vahidrezanezhad can you tell us?

)
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)
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.0a2
numpy <1.24.0
scikit-learn >= 0.23.2
tensorflow == 2.12.1
Expand Down