CoolFace
Apppublic

muhammadzain/flask-backend

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
api.py103 linesDownload Raw Back to root
1from flask import Flask, request2from flask_cors import CORS3from werkzeug.utils import secure_filename4import cv25import easyocr6import os7 8# -----9 10# Initializing flask app11app = Flask(__name__)12CORS(app)13 14 15@app.route('/')16def index():17    return ("Please use Frontend ---> https://text-extraction-ce2b5.web.app")18 19 20# Route for seeing a data21@app.route('/data', methods=['POST'])22def get_time():23    24    try:   # If your backend has any problem and you are not getting any error in logs, then remove this try except to see error.25        file = request.files['file']26        file.save(secure_filename(file.filename))27        img = cv2.imread(secure_filename(file.filename))28        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)29    except:30       return["Sorry something went wrong. Perhaps you did not select your image. Please try again."]   31    rois = request.form['rois'].split(',')32    div = request.form['divisionWH'].split(',')33 34    print(rois, "\n", div)35 36    37    # specify languages and other configs38    reader = easyocr.Reader(['en'], gpu=False)39 40    IacH, IacW = img.shape41    IapW, IapH = int(div[0]), int(div[1])42 43    Rw = IacW / IapW44    Rh = IacH / IapH45    # print(Rw,Rh)46 47    output = ""48    os.system("echo processing start")49    if len(rois) > 1:50 51        for i in range(int(len(rois) / 4)):52            j = i * 453            global X1, X2, Y1, Y254            try:55                x1, y1, x2, y2 = int(rois[j]), int(rois[j + 1]), int(rois[j + 2]), int(rois[j + 3])56                X1, Y1, X2, Y2 = int(Rw * x1), Rh * y1, int(Rw * x2), Rh * y257            except:58                return ["Sorry something went wrong. Perhaps you did not select your image. Please try again."]   59            Offset_y1 = ((IacH * y1 / 100) * 0.02) / 10060            Offset_y2 = ((IacH * y2 / 100) * 0.05) / 10061            print(Offset_y1)62            Y1 = int(Y1)63            Y2 = int(Y2 + Offset_y2)64            img_ROI = img[Y1:Y2, X1:X2]65            result = reader.readtext(img_ROI, paragraph='True')66 67            for res in result:68                top_left = tuple(res[0][0])  # top left coordinates as tuple69                bottom_right = tuple(res[0][2])  # bottom right coordinates as tuple70                # draw rectangle on image71                cv2.rectangle(img_ROI, top_left, bottom_right, (0, 255, 0), 2)72                # write recognized text on image (top_left) minus 10 pixel on y73                cv2.putText(img_ROI, res[1], (top_left[0], top_left[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,74                            (255, 0, 0, 1))75                output = output + res[1] + '\n\n'76                print(output)77                cv2.rectangle(img_ROI, (X1, Y1), (X2, Y2), (255, 0, 255), 2)78 79    else:80        print((int(img.shape[1]*0.65),int(img.shape[0]*0.65)))81        img = cv2.resize(img,(int(img.shape[1]*0.65),int(img.shape[0]*0.65)))82        result = reader.readtext(img, paragraph='True')83        print(result, "Result")84        # iterate on all results85        for res in result:86            top_left = tuple(res[0][0])  # top left coordinates as tuple87            bottom_right = tuple(res[0][2])  # bottom right coordinates as tuple88            # draw rectangle on image89            top_left = (int(top_left[0]), int(top_left[1]))90            bottom_right = (int(bottom_right[0]), int(bottom_right[1]))91            cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)92            # write recognized text on image (top_left) minus 10 pixel on y93            # cv2.putText(img, res[1], (top_left[0], top_left[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)94            output = output + res[1]95 96    print('ok')97    os.remove(secure_filename(file.filename))98    os.system("echo processing done!")99    return [output]100 101 102if __name__ == "__main__":103    app.run(debug=True,host="0.0.0.0",port=5000)