Improved tesseract execution handling
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#Some code from http://wiki.github.com/hoffstaetter/python-tesseract
|
#Some code from http://wiki.github.com/hoffstaetter/python-tesseract
|
||||||
|
|
||||||
|
import codecs
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -14,6 +15,11 @@ from documents.models import Document
|
|||||||
from converter.api import convert_document_for_ocr
|
from converter.api import convert_document_for_ocr
|
||||||
|
|
||||||
from ocr.conf.settings import TESSERACT_PATH
|
from ocr.conf.settings import TESSERACT_PATH
|
||||||
|
from ocr.conf.settings import TESSERACT_LANGUAGE
|
||||||
|
|
||||||
|
|
||||||
|
class TesseractError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def cleanup(filename):
|
def cleanup(filename):
|
||||||
@@ -23,51 +29,37 @@ def cleanup(filename):
|
|||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TesseractError(Exception):
|
|
||||||
pass
|
|
||||||
# def __init__(self, status, message):
|
|
||||||
# self.status = status
|
|
||||||
# self.message = message
|
|
||||||
|
|
||||||
def get_errors(error_string):
|
|
||||||
'''
|
|
||||||
returns all lines in the error_string that start with the string "error"
|
|
||||||
|
|
||||||
'''
|
|
||||||
lines = error_string.splitlines()
|
|
||||||
return lines[1]
|
|
||||||
#error_lines = (line for line in lines if line.find('error') >= 0)
|
|
||||||
#return '\n'.join(error_lines)
|
|
||||||
|
|
||||||
def run_tesseract(input_filename, output_filename_base, lang=None):
|
def run_tesseract(input_filename, output_filename_base, lang=None):
|
||||||
command = [TESSERACT_PATH, input_filename, output_filename_base]
|
command = [TESSERACT_PATH, input_filename, output_filename_base]
|
||||||
if lang is not None:
|
if lang is not None:
|
||||||
command += ['-l', lang]
|
command += ['-l', lang]
|
||||||
|
|
||||||
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
|
proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||||
return (proc.wait(), proc.stderr.read())
|
return_code = proc.wait()
|
||||||
|
if return_code != 0:
|
||||||
|
error_text = proc.stderr.read()
|
||||||
|
raise TesseractError(error_text)
|
||||||
|
|
||||||
|
|
||||||
def do_document_ocr(document):
|
def do_document_ocr(document):
|
||||||
for page_index, document_page in enumerate(document.documentpage_set.all()):
|
for page_index, document_page in enumerate(document.documentpage_set.all()):
|
||||||
imagefile = convert_document_for_ocr(document, page=page_index)
|
imagefile = convert_document_for_ocr(document, page=page_index)
|
||||||
desc, filepath = tempfile.mkstemp()
|
desc, filepath = tempfile.mkstemp()
|
||||||
try:
|
try:
|
||||||
status, error_string = run_tesseract(imagefile, filepath)
|
run_tesseract(imagefile, filepath, TESSERACT_LANGUAGE)
|
||||||
if status:
|
|
||||||
errors = get_errors(error_string)
|
|
||||||
raise TesseractError(errors)
|
|
||||||
finally:
|
|
||||||
ocr_output = os.extsep.join([filepath, 'txt'])
|
ocr_output = os.extsep.join([filepath, 'txt'])
|
||||||
|
f = codecs.open(ocr_output, 'r', 'utf-8')
|
||||||
f = file(ocr_output)
|
|
||||||
try:
|
|
||||||
document_page = document.documentpage_set.get(page_number=page_index+1)
|
document_page = document.documentpage_set.get(page_number=page_index+1)
|
||||||
document_page.content = f.read().strip()
|
document_page.content = f.read().strip()
|
||||||
document_page.page_label = _(u'Text from OCR')
|
document_page.page_label = _(u'Text from OCR')
|
||||||
document_page.save()
|
document_page.save()
|
||||||
finally:
|
|
||||||
f.close()
|
f.close()
|
||||||
cleanup(filepath)
|
|
||||||
cleanup(ocr_output)
|
cleanup(ocr_output)
|
||||||
|
except TesseractError, e:
|
||||||
|
cleanup(filepath)
|
||||||
|
cleanup(imagefile)
|
||||||
|
raise TesseractError(e)
|
||||||
|
finally:
|
||||||
|
cleanup(filepath)
|
||||||
cleanup(imagefile)
|
cleanup(imagefile)
|
||||||
|
|||||||
Reference in New Issue
Block a user