-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr_server.py
147 lines (132 loc) · 5.54 KB
/
ocr_server.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
from flask import Flask, request, jsonify
import ddddocr
import logging
import re
import base64
app = Flask(__name__)
# 设置日志记录
logging.basicConfig(filename='app.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
class CAPTCHA:
def __init__(self):
# 初始化两个识别器,一个用于OCR和滑块,一个用于检测
self.ocr = ddddocr.DdddOcr()
self.det = ddddocr.DdddOcr(det=True)
# 滑块验证码识别函数,接收两个图像和一个simple_target参数,返回滑块的目标位置
def capcode(self, sliding_image, back_image, simple_target):
try:
sliding_bytes = self.get_image_bytes(sliding_image)
back_bytes = self.get_image_bytes(back_image)
res = self.ocr.slide_match(sliding_bytes, back_bytes, simple_target=simple_target)
return res['target'][0]
except Exception as e:
app.logger.error(f"出现错误: {e}")
return None
# OCR识别函数,接收一个图像和一个png_fix参数,返回OCR识别结果
def classification(self, image):
try:
image_bytes = self.get_image_bytes(image)
res = self.ocr.classification(image_bytes)
return res
except Exception as e:
app.logger.error(f"出现错误: {e}")
return None
# 检测函数,接收一个图像,返回图像上的所有文字或图标的坐标位置
def detection(self, image):
try:
image_bytes = self.get_image_bytes(image)
poses = self.det.detection(image_bytes)
return poses
except Exception as e:
app.logger.error(f"出现错误: {e}")
return None
# 计算类验证码处理函数,接收一个图像,返回计算结果
def calculate(self, image):
try:
image_bytes = self.get_image_bytes(image)
expression = self.ocr.classification(image_bytes)
expression = re.sub('=.*$', '', expression)
expression = re.sub('[^0-9+\-*/()]', '', expression)
result = eval(expression)
return result
except Exception as e:
app.logger.error(f"出现错误: {e}")
app.logger.error(f"错误类型: {type(e)}")
app.logger.error(f"错误详细信息: {e.args}")
return None
# 辅助函数,根据输入类型获取图像字节流
def get_image_bytes(self, image_data):
if isinstance(image_data, bytes):
return image_data
elif isinstance(image_data, str):
return base64.b64decode(image_data)
elif hasattr(image_data, 'read'):
return image_data.read()
else:
raise ValueError("Unsupported image data type")
# 初始化CAPTCHA类
captcha = CAPTCHA()
# 滑块验证码识别路由,接收POST请求,返回滑块的目标位置
@app.route('/capcode', methods=['POST'])
def capcode():
try:
data = request.get_json()
sliding_image = data['slidingImage']
back_image = data['backImage']
simple_target = data.get('simpleTarget', True)
result = captcha.capcode(sliding_image, back_image, simple_target)
if result is None:
app.logger.error('处理过程中出现错误.')
return jsonify({'error': '处理过程中出现错误.'}), 500
return jsonify({'result': result})
except Exception as e:
app.logger.error(f"出现错误: {e}")
return jsonify({'error': f"出现错误: {e}"}), 400
# OCR识别路由,接收POST请求,返回OCR识别结果
@app.route('/classification', methods=['POST'])
def classification():
try:
data = request.get_json()
image = data['image']
result = captcha.classification(image)
if result is None:
app.logger.error('处理过程中出现错误.')
return jsonify({'error': '处理过程中出现错误.'}), 500
return jsonify({'result': result})
except Exception as e:
app.logger.error(f"出现错误: {e}")
return jsonify({'error': f"出现错误: {e}"}), 400
# 检测路由,接收POST请求,返回图像上的所有文字或图标的坐标位置
@app.route('/detection', methods=['POST'])
def detection():
try:
data = request.get_json()
image = data['image']
result = captcha.detection(image)
if result is None:
app.logger.error('处理过程中出现错误.')
return jsonify({'error': '处理过程中出现错误.'}), 500
return jsonify({'result': result})
except Exception as e:
app.logger.error(f"出现错误: {e}")
return jsonify({'error': f"出现错误: {e}"}), 400
# 计算类验证码处理路由,接收POST请求,返回计算结果
@app.route('/calculate', methods=['POST'])
def calculate():
try:
data = request.get_json()
image = data['image']
result = captcha.calculate(image)
if result is None:
app.logger.error('处理过程中出现错误.')
return jsonify({'error': '处理过程中出现错误.'}), 500
return jsonify({'result': result})
except Exception as e:
app.logger.error(f"出现错误: {e}")
return jsonify({'error': f"出现错误: {e}"}), 400
# 基本运行状态路由,返回一个表示服务器正常运行的消息
@app.route('/')
def hello_world():
return 'API运行成功!'
# 启动Flask应用
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=3721)