mirror of
https://github.com/qurator-spk/sbb_textline_detection.git
synced 2025-07-06 17:10:02 +02:00
✨ sbb_textline_detector: Add a OCR-D interface
This commit is contained in:
parent
561a6f8a90
commit
0c915c75de
7 changed files with 98 additions and 11 deletions
1
ocrd-tool.json
Symbolic link
1
ocrd-tool.json
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
qurator/sbb_textline_detector/ocrd-tool.json
|
|
@ -1 +1,2 @@
|
||||||
from .main import *
|
from .main import *
|
||||||
|
from .ocrd_cli import *
|
||||||
|
|
|
@ -35,16 +35,18 @@ __doc__ = \
|
||||||
|
|
||||||
|
|
||||||
class textlineerkenner:
|
class textlineerkenner:
|
||||||
def __init__(self, image_dir, dir_out, dir_models):
|
def __init__(self, image_dir, dir_out, f_name, dir_models):
|
||||||
self.image_dir = image_dir
|
self.image_dir = image_dir
|
||||||
self.dir_out = dir_out
|
self.dir_out = dir_out
|
||||||
self.dir_models = dir_models
|
self.f_name = f_name
|
||||||
|
if self.f_name is None:
|
||||||
try:
|
try:
|
||||||
self.f_name = image_dir.split('/')[len(image_dir.split('/')) - 1]
|
self.f_name = image_dir.split('/')[len(image_dir.split('/')) - 1]
|
||||||
self.f_name = self.f_name.split('.')[0]
|
self.f_name = self.f_name.split('.')[0]
|
||||||
print(self.f_name)
|
print(self.f_name)
|
||||||
except:
|
except:
|
||||||
self.f_name = self.f_name.split('.')[0]
|
self.f_name = self.f_name.split('.')[0]
|
||||||
|
self.dir_models = dir_models
|
||||||
self.kernel = np.ones((5, 5), np.uint8)
|
self.kernel = np.ones((5, 5), np.uint8)
|
||||||
self.model_page_dir = dir_models + '/model_page.h5'
|
self.model_page_dir = dir_models + '/model_page.h5'
|
||||||
self.model_region_dir = dir_models + '/model_strukturerkennung.h5'
|
self.model_region_dir = dir_models + '/model_strukturerkennung.h5'
|
||||||
|
@ -365,6 +367,7 @@ class textlineerkenner:
|
||||||
return img_r
|
return img_r
|
||||||
|
|
||||||
def get_image_and_scales(self):
|
def get_image_and_scales(self):
|
||||||
|
print(self.image_dir)
|
||||||
self.image = cv2.imread(self.image_dir)
|
self.image = cv2.imread(self.image_dir)
|
||||||
self.height_org = self.image.shape[0]
|
self.height_org = self.image.shape[0]
|
||||||
self.width_org = self.image.shape[1]
|
self.width_org = self.image.shape[1]
|
||||||
|
@ -1298,7 +1301,7 @@ class textlineerkenner:
|
||||||
unireg.text = ' '
|
unireg.text = ' '
|
||||||
|
|
||||||
tree = ET.ElementTree(data)
|
tree = ET.ElementTree(data)
|
||||||
tree.write(dir_of_image + self.f_name + ".xml")
|
tree.write(os.path.join(dir_of_image, self.f_name) + ".xml")
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.get_image_and_scales()
|
self.get_image_and_scales()
|
||||||
|
@ -1321,7 +1324,7 @@ class textlineerkenner:
|
||||||
def main(image, out, model):
|
def main(image, out, model):
|
||||||
possibles = globals() # XXX unused?
|
possibles = globals() # XXX unused?
|
||||||
possibles.update(locals())
|
possibles.update(locals())
|
||||||
x = textlineerkenner(image, out, model)
|
x = textlineerkenner(image, out, None, model)
|
||||||
x.run()
|
x.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
19
qurator/sbb_textline_detector/ocrd-tool.json
Normal file
19
qurator/sbb_textline_detector/ocrd-tool.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"version": "0.0.1",
|
||||||
|
"tools": {
|
||||||
|
"ocrd_sbb_textline_detector": {
|
||||||
|
"executable": "ocrd_sbb_textline_detector",
|
||||||
|
"description": "Detect lines",
|
||||||
|
"steps": ["layout/segmentation/line"],
|
||||||
|
"input_file_grp": [
|
||||||
|
"OCR-D-IMG"
|
||||||
|
],
|
||||||
|
"output_file_grp": [
|
||||||
|
"OCR-D-SBB-SEG-LINE"
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"model": {"type": "string", "format": "file", "cacheable": true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
61
qurator/sbb_textline_detector/ocrd_cli.py
Normal file
61
qurator/sbb_textline_detector/ocrd_cli.py
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import click
|
||||||
|
from ocrd import Processor
|
||||||
|
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
|
||||||
|
from ocrd_utils import concat_padded, getLogger
|
||||||
|
from pkg_resources import resource_string
|
||||||
|
|
||||||
|
from qurator.sbb_textline_detector import textlineerkenner
|
||||||
|
|
||||||
|
log = getLogger('processor.OcrdSbbTextlineDetectorRecognize')
|
||||||
|
|
||||||
|
OCRD_TOOL = json.loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
|
||||||
|
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@ocrd_cli_options
|
||||||
|
def ocrd_sbb_textline_detector(*args, **kwargs):
|
||||||
|
return ocrd_cli_wrap_processor(OcrdSbbTextlineDetectorRecognize, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class OcrdSbbTextlineDetectorRecognize(Processor):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
kwargs['ocrd_tool'] = OCRD_TOOL['tools']['ocrd_sbb_textline_detector']
|
||||||
|
super(OcrdSbbTextlineDetectorRecognize, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def _make_file_id(self, input_file, input_file_grp, n):
|
||||||
|
file_id = input_file.ID.replace(input_file_grp, self.output_file_grp)
|
||||||
|
if file_id == input_file.ID:
|
||||||
|
file_id = concat_padded(self.output_file_grp, n)
|
||||||
|
return file_id
|
||||||
|
|
||||||
|
def process(self):
|
||||||
|
for n, page_id in enumerate(self.workspace.mets.physical_pages):
|
||||||
|
image_file = self.workspace.mets.find_files(fileGrp=self.input_file_grp, pageId=page_id)[0]
|
||||||
|
log.info("INPUT FILE %i / %s", n, image_file)
|
||||||
|
|
||||||
|
file_id = self._make_file_id(image_file, self.output_file_grp, n)
|
||||||
|
|
||||||
|
# Process the files
|
||||||
|
try:
|
||||||
|
os.mkdir(self.output_file_grp)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
model = self.parameter['model']
|
||||||
|
x = textlineerkenner(image_file.local_filename, self.output_file_grp, file_id, model)
|
||||||
|
x.run()
|
||||||
|
|
||||||
|
self.workspace.add_file(
|
||||||
|
ID=file_id + '.xml',
|
||||||
|
file_grp=self.output_file_grp,
|
||||||
|
pageId=page_id,
|
||||||
|
mimetype='application/vnd.prima.page+xml',
|
||||||
|
local_filename=self.output_file_grp + '/' + file_id)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ocrd_sbb_textline_detector()
|
|
@ -1,5 +1,5 @@
|
||||||
opencv-python
|
opencv-python
|
||||||
numpy<=1.14.5
|
numpy
|
||||||
matplotlib
|
matplotlib
|
||||||
seaborn
|
seaborn
|
||||||
tqdm
|
tqdm
|
||||||
|
@ -9,3 +9,4 @@ scikit-learn
|
||||||
tensorflow-gpu < 2.0
|
tensorflow-gpu < 2.0
|
||||||
scipy
|
scipy
|
||||||
click
|
click
|
||||||
|
ocrd >= 1.0.0b19
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -21,6 +21,7 @@ setup(
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
"sbb_textline_detector=qurator.sbb_textline_detector:main",
|
"sbb_textline_detector=qurator.sbb_textline_detector:main",
|
||||||
|
"ocrd_sbb_textline_detector=qurator.sbb_textline_detector:ocrd_sbb_textline_detector",
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
python_requires='>=3.6.0',
|
python_requires='>=3.6.0',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue