|
from paddleocr import PaddleOCR
|
from flask import request, Flask
|
from wsgiref.simple_server import make_server
|
import time
|
import base64
|
import json
|
|
|
class OpenOcr:
|
_ocr = None
|
|
def __int__(self):
|
self._ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=False
|
, rec_model_dir='models/', det_model_dir='models/')
|
# , rec_model_dir='./models/ch_ppocr_server_v2.0_rec_infer/'
|
# , cls_model_dir='./models/ch_ppocr_mobile_v2.0_cls_infer/'
|
# , det_model_dir='./models/ch_ppocr_server_v2.0_det_infer/')
|
print('ocr create success!')
|
|
def start_up(self):
|
self._ocr = PaddleOCR(use_angle_cls=True, lang="ch", use_gpu=False)
|
print('start_up: _ocr created!')
|
|
def recognize(self, image_path):
|
text_block_list = []
|
result = self._ocr.ocr(image_path, cls=True)
|
for item in result:
|
data = {"name": item[1][0], "x1": item[0][0][0], "y1": item[0][0][1], "x2": item[0][1][0], "y2": item[0][1][1], "score": item[1][1]}
|
text_block_list.append(data)
|
# print(item[1][0])
|
# print(item[0][0])
|
# print(item[0][1])
|
# print(item[1][1])
|
return text_block_list
|
|
|
# 1.启动OCR模型
|
openOcr = OpenOcr()
|
openOcr.start_up()
|
|
# 2.启动web服务
|
app = Flask(__name__)
|
# app.config['PORT'] = 8801
|
# save_path = '/opt/soft/fire_images/'
|
save_path = 'd:/dev_tools/ai/'
|
|
|
@app.route('/')
|
def hello_world():
|
return 'Hello World!'
|
|
|
@app.route("/ai/ocr/table", methods=['post'])
|
def request_ocr_table():
|
start_time = time.time()
|
data = json.loads(request.get_data(as_text=True))
|
if 'img' not in data or 'id' not in data:
|
return '参数不正确'
|
request_id = data['id']
|
img_base64 = data['img']
|
if not img_base64 or img_base64 == '':
|
return 'file is needed!'
|
try:
|
# print('')
|
imgdata = base64.b64decode(img_base64)
|
source_file_id = save_path + 'ocr_' + request_id + '.jpg'
|
file = open(source_file_id, 'wb')
|
file.write(imgdata)
|
file.close()
|
# detect_img(source_file_id, score=score, id=requestId)
|
return openOcr.recognize(image_path=source_file_id)
|
|
except Exception as e:
|
print(e)
|
return 'error:' + source_file_id
|
# return 'success'
|
|
|
if __name__ == '__main__':
|
# openOcr = OpenOcr()
|
# openOcr.start_up()
|
# openOcr.recognize(image_path='../doc/imgs/00015504.jpg')
|
# openOcr.recognize(image_path='D:/dev_tools/ai/demo_table_02.jpg')
|
# app.run(host='127.0.0.1', port=8801)
|
server = make_server('127.0.0.1', 8801, app)
|
server.serve_forever()
|