Save glyph database on exit, load on startup.

This commit is contained in:
Andrey Golovizin 2014-08-21 21:19:57 +02:00
parent a929d1011b
commit 5babd24450
2 changed files with 19 additions and 3 deletions

View file

@ -46,6 +46,7 @@ def main():
ocr.pageChanged.connect(win.pageScene.setPage)
ocr.unknownGlyph.connect(win.unknownGlyph)
win.glyphEntered.connect(ocr.give_help)
app.aboutToQuit.connect(ocr.save_glyphdb)
ocr.start()
signal.signal(signal.SIGINT, signal.SIG_DFL)

View file

@ -14,6 +14,7 @@
# 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
@ -34,10 +35,24 @@ class OCREngine(QThread):
def __init__(self, dirname):
super().__init__()
self.dirname = dirname
self.filenames = glob(path.join(dirname, '*.png'))
self.chardb = {}
self.glyphdb_filename = path.join(self.dirname, 'glyphdb.pickle')
self.glyphdb = self.load_glyphdb()
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)
def load_page(self, filename):
return Page(Image.fromfile(filename).unframe(10))
@ -63,10 +78,10 @@ class OCREngine(QThread):
if isinstance(glyph, Space):
return ' '
try:
return self.chardb[glyph.key]
return self.glyphdb[glyph.key]
except KeyError:
text = self.ask_for_help(glyph)
self.chardb[glyph.key] = text
self.glyphdb[glyph.key] = text
return text
def ask_for_help(self, unknown_glyph):