Add Configuration() class and make it accessible as Document.config and PageObject.config.

This commit is contained in:
Andrey Golovizin 2014-09-12 12:35:44 +02:00
parent 8805803e53
commit d3c32b82ab
3 changed files with 35 additions and 4 deletions

27
pixelocr/config.py Normal file
View file

@ -0,0 +1,27 @@
# Copyright (C) 2014 Andrey Golovizin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from confire import Configuration as BaseConfiguration
class Configuration(BaseConfiguration):
min_body_height = 10
@classmethod
def load_file(cls, filename):
class MyConfiguration(cls):
CONF_PATHS = [filename]
return MyConfiguration.load()

View file

@ -22,6 +22,7 @@ from . import formatting
from .image import Image
from .page import Page, Space
from .glyphdb import GlyphDB, SPACE, NEWLINE
from .config import Configuration
class Document(object):
@ -33,6 +34,7 @@ class Document(object):
self.ui = ui
self.filenames = sorted(glob(path.join(dirname, '*.png')))[skip:skip + limit if limit else None]
self.glyphdb = GlyphDB(path.join(self.dirname, 'glyphdb.pickle'))
self.config = Configuration.load_file(path.join(self.dirname, 'config.yaml'))
self.output_format = output_format
self.last_style = (False, False, (255, 255, 255)) # FIXME get rid of hardcoded value
@ -40,7 +42,7 @@ class Document(object):
self.glyphdb.save()
def load_page(self, filename):
return Page(Image.fromfile(filename), filename)
return Page(self, Image.fromfile(filename), filename)
def recognize(self):
for filename in self.filenames:

View file

@ -29,9 +29,11 @@ CONNECTIVITY8 = ndimage.generate_binary_structure(2, 2)
class PageObject(object):
def __init__(self, image, filename=None):
def __init__(self, document, image, filename=None):
self.image = image
self.filename = filename
self.document = document
self.config = document.config
def _repr_png_(self):
return self.image._repr_png_()
@ -163,7 +165,7 @@ class Page(PageObject):
class Line(PageObject):
def __init__(self, page, image):
super().__init__(image)
super().__init__(page.document, image)
self.page = page
def __iter__(self):
@ -247,7 +249,7 @@ class Glyph(PageObject):
MIN_BODY_HEIGHT = 10
def __init__(self, line, image, elevation):
super().__init__(image)
super().__init__(line.document, image)
self.elevation = elevation
self.line = line