I am trying to link PyTesserocr to Google Cloud Translate API, using Python 3.6 and Django. But I did not know to link correctly it. I also tried to call Google Translate API variables to the HTML file.
For example, the original codes:
views.py:a. PyTesserocr (this code works perfectly):
from django.http import Http404
from django.http import HttpResponse
from django.http.response import JsonResponse
from django.shortcuts import get_object_or_404, render
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View, TemplateView
from google.cloud import translate
from PIL import Image, ImageFilter
from tesserocr import PyTessBaseAPI
class OcrFormView(TemplateView):
template_name = 'documents/ocr_form.html'
ocr_form_view = OcrFormView.as_view()
class OcrView(View):
def post(self, request, *args, **kwargs):
with PyTessBaseAPI() as api:
with Image.open(request.FILES['image']) as image:
image = image.convert('RGB')
sharpened_image = image.filter(ImageFilter.SHARPEN)
api.SetImage(sharpened_image)
utf8_text = api.GetUTF8Text()
return JsonResponse({'utf8_text': utf8_text})
ocr_view = csrf_exempt(OcrView.as_view())
b. Google Translate Cloud (this code works perfectly)
# Instanciando o cliente
translate_client = translate.Client()
# translate_client = translate.Client.from_service_account_json('meu_projecto.json')
# O texto para traduzir The text to translate
text2 = text
# O idioma-alvo
target = 'pt'
# Translates some text into Russian
translation = translate_client.translate(
text2,
target_language=target)
print(u'Texto: {}'.format(text2))
print(u'Tradução: {}'.format(translation['translatedText']))
I replaced text2 = text for text2 = ocr_view.
I replaced
print(u'Texto: {}'.format(text2))
print(u'Tradução: {}'.format(translation['translatedText']))
for
def index(request):
text2 = {'text2': text2}
return render(request, 'documents/ocr_form.html', text2)
def index(request):
translation['translatedText'] = {'translatedText': translation}
return render(request, 'documents/ocr_form.html', translation)
documents/ocr_form.html):<h2>Translating scanned text from OCR into our native language</h2>
<h3>Text</h3>
<p> {{ text2 }} </p>
<h3>Translation</h3>
<p> {{ translation['translatedText'] }} </p>
Do you know solving it? Which variables do you call correctly?
No responses yet.