Skip to content

Commit

Permalink
Add the basic project layout and minimal functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wrznr committed Aug 6, 2019
1 parent 072d1d8 commit 5f02107
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright © 2019 Kay-Michael Würzner

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1 change: 1 addition & 0 deletions ocrd-tool.json
1 change: 1 addition & 0 deletions ocrd_evaluate_segmentation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .evaluate import EvaluateSegmentation
9 changes: 9 additions & 0 deletions ocrd_evaluate_segmentation/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click

from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor
from ocrd_evaluate_segmentation.evaluate import EvaluateSegmentation

@click.command()
@ocrd_cli_options
def ocrd_evaluate_segmentation(*args, **kwargs):
return ocrd_cli_wrap_processor(EvaluateSegmentation, *args, **kwargs)
5 changes: 5 additions & 0 deletions ocrd_evaluate_segmentation/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
import json
from pkg_resources import resource_string

OCRD_TOOL = json.loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
89 changes: 89 additions & 0 deletions ocrd_evaluate_segmentation/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from __future__ import absolute_import

import os.path

from ocrd import Processor
from ocrd_utils import (
getLogger, concat_padded,
polygon_from_points,
MIMETYPE_PAGE
)
from ocrd_modelfactory import page_from_file
from ocrd_models.ocrd_page import (
CoordsType,
LabelType, LabelsType,
MetadataItemType,
TextLineType,
to_xml
)

from .config import OCRD_TOOL

from shapely.geometry import Polygon

TOOL = 'ocrd-evaluate-segmentation'
LOG = getLogger('processor.EvaluateSegmentation')

class EvaluateSegmentation(Processor):

def __init__(self, *args, **kwargs):
kwargs['ocrd_tool'] = OCRD_TOOL['tools'][TOOL]
kwargs['version'] = OCRD_TOOL['version']
super(EvaluateSegmentation, self).__init__(*args, **kwargs)


def process(self):
"""Performs segmentation evaluation with Shapely on the workspace.
Open and deserialize PAGE input files and their respective images,
then iterate over the element hierarchy down to the region level.
Return information on the plausibility of the segmentation into
regions on the logging level.
"""

for (n, input_file) in enumerate(self.input_files):
page_id = input_file.pageId or input_file.ID
LOG.info("INPUT FILE %i / %s", n, page_id)
pcgts = page_from_file(self.workspace.download_file(input_file))
metadata = pcgts.get_Metadata() # ensured by from_file()
metadata.add_MetadataItem(
MetadataItemType(type_="processingStep",
name=self.ocrd_tool['steps'][0],
value=TOOL,
# FIXME: externalRef is invalid by pagecontent.xsd, but ocrd does not reflect this
# what we want here is `externalModel="ocrd-tool" externalId="parameters"`
Labels=[LabelsType(#externalRef="parameters",
Label=[LabelType(type_=name,
value=self.parameter[name])
for name in self.parameter.keys()])]))
page = pcgts.get_Page()

for region1 in page.get_TextRegion():
for region2 in page.get_TextRegion():
if region1.id == region2.id:
continue
LOG.info('Comparing regions "%s" and "%s"', region1.id, region2.id)
region_poly1 = Polygon(polygon_from_points(region1.get_Coords().points))
region_poly2 = Polygon(polygon_from_points(region2.get_Coords().points))
LOG.info('Intersection %i', region_poly1.intersects(region_poly2))
LOG.info('Containment %i', region_poly1.contains(region_poly2))
if region_poly1.intersects(region_poly2):
LOG.info('Area 1 %d', region_poly1.area)
LOG.info('Area 2 %d', region_poly2.area)
LOG.info('Area intersect %d', region_poly1.intersection(region_poly2).area)


# Use input_file's basename for the new file -
# this way the files retain the same basenames:
file_id = input_file.ID.replace(self.input_file_grp, self.output_file_grp)
if file_id == input_file.ID:
file_id = concat_padded(self.output_file_grp, n)
self.workspace.add_file(
ID=file_id,
file_grp=self.output_file_grp,
pageId=input_file.pageId,
mimetype=MIMETYPE_PAGE,
local_filename=os.path.join(self.output_file_grp,
file_id + '.xml'),
content=to_xml(pcgts))
19 changes: 19 additions & 0 deletions ocrd_evaluate_segmentation/ocrd-tool.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "0.0.1",
"git_url": "https://github.com/OCR-D/ocrd_evaluate_segmentation",
"tools": {
"ocrd-evaluate-segmentation": {
"executable": "ocrd-evaluate-segmentation",
"categories": ["Image preprocessing"],
"description": "Evaluate regions",
"input_file_grp": [
"OCR-D-IMG",
"OCR-D-SEG-BLOCK"
],
"output_file_grp": [
"OCR-D-EVAL-BLOCK"
],
"steps": ["preprocessing/optimization/deskewing"]
}
}
}
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ocrd >= 1.0.0b11
click
shapely
30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
"""
Installs:
- ocrd-evaluate-segmentation
"""
import codecs

from setuptools import setup, find_packages

setup(
name='ocrd_evaluate_segmentation',
version='0.0.1',
description='Segmentation evaluation',
long_description=codecs.open('README.md', encoding='utf-8').read(),
author='Konstantin Baierer, Kay-Michael Würzner, Robert Sachunsky',
author_email='[email protected], [email protected], [email protected]',
url='https://github.com/OCR-D/ocrd_evaluate_segmentation',
license='Apache License 2.0',
packages=find_packages(exclude=('tests', 'docs')),
install_requires=open('requirements.txt').read().split('\n'),
package_data={
'': ['*.json', '*.yml', '*.yaml'],
},
entry_points={
'console_scripts': [
'ocrd-evaluate-segmentation=ocrd_evaluate_segmentation.cli:ocrd_evaluate_segmentation',
]
},
)

0 comments on commit 5f02107

Please sign in to comment.