File size: 3,837 Bytes
685252d
79372a6
685252d
 
 
 
 
0a9e9de
685252d
 
 
79372a6
685252d
 
0f2abbb
685252d
57bcb1c
685252d
 
 
 
 
c8efbb8
db8472d
 
 
810fd71
 
db8472d
 
685252d
 
 
 
 
810fd71
685252d
 
 
 
 
 
 
 
 
 
 
99522f4
685252d
 
 
 
 
810fd71
 
 
 
 
685252d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8025d06
 
685252d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99522f4
685252d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from flask import Flask, request
from flask_cors import CORS
from werkzeug.utils import secure_filename
import cv2
import easyocr
import os

# -----

# Initializing flask app
app = Flask(__name__)
CORS(app)


@app.route('/')
def index():
    return ("Please use Frontend ---> https://text-extraction-ce2b5.web.app")


# Route for seeing a data
@app.route('/data', methods=['POST'])
def get_time():
    
    try:   # If your backend has any problem and you are not getting any error in logs, then remove this try except to see error.
        file = request.files['file']
        file.save(secure_filename(file.filename))
        img = cv2.imread(secure_filename(file.filename))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    except:
       return["Sorry something went wrong. Perhaps you did not select your image. Please try again."]   
    rois = request.form['rois'].split(',')
    div = request.form['divisionWH'].split(',')

    print(rois, "\n", div)

    
    # specify languages and other configs
    reader = easyocr.Reader(['en'], gpu=False)

    IacH, IacW = img.shape
    IapW, IapH = int(div[0]), int(div[1])

    Rw = IacW / IapW
    Rh = IacH / IapH
    # print(Rw,Rh)

    output = ""
    os.system("echo processing start")
    if len(rois) > 1:

        for i in range(int(len(rois) / 4)):
            j = i * 4
            global X1, X2, Y1, Y2
            try:
                x1, y1, x2, y2 = int(rois[j]), int(rois[j + 1]), int(rois[j + 2]), int(rois[j + 3])
                X1, Y1, X2, Y2 = int(Rw * x1), Rh * y1, int(Rw * x2), Rh * y2
            except:
                return ["Sorry something went wrong. Perhaps you did not select your image. Please try again."]   
            Offset_y1 = ((IacH * y1 / 100) * 0.02) / 100
            Offset_y2 = ((IacH * y2 / 100) * 0.05) / 100
            print(Offset_y1)
            Y1 = int(Y1)
            Y2 = int(Y2 + Offset_y2)
            img_ROI = img[Y1:Y2, X1:X2]
            result = reader.readtext(img_ROI, paragraph='True')

            for res in result:
                top_left = tuple(res[0][0])  # top left coordinates as tuple
                bottom_right = tuple(res[0][2])  # bottom right coordinates as tuple
                # draw rectangle on image
                cv2.rectangle(img_ROI, top_left, bottom_right, (0, 255, 0), 2)
                # write recognized text on image (top_left) minus 10 pixel on y
                cv2.putText(img_ROI, res[1], (top_left[0], top_left[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                            (255, 0, 0, 1))
                output = output + res[1] + '\n\n'
                print(output)
                cv2.rectangle(img_ROI, (X1, Y1), (X2, Y2), (255, 0, 255), 2)

    else:
        print((int(img.shape[1]*0.65),int(img.shape[0]*0.65)))
        img = cv2.resize(img,(int(img.shape[1]*0.65),int(img.shape[0]*0.65)))
        result = reader.readtext(img, paragraph='True')
        print(result, "Result")
        # iterate on all results
        for res in result:
            top_left = tuple(res[0][0])  # top left coordinates as tuple
            bottom_right = tuple(res[0][2])  # bottom right coordinates as tuple
            # draw rectangle on image
            top_left = (int(top_left[0]), int(top_left[1]))
            bottom_right = (int(bottom_right[0]), int(bottom_right[1]))
            cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)
            # write recognized text on image (top_left) minus 10 pixel on y
            # cv2.putText(img, res[1], (top_left[0], top_left[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            output = output + res[1]

    print('ok')
    os.remove(secure_filename(file.filename))
    os.system("echo processing done!")
    return [output]


if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0",port=5000)