Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - Uploading image and getting error 500 Internet Server after deploy in Heroku by Flask

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...