Sergey Mikhno
commited on
Commit
·
ed533ad
1
Parent(s):
8a7e11b
blocks
Browse files- app/app.py +9 -9
app/app.py
CHANGED
|
@@ -5,7 +5,7 @@ import torch
|
|
| 5 |
import spaces
|
| 6 |
import cv2
|
| 7 |
from doctr.io import DocumentFile
|
| 8 |
-
from doctr.models import
|
| 9 |
|
| 10 |
import os
|
| 11 |
import json
|
|
@@ -22,7 +22,8 @@ app = Flask(__name__)
|
|
| 22 |
app.secret_key = "super secret key"
|
| 23 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def allowed_file(filename):
|
|
@@ -60,17 +61,16 @@ def doc_layout():
|
|
| 60 |
xyxy = [a.tolist() for a in det_res[0].boxes.xyxy]
|
| 61 |
res = [{"coords": y, "type": x} for x, y in zip(blocknames, xyxy)]
|
| 62 |
pimg = cv2.imread(filename, 0)
|
| 63 |
-
|
|
|
|
| 64 |
if r["type"] == "plain text":
|
| 65 |
x, y, x1, y1 = r["coords"]
|
| 66 |
x, y, x1, y1 = int(x), int(y), int(x1), int(y1)
|
| 67 |
-
block = pimg[y:y1,x:x1]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
result = doctr_model(single_img_doc)
|
| 71 |
-
print(result)
|
| 72 |
|
| 73 |
-
return json.dumps(
|
| 74 |
return '''
|
| 75 |
<!doctype html>
|
| 76 |
<title>Upload new File</title>
|
|
|
|
| 5 |
import spaces
|
| 6 |
import cv2
|
| 7 |
from doctr.io import DocumentFile
|
| 8 |
+
from doctr.models import detection_predictor
|
| 9 |
|
| 10 |
import os
|
| 11 |
import json
|
|
|
|
| 22 |
app.secret_key = "super secret key"
|
| 23 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 24 |
|
| 25 |
+
|
| 26 |
+
doctr_model = detection_predictor(arch='db_resnet50', pretrained=True)
|
| 27 |
|
| 28 |
|
| 29 |
def allowed_file(filename):
|
|
|
|
| 61 |
xyxy = [a.tolist() for a in det_res[0].boxes.xyxy]
|
| 62 |
res = [{"coords": y, "type": x} for x, y in zip(blocknames, xyxy)]
|
| 63 |
pimg = cv2.imread(filename, 0)
|
| 64 |
+
blocks = dict()
|
| 65 |
+
for i, r in enumerate(res):
|
| 66 |
if r["type"] == "plain text":
|
| 67 |
x, y, x1, y1 = r["coords"]
|
| 68 |
x, y, x1, y1 = int(x), int(y), int(x1), int(y1)
|
| 69 |
+
block = pimg[y:y1, x:x1]
|
| 70 |
+
result = doctr_model(block)
|
| 71 |
+
blocks["block" + str(i)] = result[0]["words"].tolist()
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
return json.dumps(blocks)
|
| 74 |
return '''
|
| 75 |
<!doctype html>
|
| 76 |
<title>Upload new File</title>
|