diff --git a/pixelocr/glyphdb.py b/pixelocr/glyphdb.py new file mode 100644 index 0000000..3026ae3 --- /dev/null +++ b/pixelocr/glyphdb.py @@ -0,0 +1,41 @@ +# 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 . + + +import pickle +from os import path + + +class GlyphDB(object): + def __init__(self, filename): + self.filename = filename + if path.isfile(self.filename): + with open(self.filename, 'rb') as fileobj: + self.data = pickle.load(fileobj) + else: + self.data = {} + + def __getitem__(self, key): + return self.data.__getitem__(key) + + def __setitem__(self, key, value): + return self.data.__setitem__(key, value) + + def __delitem__(self, key): + return self.data.__delitem__(key) + + def save(self): + with open(self.filename, 'wb') as fileobj: + pickle.dump(self.data, fileobj) diff --git a/pixelocr/gui/ocrengine.py b/pixelocr/gui/ocrengine.py index b78e22b..2dd2d54 100644 --- a/pixelocr/gui/ocrengine.py +++ b/pixelocr/gui/ocrengine.py @@ -14,7 +14,6 @@ # along with this program. If not, see . -import pickle from glob import glob from os import path from queue import Queue @@ -27,6 +26,7 @@ from PyQt4.QtCore import ( from ..image import Image from ..page import Page, Glyph, Space +from ..glyphdb import GlyphDB class OCREngine(QThread): @@ -37,21 +37,11 @@ class OCREngine(QThread): super().__init__() self.dirname = dirname self.filenames = glob(path.join(dirname, '*.png')) - self.glyphdb_filename = path.join(self.dirname, 'glyphdb.pickle') - self.glyphdb = self.load_glyphdb() + self.glyphdb = GlyphDB(path.join(self.dirname, 'glyphdb.pickle')) self.help_queue = Queue() - def load_glyphdb(self): - if path.isfile(self.glyphdb_filename): - with open(self.glyphdb_filename, 'rb') as glyphdb_file: - glyphdb = pickle.load(glyphdb_file) - else: - glyphdb = {} - return glyphdb - def save_glyphdb(self): - with open(self.glyphdb_filename, 'wb') as glyphdb_file: - pickle.dump(self.glyphdb, glyphdb_file) + self.glyphdb.save() def load_page(self, filename): return Page(Image.fromfile(filename).unframe(10))