Spaces:
Sleeping
Sleeping
Commit
·
685252d
1
Parent(s):
c80c26c
Add application file
Browse files- Dockerfile +20 -0
- api.py +95 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
RUN apt-get update -y && apt-get install -y build-essential
|
4 |
+
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
ENV FLASK_APP=app.py
|
8 |
+
|
9 |
+
ENV FLASK_DEBUG=development
|
10 |
+
|
11 |
+
COPY api.py api.py
|
12 |
+
|
13 |
+
RUN pip install Flask
|
14 |
+
RUN pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu
|
15 |
+
RUN pip install easyocr
|
16 |
+
RUN pip install gunicorn
|
17 |
+
|
18 |
+
|
19 |
+
CMD ["gunicorn","-b","0.0.0.0:7860", "api:app","--timeout","950"]
|
20 |
+
|
api.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
import datetime
|
3 |
+
from werkzeug.utils import secure_filename
|
4 |
+
import cv2
|
5 |
+
import easyocr
|
6 |
+
import os
|
7 |
+
|
8 |
+
x = datetime.datetime.now()
|
9 |
+
|
10 |
+
# Initializing flask app
|
11 |
+
app = Flask(__name__)
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
@app.route('/data')
|
16 |
+
def index():
|
17 |
+
return ("<h1>Hi, I am running fine as backend. Please use the frontend to perform text extraction.</h1>")
|
18 |
+
|
19 |
+
|
20 |
+
# Route for seeing a data
|
21 |
+
@app.route('/data', methods=['POST'])
|
22 |
+
def get_time():
|
23 |
+
try:
|
24 |
+
file = request.files['file']
|
25 |
+
file.save(secure_filename(file.filename))
|
26 |
+
except:
|
27 |
+
return["Sorry something went wrong. Perhaps you did not select your image. Please try again."]
|
28 |
+
rois = request.form['rois'].split(',')
|
29 |
+
div = request.form['divisionWH'].split(',')
|
30 |
+
|
31 |
+
print(rois, "\n", div)
|
32 |
+
|
33 |
+
img = cv2.imread(secure_filename(file.filename))
|
34 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
35 |
+
# specify languages and other configs
|
36 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
37 |
+
|
38 |
+
IacH, IacW = img.shape
|
39 |
+
IapW, IapH = int(div[0]), int(div[1])
|
40 |
+
|
41 |
+
Rw = IacW / IapW
|
42 |
+
Rh = IacH / IapH
|
43 |
+
# print(Rw,Rh)
|
44 |
+
|
45 |
+
output = ""
|
46 |
+
|
47 |
+
if len(rois) > 1:
|
48 |
+
|
49 |
+
for i in range(int(len(rois) / 4)):
|
50 |
+
j = i * 4
|
51 |
+
global X1, X2, Y1, Y2
|
52 |
+
x1, y1, x2, y2 = int(rois[j]), int(rois[j + 1]), int(rois[j + 2]), int(rois[j + 3])
|
53 |
+
X1, Y1, X2, Y2 = int(Rw * x1), Rh * y1, int(Rw * x2), Rh * y2
|
54 |
+
Offset_y1 = ((IacH * y1 / 100) * 0.02) / 100
|
55 |
+
Offset_y2 = ((IacH * y2 / 100) * 0.05) / 100
|
56 |
+
print(Offset_y1)
|
57 |
+
Y1 = int(Y1)
|
58 |
+
Y2 = int(Y2 + Offset_y2)
|
59 |
+
img_ROI = img[Y1:Y2, X1:X2]
|
60 |
+
result = reader.readtext(img_ROI, paragraph='True')
|
61 |
+
|
62 |
+
for res in result:
|
63 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
64 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
65 |
+
# draw rectangle on image
|
66 |
+
cv2.rectangle(img_ROI, top_left, bottom_right, (0, 255, 0), 2)
|
67 |
+
# write recognized text on image (top_left) minus 10 pixel on y
|
68 |
+
cv2.putText(img_ROI, res[1], (top_left[0], top_left[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
|
69 |
+
(255, 0, 0, 1))
|
70 |
+
output = output + res[1] + '\n\n'
|
71 |
+
print(output)
|
72 |
+
cv2.rectangle(img_ROI, (X1, Y1), (X2, Y2), (255, 0, 255), 2)
|
73 |
+
|
74 |
+
else:
|
75 |
+
result = reader.readtext(img, paragraph='True')
|
76 |
+
print(result, "Result")
|
77 |
+
# iterate on all results
|
78 |
+
for res in result:
|
79 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
80 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
81 |
+
# draw rectangle on image
|
82 |
+
top_left = (int(top_left[0]), int(top_left[1]))
|
83 |
+
bottom_right = (int(bottom_right[0]), int(bottom_right[1]))
|
84 |
+
cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)
|
85 |
+
# write recognized text on image (top_left) minus 10 pixel on y
|
86 |
+
# cv2.putText(img, res[1], (top_left[0], top_left[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
87 |
+
output = output + res[1]
|
88 |
+
|
89 |
+
print('ok')
|
90 |
+
os.remove(secure_filename(file.filename))
|
91 |
+
return [output]
|
92 |
+
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
app.run(debug=True,host="0.0.0.0",port=5000)
|