-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.py
181 lines (145 loc) · 4.87 KB
/
app.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
"""
File: app.py
Project: poirot
File Created: Wednesday, 25th August 2021 3:19:54 pm
Author: harumonia (zxjlm233@gmail.com)
-----
Last Modified: Wednesday, 25th August 2021 4:48:51 pm
Modified By: harumonia (zxjlm233@gmail.com>)
-----
Copyright 2020 - 2021 Node Supply Chain Manager Corporation Limited
-----
Description:
"""
import re
import time
from threading import Lock
import config
import os
from flask_cors import CORS
from flask_socketio import SocketIO, emit, disconnect
from flask import Flask, request, jsonify, render_template, \
copy_current_request_context, session
from utils import ocr_processor, check_file, ocr_func
from progress import SocketQueue, ProgressBar
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
app.config['SECRET_KEY'] = 'Poirot%%harumonia'
socketio = SocketIO()
socketio.init_app(app, cors_allowed_origins="*")
thread = None
thread_lock = Lock()
@socketio.on('disconnect_request')
def disconnect_request():
"""关闭websocket连接"""
@copy_current_request_context
def can_disconnect():
disconnect()
session['receive_count'] = session.get('receive_count', 0) + 1
# for this emit we use a callback function
# when the callback function is invoked we know that the message has been
# received and it is safe to disconnect
emit('my_response',
{'data': 'Disconnected!', 'count': session['receive_count']},
callback=can_disconnect)
def background_thread():
"""Example of how to send server generated events to clients."""
while True:
socketio.sleep(1)
ret = []
while not SocketQueue.res_queue.empty():
ProgressBar.now_length += 1
ret.append(SocketQueue.res_queue.get())
if ret:
socketio.emit('my_response',
{'data': ret, 'width': str(ProgressBar.calculate()) + '%'})
socketio.emit('my_response',
{'data': ret, 'width': str(ProgressBar.calculate()) + '%'})
@app.route('/')
def index():
"""
返回主页
:return:
"""
return render_template('index.html')
@app.route('/page_pic')
def page_pic():
"""
返回解析图片页面
:return:
"""
return render_template('pic.html')
@app.route('/page_font')
def page_font():
"""
返回解析字体文件页面
:return:
"""
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(background_thread)
return render_template('font.html')
@app.route('/page_digit')
def page_digit():
"""
返回解析字体文件页面
:return:
"""
return render_template('digit.html')
@app.route('/page_instruction')
def page_instruction():
"""
返回使用说明页面
:return:
"""
return render_template('instruction.html')
@app.route('/api/font_file_cracker', methods=['POST'])
def font_file_cracker():
try:
file = request.files.get('font_file')
type_ = request.form.get('type')
except Exception as _e:
return jsonify({'code': 400, 'msg': f'lose args,{_e}', 'res': {}})
filename = re.sub('[(()) ]', '', file.filename)
if not os.path.exists('./font_collection'):
os.mkdir('./font_collection')
file.save('./font_collection/' + filename)
if config.is_online and not check_file('./font_collection/' + filename):
return jsonify({'code': 300, 'msg': 'Please use example file(*^_^*)'})
ProgressBar.init()
res = ocr_processor('./font_collection/' + filename)
if type_ == 'html':
font_dict = {}
for foo in res:
font_dict[foo['name']] = foo['ocr_result']
return jsonify({'code': 200,
'html': render_template('images.html', result=res),
'font_dict': font_dict})
else:
return jsonify({'code': 200, 'msg': 'success', 'res': res})
@app.route('/api/img_cracker_via_local_ocr/', methods=['POST'])
def local_cracker():
"""
接受单个图片,进行本地的ocr,返回图片破解结果
:return:
"""
if config.is_online:
return jsonify(
{'code': 300, 'msg': 'online mode can`t use image cracker'})
img_b64 = request.form['img'].replace('data:image/png;base64,', '')
start_time = time.time()
res = ocr_func(img_b64, 'single_image', request.remote_addr)
return jsonify({'code': 200, 'msg': '成功',
'data': {'raw_out': res,
'speed_time':
round(time.time() - start_time, 2)}})
@app.route('/api/special_for_printed_digits/', methods=['POST'])
def special_for_printed_digits():
"""
针对数字进行特化
Returns:
如果传入的参数中指定了 has_pic_detail为 '0',那么就只返回映射表
默认情况下 has_pic_detail 的值是 '1'。
"""
return jsonify({'code': 200})