-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathflaskOcrDz.py
More file actions
148 lines (132 loc) · 4.31 KB
/
Copy pathflaskOcrDz.py
File metadata and controls
148 lines (132 loc) · 4.31 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : flaskOcrDz.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2021/11/1
import json
from flask import Flask, jsonify, request,Response
import requests
import ddddocr
ocr = ddddocr.DdddOcr()
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False # jsonify返回的中文正常显示
@app.route("/",methods=['GET'])
def index():
return '欢迎使用简单验证码文字识别,海阔视界道长专用'
def hexStringTobytes(str):
str = str.replace(" ", "")
print(str)
return bytes.fromhex(str)
def bytesToHexString(bs):
return ''.join(['%02X ' % b for b in bs])
class LocalOcr:
def __init__(self,file=None,url='http://127.0.0.1:10000'):
self.file = file
self.url = url
self.yzm = self.yzm_ocr(0)
def read(self):
return self.yzm
def yzm_ocr(self,count=0):
try:
r = requests.post(self.url, data=self.file,timeout=(0.5,2))
yzm = r.text
return yzm
except:
if count < 3:
count += 1
return self.yzm_ocr(count)
else:
return ""
def ocr2(hex):
if type(hex) == list:
hex = ''.join(hex)
img_bytes = hexStringTobytes(hex)
# print(img_bytes)
# with open('1.png','wb+') as f:
# f.write(img_bytes)
dm_url = 'http://dm.mudery.com:10000'
im_ocr = LocalOcr(img_bytes, dm_url)
yzm = im_ocr.read()
ret = {'msg': 'ok','ret':yzm,'code':0,'detail':'验证码识别成功'}
print(ret)
return jsonify(ret)
def docr(hex):
if type(hex) == list:
hex = ''.join(hex)
img_bytes = hexStringTobytes(hex)
try:
img_str = img_bytes.decode("latin1")
if img_str.find('html') > -1:
ret = {'msg': '拜托,我收到你传过来的数据是个网页而不是图片,麻烦解密后把图片的hex给我', 'ret': img_str[:500], 'code': -1, 'detail': '图片识别失败'}
return jsonify(ret)
else:
res = ocr.classification(img_bytes)
ret = {'msg': 'ok', 'ret': res, 'code': 0, 'detail': '图片识别成功'}
# print(ret)
return jsonify(ret)
except Exception as e:
# print(f'{e}')
ret = {'msg': 'error', 'ret': f'{e}', 'code': -2, 'detail': '发生了意外的错误'}
return ret
@app.route("/api/ocr",methods=['GET', 'POST'])
def ocr_fast():
args = {}
try:
ctp = request.content_type
if request.method == 'POST':
if ctp.find('application/json') > -1:
try:
args = request.json
except Exception as e:
# args = request.get_data(as_text=True)
args = {}
else:
args = request.form
elif request.method == 'GET':
args = request.args
if not args.get('hex'):
return '缺少必传参数:hex!'
except Exception as e:
return jsonify({'msg':'非法调用','code':'-1'})
# print(args.get('hex'))
# return ocr2(args.get('hex'))
return docr(args.get('hex'))
@app.route("/api/hex2img",methods=['GET'])
def ocr_hex2img():
try:
args = request.args
if not args.get('hex'):
return '缺少必传参数:hex!'
except Exception as e:
return jsonify({'msg':'非法调用','code':'-1'})
# print(args.get('hex'))
# return ocr2(args.get('hex'))
hex = args.get('hex')
if type(hex) == list:
hex = ''.join(hex)
img_bytes = hexStringTobytes(hex)
resp = Response(img_bytes, mimetype='image/jpeg')
return resp
@app.route("/api/ocr_img",methods=['POST'])
def ocr_img_fast():
# print(request.values)
# print(request.files)
# print(request.data)
try:
img_bytes = request.data
ret = ocr.classification(img_bytes)
return ret
# return jsonify({'ret':ret,'code':0,'msg':'识别完毕'})
except Exception as e:
return ''
# return jsonify({'msg':'请求出错','code':-1,'detail':f'{e}'})
def test():
pic = 'yzm1.png'
# pic = '2.png'
with open(pic, 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=9000)
# test()