mirror of
https://github.com/sml2h3/ocr_api_server.git
synced 2025-05-03 02:10:36 +08:00
Merge pull request #1 from NowAnti/main
支持请求格式(b64/file)与返回格式(text/json)的任意组合使用
This commit is contained in:
commit
6be998dc2c
17
README.md
17
README.md
@ -23,7 +23,7 @@ pip install -r requirements.txt -i https://pypi.douban.com/simple
|
||||
python ocr_server.py --port 9898 --ocr
|
||||
|
||||
# 开启ocr模块并使用旧模型计算
|
||||
python ocr_server.py --port 9898 --ocr -old
|
||||
python ocr_server.py --port 9898 --ocr --old
|
||||
|
||||
# 只开启目标检测模块
|
||||
python ocr_server.py --port 9898 --det
|
||||
@ -61,12 +61,21 @@ docker run -p 9898:9898 -d ocr_server:v1
|
||||
```python
|
||||
# 1、测试是否启动成功,可以通过直接GET访问http://{host}:{port}/ping来测试,如果返回pong则启动成功
|
||||
|
||||
# 2、OCR请求
|
||||
# 2、OCR/目标检测请求接口格式:
|
||||
|
||||
# http://{host}:{port}/{opt}/{img_type}/{ret_type}
|
||||
# opt:操作类型 ocr=OCR det=目标检测
|
||||
# img_type: 数据类型 file=文件上传方式 b64=base64(imgbyte)方式 默认为file方式
|
||||
# ret_type: 返回类型 json=返回json(识别出错会在msg里返回错误信息) text=返回文本格式(识别出错时回直接返回空文本)
|
||||
|
||||
# 例子:
|
||||
|
||||
# OCR请求
|
||||
# resp = requests.post("http://{host}:{port}/ocr", files={'image': image_bytes})
|
||||
# resp = requests.post("http://{host}:{port}/ocr/b64/text", data=base64.b64encode(file).decode())
|
||||
|
||||
# 3、目标检测请求
|
||||
|
||||
# 目标检测请求
|
||||
# resp = requests.post("http://{host}:{port}/det", files={'image': image_bytes})
|
||||
# resp = requests.post("http://{host}:{port}/ocr/b64/json", data=base64.b64encode(file).decode())
|
||||
|
||||
```
|
||||
|
@ -1,5 +1,6 @@
|
||||
# encoding=utf-8
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
|
||||
import ddddocr
|
||||
@ -39,7 +40,6 @@ class Server(object):
|
||||
else:
|
||||
print("目标检测模块未开启,如需要使用,请使用参数 --det开启")
|
||||
|
||||
|
||||
def classification(self, img: bytes):
|
||||
if self.ocr_option:
|
||||
return self.ocr.classification(img)
|
||||
@ -56,23 +56,42 @@ class Server(object):
|
||||
server = Server(ocr=args.ocr, det=args.det, old=args.old)
|
||||
|
||||
|
||||
@app.route('/ocr', methods=['POST'])
|
||||
def ocr():
|
||||
try:
|
||||
def get_img(request, img_type='file'):
|
||||
if img_type == 'b64':
|
||||
img = base64.b64decode(request.stream.read().decode())
|
||||
else:
|
||||
img = request.files.get('image').read()
|
||||
r = server.classification(img)
|
||||
return json.dumps({"status": "200", "result": str(r), "msg": ""})
|
||||
except Exception as e:
|
||||
return json.dumps({"status": "500", "result": "", "msg": str(e)})
|
||||
return img
|
||||
|
||||
@app.route('/det', methods=['POST'])
|
||||
def det():
|
||||
|
||||
def set_ret(result, ret_type='text'):
|
||||
if ret_type == 'json':
|
||||
if isinstance(result, Exception):
|
||||
return json.dumps({"status": 200, "result": "", "msg": str(result)})
|
||||
else:
|
||||
return json.dumps({"status": 200, "result": result, "msg": ""})
|
||||
# return json.dumps({"succ": isinstance(result, str), "result": str(result)})
|
||||
else:
|
||||
if isinstance(result, Exception):
|
||||
return ''
|
||||
else:
|
||||
return str(result).strip()
|
||||
|
||||
|
||||
@app.route('/<opt>/<img_type>', methods=['POST'])
|
||||
@app.route('/<opt>/<img_type>/<ret_type>', methods=['POST'])
|
||||
def ocr(opt, img_type='file', ret_type='text'):
|
||||
try:
|
||||
img = request.files.get('image').read()
|
||||
r = server.detection(img)
|
||||
return json.dumps({"status": "200", "result": r, "msg": ""})
|
||||
img = get_img(request, img_type)
|
||||
if opt == 'ocr':
|
||||
result = server.classification(img)
|
||||
elif opt == 'det':
|
||||
result = server.detection(img)
|
||||
else:
|
||||
raise f"<opt={opt}> is invalid"
|
||||
return set_ret(result, ret_type)
|
||||
except Exception as e:
|
||||
return json.dumps({"status": "500", "result": "", "msg": str(e)})
|
||||
return set_ret(e, ret_type)
|
||||
|
||||
|
||||
@app.route('/ping', methods=['GET'])
|
||||
|
35
test_api.py
35
test_api.py
@ -7,17 +7,40 @@
|
||||
# @Email : sml2h3@gmail.com
|
||||
# @File : test_api.py
|
||||
# @Software: PyCharm
|
||||
import base64
|
||||
|
||||
import requests
|
||||
|
||||
print(' ')
|
||||
# ******************OCR识别部分开始******************
|
||||
# host = "http://127.0.0.1:9898"
|
||||
# 目标检测就把ocr改成det,其他相同
|
||||
api_url = "http://10.0.20.198:9898/ocr"
|
||||
|
||||
# 方式一
|
||||
# file = open(r'test.jpg', 'rb').read()
|
||||
#
|
||||
# resp = requests.post(api_url, files={'image': file})
|
||||
# print(resp.text)
|
||||
file = open(r'test.jpg', 'rb').read()
|
||||
|
||||
api_url = f"{host}/ocr/file"
|
||||
resp = requests.post(api_url, files={'image': file})
|
||||
print(f"{api_url=}, {resp.text=}")
|
||||
|
||||
api_url = f"{host}/ocr/file/json"
|
||||
resp = requests.post(api_url, files={'image': file})
|
||||
print(f"{api_url=}, {resp.text=}")
|
||||
|
||||
api_url = f"{host}/ocr/b64"
|
||||
resp = requests.post(api_url, data=base64.b64encode(file).decode())
|
||||
print(f"{api_url=}, {resp.text=}")
|
||||
|
||||
api_url = f"{host}/ocr/b64/json"
|
||||
resp = requests.post(api_url, data=base64.b64encode(file).decode())
|
||||
print(f"{api_url=}, {resp.text=}")
|
||||
|
||||
api_url = f"{host}/det/file"
|
||||
resp = requests.post(api_url, files={'image': file})
|
||||
print(f"{api_url=}, {resp.text=}")
|
||||
|
||||
api_url = f"{host}/det/file/json"
|
||||
resp = requests.post(api_url, files={'image': file})
|
||||
print(f"{api_url=}, {resp.text=}")
|
||||
|
||||
# 方式二
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user