Git Product home page Git Product logo

lntechnical2 / python-deploy-tesseract-ocr-to-heroku Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kevin1061517/python-deploy-tesseract-ocr-to-heroku

0.0 0.0 0.0 29 KB

I spent several times deploying the tesseract-ocr buildpack to the heroku. But I got not only the TesseractNotFoundError but also the TesseractError......WTF , finally....heaven helps them who help themselves

License: MIT License

Python 100.00%

python-deploy-tesseract-ocr-to-heroku's Introduction

Deploy Tesseract-OCR to Heroku(Linebot)

Descript

Because I have stuck on the this question for two days, I take some notes to remind me of deeploying the Tesseract-OCR to Heroku. First, Tesseract is an OCR sponsored by Google. It is open-source and its binaries are available for lots of platforms, Additionally, it is a popular go-to library when OCR functionalities are required in an app. Setting up Tesseract-OCR is a procedure in popular development environments such as Heroku.Most of all, the following article is the process of deploying and coding.

Python Code

import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract'
image = Image.open(path)
t = pytesseract.image_to_string(image)
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=t))

Focus on the path!!! -> '/app/.apt/usr/bin/tesseract'

step

1.Add heroku-apt-buildpack into buildpacks on heroku:

You can use the command following:

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt

or you can input the https://github.com/heroku/heroku-buildpack-apt by using Graphical interface on heroku

2.Create a file named as Aptfile in your app directory and paste the following:

tesseract-ocr
tesseract-ocr-eng

You must create the file named as Aptfile, or the error will emerge.

3.set a heroku config variable named TESSDATA_PREFIX. This is the path to the data downloaded by the tesseract-ocr-eng package.

You can use the command following:

heroku config:set TESSDATA_PREFIX=/app/.apt/usr/share/tesseract-ocr/tessdata

or you can input the TESSDATA_PREFIX=/app/.apt/usr/share/tesseract-ocr/tessdata by using Graphical interface on heroku

Notes

1.I got errors when I paste the tesseract-ocr into the requirements.txt.Because

  • I used to add buildpack on heroku, I always need to input my required module into requirements.txt. But in this case,you never need the requirements.txt.

2.You must set a heroku config variable named TESSDATA_PREFIX, and inputing the TESSDATA_PREFIX's path.

  • the error image of ignoring the configuration of variable named TESSDATA_PREFIX

3.Most of all,how can I find the main file named tesseract on heroku.

  • First I would execuate the cmd command on heroku CLI and perform certain commands in the following commands.I should step into bash in order to find the path of tesseract on heroku bash.
heroku run bash
which tesseract
.......

  • Focus on fourth line

LINEBOT screenshop

Code

def template_img(path):
            print('temp---------'+str(path))
            buttons_template = TemplateSendMessage(
            alt_text='news template',
            template=ButtonsTemplate(
                title='你傳來的是照片喔',
                text='請選擇怎樣處理',
                thumbnail_image_url='https://i.imgur.com/GoAYFqv.jpg',
                actions=[
                    PostbackTemplateAction(
                        label='影像文字翻譯辨識',
                        text='請稍等....',
                        data = 'trans/{}'.format(path)
                    ),
                    PostbackTemplateAction(
                        label='影像儲存至相簿',
                        text='請稍等....',
                        data = 'image/{}'.format(path)
                    )
                ]
            )
            )
            return buttons_template
@handler.add(PostbackEvent)
def handle_postback(event):
    temp = event.postback.data
    s = ''
    if temp[:5] == 'image':
     print('------postback'+str(temp))
     t = temp.split('/')
     path = '/{}/{}'.format(t[2],t[3])
     print('postback---------'+str(path))
     img_id = 1
     t = fb.get('/pic',None)
     if t!=None:
         count = 1
         for key,value in t.items():
            if count == len(t):#取得最後一個dict項目
                img_id = int(value['id'])+1
            count+=1
     try:

        client = ImgurClient(client_id, client_secret, access_token, refresh_token)
        config = {
            'album': album_id,
            'name' : img_id,
            'title': img_id,
            'description': 'Cute kitten being cute on'
        }
        client.upload_from_path(path, config=config, anon=False)
        os.remove(path)
        line_bot_api.reply_message(event.reply_token,[TextSendMessage(text='上傳成功'),image_reply])
     except  Exception as e:
        t = '上傳失敗'+str(e.args)
        line_bot_api.reply_message(event.reply_token,TextSendMessage(text=t))
    elif temp[:5] == 'trans':
     t = temp.split('/')
     path = '/{}/{}'.format(t[2],t[3])
     print('postback----'+str(path)) 
     pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract'
     image = Image.open(path)
     t = pytesseract.image_to_string(image)
     line_bot_api.reply_message(event.reply_token,TextSendMessage(text=t))

#process image
@handler.add(MessageEvent,message=ImageMessage)
def handle_msg_img(event):
    message_content = line_bot_api.get_message_content(event.message.id)
    with tempfile.NamedTemporaryFile(prefix='jpg-', delete=False) as tf:
        for chunk in message_content.iter_content():
            tf.write(chunk)
        tempfile_path = tf.name
    path = tempfile_path

    buttons_template = template_img(path)
    line_bot_api.reply_message(event.reply_token,buttons_template)

Reference

https://stackoverflow.com/questions/42370732/heroku-error-opening-data-file-app-vendor-tesseract-ocr-tessdata-eng-traineddat
https://github.com/heroku/heroku-buildpack-apt
https://medium.com/@pro_science108/configurin-tesseract-ocr-in-heroku-16-444a4c079c41

python-deploy-tesseract-ocr-to-heroku's People

Contributors

kevin1061517 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.