-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_wired.py
67 lines (52 loc) · 1.9 KB
/
demo_wired.py
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
# -*- encoding: utf-8 -*-
import os
import tempfile
from wired_table_rec import WiredTableRecognition
import cv2
from flask_cors import CORS
from flask import Flask, request, send_file
import time
# table_rec = WiredTableRecognition()
# img_path = "D:\\招标参数(一次)-13824092815_12.jpg"
# img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)
# table_str, elapse, bbox = table_rec(img_path)
# print(table_str)
# print(elapse)
# print(bbox)
# for bbox in bbox:
# for line in bbox:
# x1, y1 = int(line[0]), int(line[1])
# x2 = int(bbox[1][0])
# y2 = int(bbox[3][1])
# cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# cv2.imwrite('D://rest.bmp', img)
app = Flask(__name__)
app.json.sort_keys = False
CORS(app, supports_credentials=True)
# 在全局范围内创建并配置 WiredTableRecognition 实例以使用 GPU
table_rec = WiredTableRecognition()
@app.route('/pdf-to-image', methods=['POST'])
def pdf_to_image():
uploaded_file = request.files['img']
# 保存文件到临时位置
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
uploaded_file.save(tmp_file)
tmp_file_path = tmp_file.name
# 使用 OpenCV 读取图像
img = cv2.imread(tmp_file_path)
start_time = time.time()
table_str, elapse, bbox = table_rec(img)
print(elapse)
# 处理并输出边界框
for bbox in bbox:
for line in bbox:
x1, y1 = int(line[0]), int(line[1])
x2, y2 = int(bbox[1][0]), int(bbox[3][1])
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
# 保存带边界框的图像到临时文件
output_file_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
cv2.imwrite(output_file_path, img)
# 发送图像文件
return send_file(output_file_path, mimetype='image/png')
if __name__ == "__main__":
app.run("0.0.0.0", 5200)