0

I have a Python app with Tesseract OCR. I run on my pc. It's work!

enter image description here enter image description here

However, I need deploy to Heroku. I was setting Aptfile, buildpack, TESSDATA_PREFIX already (ref: How to deploy pytesseract to Heroku). It's done.

Here is my console

Heroku https://image-text-try.herokuapp.com/

Github https://github.com/b-bella/image_text_try

It can run but error after submitting upload image.

enter image description here enter image description here

Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Could anyone see my coding on github & heroku and suggest how to fix about deploying to heroku?

from flask import Flask, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
import shutil
import os
import uuid
import pytesseract

pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract'
#pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

app = Flask(__name__)

app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpeg', 'jpg','gif'])

APP_ROOT = os.path.dirname(__file__)
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static').replace('\\','/')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'GET':
        return render_template('index.html')

    if request.method == 'POST':
        if request.files:
            file = request.files['file_image']

            if allowed_file(file.filename):
                filename = secure_filename(file.filename)

                temp_file = os.path.join(app.config['UPLOAD_FOLDER'], filename).replace('\\', '/')
                file.save(temp_file)

                pre_result = pytesseract.image_to_string(temp_file, lang='eng')

                return render_template('index.html', result=pre_result, filename=filename)

            else:
                return render_template('index.html', result='That file extension is not allowed', filename=file.filename)

if __name__ == '__main__':
    app.run()

enter image description here

Miki
  • 40,887
  • 13
  • 123
  • 202
Bella
  • 43
  • 4
  • Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied and offer poor usability. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Jan 07 '21 at 17:46
  • I suggest you add some granular logging statements to whatever function is handling this request (I'm guessing `upload_file()`). – ChrisGPT was on strike Jan 07 '21 at 17:47
  • Note also that [you shouldn't be storing uploaded files on your dyno's local filesystem](https://stackoverflow.com/a/53016453/354577). See https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted – ChrisGPT was on strike Jan 07 '21 at 17:51

0 Answers0