From 5babd24450fa839bbc9163d9711d2f233f2724c2 Mon Sep 17 00:00:00 2001 From: Andrey Golovizin Date: Thu, 21 Aug 2014 21:19:57 +0200 Subject: [PATCH] Save glyph database on exit, load on startup. --- pixelocr/gui/__init__.py | 1 + pixelocr/gui/ocrengine.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pixelocr/gui/__init__.py b/pixelocr/gui/__init__.py index 8855975..f4994b5 100644 --- a/pixelocr/gui/__init__.py +++ b/pixelocr/gui/__init__.py @@ -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) diff --git a/pixelocr/gui/ocrengine.py b/pixelocr/gui/ocrengine.py index 409d045..b78e22b 100644 --- a/pixelocr/gui/ocrengine.py +++ b/pixelocr/gui/ocrengine.py @@ -14,6 +14,7 @@ # along with this program. If not, see . +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):