-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWEB_MAIN.py
243 lines (199 loc) · 7.66 KB
/
WEB_MAIN.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# -*- coding: utf-8 -*-
import base64
import json
import time
import bson
import cv2
import flask
import numpy
from PIL import Image
from colorama import init, Fore, Back
init(autoreset=True)
from bson.json_util import dumps
from flask import Flask, request, Response, jsonify, redirect, json, flash, abort
from numpy import rint
from pymongo import MongoClient
from werkzeug.utils import secure_filename
import shutil
import os
import PRE_pross
# import MAIN_PROSS
import MAIN_PROSS_re
app = Flask(__name__, static_url_path="")
app.secret_key = '123456'
# 读取mongoDB配置文件
app.config.from_pyfile('conf/flask_config.cfg')
# 连接数据库,并获取数据库对象
db = MongoClient(app.config['DB_HOST'], app.config['DB_PORT']).HOS_REPO
# 将矫正后图片与图片识别结果(JSON)存入数据库
def save_file(file_str, f, report_data, db_data):
# content = StringIO(file_str)
content = file_str
try:
mime = 'jpeg'
print('content of mime is:', mime)
if mime not in app.config['ALLOWED_EXTENSIONS']:
raise IOError()
except IOError:
abort(400)
c = dict(report_data=report_data, content=file_str, filename=secure_filename(f.name),
mime=mime, repo_db=db_data)
db.files.save(c)
return c['_id'], c['filename']
@app.route('/', methods=['GET', 'POST'])
def index():
return redirect('/index.html')
# @app.route('/upload', methods=['POST'])
@app.route('/upload/<name1>,<name2>', methods=['POST'])
def name(name1, name2):
return name1 + ' ' + name2
def upload():
time_start = time.time()
if request.method == 'POST':
if 'imagefile' not in request.files:
flash('图片格式不支持')
return jsonify({"error": "图片格式不支持"})
imgfile = request.files['imagefile']
if imgfile.filename == '':
flash('未选择图片')
return jsonify({"error": "未选择图片"})
if imgfile:
# pil = StringIO(imgfile)
# pil = Image.open(pil)
# print 'imgfile:', imgfile
img = cv2.imdecode(numpy.frombuffer(imgfile.read(), numpy.uint8), cv2.IMREAD_COLOR)
'''
cv2.imwrite("tes.jpg", img)
img2 = cv2.imread('tes.jpg')
# todo 是否需要本地存储中转
'''
'''
report_data_str = MAIN_PROSS.main_pross(cvimg=img,
demo_or_not=demo_or_not,
hospital_lock=False,
report_type_lock=False)
'''
report_data_str = MAIN_PROSS_re.main_pross(cvimg=img,
demo_or_not=demo_or_not,
hospital_lock=False,
report_type_lock=False)
# 判断是否报错,中文开头为错误
err_or_not = PRE_pross.charactor_match_chinese_head(report_data_str)
if err_or_not is True:
print(Back.RED + '******')
print(report_data_str)
data = {
'error': report_data_str,
}
flash(report_data_str)
return jsonify(data)
# todo 开始更改
path_img_toDB = 'temp/region.jpg'
img = cv2.imread(path_img_toDB)
w = img.shape[1]
h = img.shape[0]
with open('test_data.json', 'r') as f:
db_data = json.load(f)
with open(path_img_toDB, "rb") as f:
if f is None:
pass
rint('Error! f is None!')
else:
'''
定义file_str存储矫正后的图片文件f的内容(str格式),方便之后将图片内容存储至数据库中
'''
img_file_str = f.read()
# file_str = MAIN_PROSS.cv2_to_base64(cv2.imread(path_img_toDB))
try:
fid, filename = save_file(img_file_str, f, report_data_str, db_data)
except:
print(Back.RED + 'DB离线')
data = {
'error': '错误:MongoDB离线',
}
flash(report_data_str)
return jsonify(data)
print('fid:', fid)
if fid is not None:
# 假设锁定网页显示高度为512:h=512, 所以w=512*(w/h)
display_height = 512
templates = f"<div align='center'><img id=\'filtered-report\' src=\'/file/%s\' class=\'file-preview-image\' width=\'{int(display_height * (w / h))}\' height=\'{display_height}\'></div>" % (
fid)
data = {
"templates": templates,
}
time_end = time.time()
print('本次用时', time_end - time_start)
return jsonify(data)
# return render_template("result.html", filename=filename, fileid=fid)
# return render_template("error.html", errormessage="No POST methods")
return jsonify({"error": "No POST methods"})
'''
根据图像oid,在mongodb中查询,并返回Binary对象
'''
@app.route('/file/<fid>')
def find_file(fid):
try:
file = db.files.find_one(bson.objectid.ObjectId(fid))
if file is None:
raise bson.errors.InvalidId()
return Response(file['content'], mimetype='image/' + file['mime'])
except bson.errors.InvalidId:
flask.abort(404)
'''
直接从数据库中取出之前识别好的JSON数据,并且用bson.json_util.dumps将其从BSON转换为JSON格式的str类型
'''
@app.route('/report/<fid>')
def get_report(fid):
# print 'get_report(fid):', fid
try:
file = db.files.find_one(bson.objectid.ObjectId(fid))
if file is None:
raise bson.errors.InvalidId()
print('type before transform:\n', type(file['report_data']))
report_data = bson.json_util.dumps(file['report_data'])
print('type after transform:\n', type(report_data))
if report_data is None:
print('report_data is NONE! Error!!!!')
return jsonify({"error": "can't ocr'"})
return jsonify(report_data)
except bson.errors.InvalidId:
flask.abort(404)
'''
def update_report(fid, ss):
# load json example
with open('bloodtestdata.json') as json_file:
data = json.load(json_file)
for i in range(22):
data['bloodtest'][i]['value'] = ss[i]
json_data = json.dumps(data, ensure_ascii=False, indent=4)
db.files.update_one({
'_id': bson.objectid.ObjectId(fid)}, {
'$set': {
'report_data': json_data
}
}, upsert=False)
file = db.files.find_one(bson.objectid.ObjectId(fid))
report_data = bson.json_util.dumps(file['report_data'])
print(report_data)
'''
if __name__ == '__main__':
def run():
# 检查并重置工作文件夹
try:
shutil.rmtree('temp')
os.mkdir('temp/ocr_result')
os.mkdir('temp/DEMO')
os.mkdir('temp/DEMO/mask')
except:
os.mkdir('temp')
os.mkdir('temp/DEMO')
os.mkdir('temp/ocr_result')
os.mkdir('temp/DEMO/mask')
'''
# 启动hub(不支持网络,废弃)
module = hub.Module(name="chinese_ocr_db_crnn_server")
'''
app.run(host=app.config['SERVER_HOST'], port=app.config['SERVER_PORT'])
demo_or_not = 1
run()