'How to Get Text From Image in Django Web Application using tesseract?

i am building a django web application to extract text from images. i wanna use tesseract for it. but tesseract python code work normally in windows pc and not work in django web application.

this is the error when i run python code using html click button

tesseract-ocr/tesseract.exe is not installed or it's not in your path.

this is the my python code for tesseract in django web app

def text1():
     global img

     import pytesseract as tess
     tess.pytesseract.tesseract_cmd = r'Tesseract-OCR\\tesseract.exe'
     from PIL import Image

     def program():

         img = Image.open("./static/img/figure-65.png")
         text = tess.image_to_string(img)

         print(text)

     program()

text1 function call by html click button and it correctly set to views.py and urls.py

This image shows where the tesseract python code and tesseract.exe In imageToText.py have the python code.

You can see Tesseract-OCR Folder, that's where **tesseract.exe** have.

This image shows where the tesseract python code and tesseract.exe

urls.py of web app

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name="home"),
    path('openC', views.openC, name="openC"),
    path('text1', views.text2, name="text1"),
]

urls.py of main web project

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

from img2text.settings import MEDIA_ROOT

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include("imgtext.urls"))
      
    ]+ static(settings.MEDIA_URL, document_root=MEDIA_ROOT)

views.py

from django.shortcuts import render,redirect
from .form import ImageForm
from .models import Image
from .imageToText import text1
from .opencvtext import openCv


def home(request):
    if request.method == "POST":
        form=ImageForm(data=request.POST,files=request.FILES)
        if form.is_valid():
            form.save()
            obj=form.instance
            
            return render(request,"index.html",{"obj":obj})
    else:
        form=ImageForm()
    img=Image.objects.all()

    return render(request,"index.html",{"img":img,"form":form})

def openC(request):
    openCv()

def text2(request):
    text1() #tesseract function call

so, what i did wrong? how i fix it or any other way to get text from image in django python?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source