Move glyph database logic to picelocr.glyphdb.

This commit is contained in:
Andrey Golovizin 2014-08-21 21:52:22 +02:00
parent 9656abe9b2
commit 182a49b359
2 changed files with 44 additions and 13 deletions

41
pixelocr/glyphdb.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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)

View file

@ -14,7 +14,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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))