Added OCR capabilites
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from common.api import register_links, register_menu
|
||||
|
||||
from documents.models import Document
|
||||
|
||||
|
||||
submit_document = {'text':_('submit to ocr'), 'view':'submit_document', 'args':'object.id', 'famfam':'page_lightning'}
|
||||
|
||||
register_links(Document, [submit_document])
|
||||
|
||||
#register_menu([
|
||||
# {'text':_('OCR'), 'view':'ocr_queue', 'links':[
|
||||
# ocr_queue
|
||||
# ],'famfam':'hourglass','position':5}])
|
||||
@@ -0,0 +1,66 @@
|
||||
#Some code from http://wiki.github.com/hoffstaetter/python-tesseract
|
||||
|
||||
import os
|
||||
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from documents.models import DocumentPage
|
||||
from documents.conf.settings import TEMPORARY_DIRECTORY
|
||||
from converter.api import convert_document_for_ocr
|
||||
|
||||
from ocr.conf.settings import TESSERACT_PATH
|
||||
|
||||
|
||||
def cleanup(filename):
|
||||
''' tries to remove the given filename. Ignores non-existent files '''
|
||||
try:
|
||||
os.remove(filename)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
class TesseractError(Exception):
|
||||
def __init__(self, status, message):
|
||||
self.status = status
|
||||
self.message = message
|
||||
|
||||
|
||||
def run_tesseract(input_filename, output_filename_base, lang=None):
|
||||
command = [TESSERACT_PATH, input_filename, output_filename_base]
|
||||
if lang is not None:
|
||||
command += ['-l', lang]
|
||||
|
||||
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
|
||||
return (proc.wait(), proc.stderr.read())
|
||||
|
||||
|
||||
def ocr_document(document):
|
||||
total_pages = 1
|
||||
page = 0
|
||||
while page < total_pages:
|
||||
imagefile = convert_document_for_ocr(document, page=page)
|
||||
desc, filepath = tempfile.mkstemp()
|
||||
try:
|
||||
status, error_string = run_tesseract(imagefile, filepath)
|
||||
if status:
|
||||
errors = get_errors(error_string)
|
||||
raise TesseractError(status, errors)
|
||||
finally:
|
||||
ocr_output = os.extsep.join([filepath, 'txt'])
|
||||
f = file(ocr_output)
|
||||
try:
|
||||
document_page, created = DocumentPage.objects.get_or_create(document=document,
|
||||
page_number=page)
|
||||
document_page.content = f.read().strip()
|
||||
document_page.page_label = _(u'Text from OCR')
|
||||
document_page.save()
|
||||
finally:
|
||||
f.close()
|
||||
cleanup(filepath)
|
||||
cleanup(ocr_output)
|
||||
cleanup(imagefile)
|
||||
|
||||
page += 1
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.conf import settings
|
||||
|
||||
TESSERACT_PATH = getattr(settings, 'OCR_TESSERACT_PATH', u'/usr/bin/tesseract')
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -0,0 +1,23 @@
|
||||
"""
|
||||
This file demonstrates two different styles of tests (one doctest and one
|
||||
unittest). These will both pass when you run "manage.py test".
|
||||
|
||||
Replace these with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def test_basic_addition(self):
|
||||
"""
|
||||
Tests that 1 + 1 always equals 2.
|
||||
"""
|
||||
self.failUnlessEqual(1 + 1, 2)
|
||||
|
||||
__test__ = {"doctest": """
|
||||
Another way to test that 1 + 1 is equal to 2.
|
||||
|
||||
>>> 1 + 1 == 2
|
||||
True
|
||||
"""}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.conf.urls.defaults import *
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
urlpatterns = patterns('ocr.views',
|
||||
url(r'^(?P<document_id>\d+)/submit/$', 'submit_document', (), 'submit_document'),
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response, get_object_or_404, redirect
|
||||
from django.template import RequestContext
|
||||
from django.contrib import messages
|
||||
from django.views.generic.list_detail import object_detail, object_list
|
||||
from django.core.urlresolvers import reverse
|
||||
#from django.views.generic.create_update import create_object, delete_object, update_object
|
||||
from django.conf import settings
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
|
||||
from documents.models import Document
|
||||
|
||||
|
||||
from api import ocr_document
|
||||
|
||||
def submit_document(request, document_id):
|
||||
document = get_object_or_404(Document, pk=document_id)
|
||||
|
||||
try:
|
||||
result = ocr_document(document)
|
||||
except Exception, e:
|
||||
messages.error(request, e.message)
|
||||
return HttpResponseRedirect(request.META['HTTP_REFERER'])
|
||||
|
||||
messages.success(request, _(u'Document OCR was successful.'))
|
||||
return HttpResponseRedirect(request.META['HTTP_REFERER'])
|
||||
Reference in New Issue
Block a user