Add support for output formats.

This commit is contained in:
Andrey Golovizin 2014-09-04 17:46:48 +02:00
parent bd2a206940
commit 296035c966
3 changed files with 97 additions and 10 deletions

View file

@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import itertools
from glob import glob
from os import path
from queue import Queue
@ -27,9 +28,10 @@ from PyQt4.QtGui import (
qApp
)
from .. import formatting
from ..image import Image
from ..page import Page, Glyph, Space
from ..glyphdb import GlyphDB
from ..glyphdb import GlyphDB, SPACE, NEWLINE
class OCREngine(QThread):
@ -53,7 +55,6 @@ class OCREngine(QThread):
def run(self):
for page_text in self.recognize():
print()
print(page_text)
if self.quit:
qApp.quit()
@ -62,27 +63,30 @@ class OCREngine(QThread):
for filename in self.filenames:
page = self.load_page(filename)
self.pageChanged.emit(page)
yield '\n'.join(self.recognize_page(page))
yield self.recognize_page(page)
def recognize_page(self, page):
for line in page.lines:
yield ''.join(self.recognize_line(line))
glyph_data_seq = itertools.chain(*(self.recognize_line(line) for line in page.lines))
output_format = formatting.TextFormat()
return ''.join(output_format.format(glyph_data_seq))
def recognize_line(self, line):
yield from ' ' * int(line.indent / self.SPACE_WIDTH)
yield from [SPACE] * int(line.indent / self.SPACE_WIDTH)
for glyph in line.glyphs:
yield self.recognize_glyph(glyph)
yield NEWLINE
def recognize_glyph(self, glyph):
qApp.processEvents()
if isinstance(glyph, Space):
return ' '
return SPACE
try:
return self.glyphdb[glyph].text
glyph_data = self.glyphdb[glyph]
except KeyError:
text, bold, italic = self.ask_for_help(glyph)
self.glyphdb.add_glyph(glyph, text, bold, italic)
return text
glyph_data = self.glyphdb.add_glyph(glyph, text, bold, italic)
return glyph_data
def ask_for_help(self, unknown_glyph):
self.unknownGlyph.emit(unknown_glyph)