kiki
kiki

Reputation: 79

calling local python file in flask service

I have two python code, the first one is a flask python code(main.py) which collects image from the user and stored it in my local directory, and the second one is tesseract-ocr(ocrDetection.py) python code where the image are getting started to detect text in it.

Now I desired to integrate these two codes, by importing ocr code in flask [ import ocrDetection in main.py]

import os
#import magic
import urllib.request
from app import app
from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename


ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
    
@app.route('/')
def upload_form():
    return render_template('upload.html')

@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the files part
        if 'files[]' not in request.files:
            flash('No file part')
            return redirect(request.url)
        files = request.files.getlist('files[]')
        for file in files:
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                                
        flash('File(s) successfully uploaded')
        return redirect('/')

@app.route('/')
def usemain():
        if request.method == 'POST':
                import ocrDetection
                ocrDetection.mask()
                
if __name__ == "__main__":
    app.run(host='192.168.106.51')
    

this is not working properly if I give my OCR program after __name__=="__main__" like this

if __name__ == "__main__":
    app.run(host='192.168.106.51')
    import ocrDetection

it works only when I quit the server (ctrl+c), but I want to start my OCR program simultaniously after giving submit button

Upvotes: 2

Views: 282

Answers (1)

AMIT
AMIT

Reputation: 176

This will not work because you have two same routes ('/') which create conflict. I suggest that you create a new route for OCR e.g. '/ocr_detection' or create ocr_detection() as a module in a separate file.

To call OCR module, you can perhaps use it as API endpoint if you want to call from client like browser or as a module to simply call it from your backend (from within the views).

If you call it from within upload_file() module then it will be a sequential call and your browser has to wait until OCR is finished. But to avoid this, you can run periodic script which monitors the UPLOAD_DIR for new files and OCR them. This way your user browsing experience will also be much better.

....

@app.route('/', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the files part
        if 'files[]' not in request.files:
            flash('No file part')
            return redirect(request.url)
        files = request.files.getlist('files[]')
        for file in files:
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                ocr_detection(filename)
                                
        flash('File(s) successfully uploaded')
        return redirect('/')


def ocr_detection(file):
        import ocrDetection
        ocrDetection.mask()
                
if __name__ == "__main__":
    app.run(host='192.168.106.51')

Upvotes: 2

Related Questions