Added support for the command line program pdftotext from the poppler-utils packages to extract text from PDF documents without doing OCR

This commit is contained in:
Roberto Rosario
2011-04-15 23:59:52 -04:00
parent 73a52293e8
commit eaaaa5b645
5 changed files with 49 additions and 13 deletions

View File

@@ -14,6 +14,8 @@ from documents.models import DocumentPage
from ocr.conf.settings import TESSERACT_PATH
from ocr.conf.settings import TESSERACT_LANGUAGE
from ocr.conf.settings import PDFTOTEXT_PATH
from exceptions import TesseractError, PdftotextError
def get_language_backend():
@@ -27,10 +29,6 @@ def get_language_backend():
backend = get_language_backend()
class TesseractError(Exception):
pass
def cleanup(filename):
''' tries to remove the given filename. Ignores non-existent files '''
try:
@@ -51,27 +49,54 @@ def run_tesseract(input_filename, output_filename_base, lang=None):
raise TesseractError(error_text)
def run_pdftotext(input_filename, output_filename, page_number=None):
command = [unicode(PDFTOTEXT_PATH)]
if page_number:
command.extend(['-nopgbrk', '-f', unicode(page_number), '-l', unicode(page_number)])
command.extend([unicode(input_filename), unicode(output_filename)])
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
if return_code != 0:
error_text = proc.stderr.read()
raise PdftotextError(error_text)
def do_document_ocr(document):
for page_index, document_page in enumerate(document.documentpage_set.all()):
imagefile = convert_document_for_ocr(document, page=page_index)
desc, filepath = tempfile.mkstemp()
imagefile = None
source = u''
try:
run_tesseract(imagefile, filepath, TESSERACT_LANGUAGE)
ocr_output = os.extsep.join([filepath, 'txt'])
if document.file_mimetype == u'application/pdf':
pdf_filename = os.extsep.join([filepath, u'pdf'])
document.save_to_file(pdf_filename)
run_pdftotext(pdf_filename, filepath, document_page.page_number)
cleanup(pdf_filename)
if os.stat(filepath).st_size == 0:
#PDF page had no text, run tesseract on the page
imagefile = convert_document_for_ocr(document, page=page_index)
run_tesseract(imagefile, filepath, TESSERACT_LANGUAGE)
ocr_output = os.extsep.join([filepath, 'txt'])
source = _(u'Text from OCR')
else:
ocr_output = filepath
source = _(u'Text extracted from PDF')
else:
imagefile = convert_document_for_ocr(document, page=page_index)
run_tesseract(imagefile, filepath, TESSERACT_LANGUAGE)
ocr_output = os.extsep.join([filepath, 'txt'])
source = _(u'Text from OCR')
f = codecs.open(ocr_output, 'r', 'utf-8')
document_page = document.documentpage_set.get(page_number=page_index + 1)
document_page.content = ocr_cleanup(f.read().strip())
document_page.page_label = _(u'Text from OCR')
document_page.page_label = source
document_page.save()
f.close()
cleanup(ocr_output)
except TesseractError, e:
cleanup(filepath)
cleanup(imagefile)
raise TesseractError(e)
finally:
cleanup(filepath)
cleanup(imagefile)
if imagefile:
cleanup(imagefile)
def ocr_cleanup(text):