diff --git a/README.md b/README.md index fd4e431..429c251 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ docker run -p 9898:9898 -d ocr_server:v1 # 2、OCR/目标检测请求接口格式: # http://{host}:{port}/{opt}/{img_type}/{ret_type} -# opt:操作类型 ocr=OCR det=目标检测 +# opt:操作类型 ocr=OCR det=目标检测 slide=滑块(match和compare两种算法,默认为compare) # img_type: 数据类型 file=文件上传方式 b64=base64(imgbyte)方式 默认为file方式 # ret_type: 返回类型 json=返回json(识别出错会在msg里返回错误信息) text=返回文本格式(识别出错时回直接返回空文本) @@ -78,4 +78,8 @@ docker run -p 9898:9898 -d ocr_server:v1 # 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()) +# 滑块识别请求 +# resp = requests.post("http://{host}:{port}/slide/match/", files={'target_img': target_bytes, 'bg_img': bg_bytes}) +# jsonstr = json.dumps({'target_img': target_b64str, 'bg_img': bg_b64str}) +# resp = requests.post("http://{host}:{port}/slide/compare/b64", files=base64.b64encode(jsonstr.encode()).decode()) ``` diff --git a/compare_bg.jpg b/compare_bg.jpg new file mode 100644 index 0000000..1a99db5 Binary files /dev/null and b/compare_bg.jpg differ diff --git a/compare_target.jpg b/compare_target.jpg new file mode 100644 index 0000000..b2d83dc Binary files /dev/null and b/compare_target.jpg differ diff --git a/match_bg.png b/match_bg.png new file mode 100644 index 0000000..adb3625 Binary files /dev/null and b/match_bg.png differ diff --git a/match_target.png b/match_target.png new file mode 100644 index 0000000..56cd1f7 Binary files /dev/null and b/match_target.png differ diff --git a/ocr_server.py b/ocr_server.py index 7daa5e5..b3fe8f0 100644 --- a/ocr_server.py +++ b/ocr_server.py @@ -52,15 +52,28 @@ class Server(object): else: raise Exception("目标检测模块模块未开启") + def slide(self, target_img: bytes, bg_img: bytes, algo_type: str): + dddd = self.ocr or self.det or ddddocr.DdddOcr(ocr=False) + if algo_type == 'match': + return dddd.slide_match(target_img, bg_img) + elif algo_type == 'compare': + return dddd.slide_comparison(target_img, bg_img) + else: + raise Exception(f"不支持的滑块算法类型: {algo_type}") server = Server(ocr=args.ocr, det=args.det, old=args.old) -def get_img(request, img_type='file'): +def get_img(request, img_type='file', img_name='image'): if img_type == 'b64': - img = base64.b64decode(request.stream.read().decode()) + img = base64.b64decode(request.get_data()) # + try: # json str of multiple images + dic = json.loads(img) + img = base64.b64decode(dic.get(img_name).encode()) + except Exception as e: # just base64 of single image + pass else: - img = request.files.get('image').read() + img = request.files.get(img_name).read() return img @@ -93,6 +106,16 @@ def ocr(opt, img_type='file', ret_type='text'): except Exception as e: return set_ret(e, ret_type) +@app.route('/slide//', methods=['POST']) +@app.route('/slide///', methods=['POST']) +def slide(algo_type='compare', img_type='file', ret_type='text'): + try: + target_img = get_img(request, img_type, 'target_img') + bg_img = get_img(request, img_type, 'bg_img') + result = server.slide(target_img, bg_img, algo_type) + return set_ret(result, ret_type) + except Exception as e: + return set_ret(e, ret_type) @app.route('/ping', methods=['GET']) def ping(): diff --git a/test_api.py b/test_api.py index 3c38f4e..ba77d7d 100644 --- a/test_api.py +++ b/test_api.py @@ -8,12 +8,12 @@ # @File : test_api.py # @Software: PyCharm import base64 - +import json import requests print(' ') # ******************OCR识别部分开始****************** -# host = "http://127.0.0.1:9898" +host = "http://127.0.0.1:9898" # 目标检测就把ocr改成det,其他相同 # 方式一 file = open(r'test.jpg', 'rb').read() @@ -44,6 +44,52 @@ api_url = f"{host}/det/file/json" resp = requests.post(api_url, files={'image': file}) print(f"{api_url=}, {resp.text=}") +# 滑块识别 + +target_file = open(r'match_target.png', 'rb').read() +bg_file = open(r'match_bg.png', 'rb').read() + +api_url = f"{host}/slide/match/file" +resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file}) +print(f"{api_url=}, {resp.text=}") + +api_url = f"{host}/slide/match/file/json" +resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file}) +print(f"{api_url=}, {resp.text=}") + +api_url = f"{host}/slide/match/b64" +target_b64str = base64.b64encode(target_file).decode() +bg_b64str = base64.b64encode(bg_file).decode() +jsonstr = json.dumps({'target_img': target_b64str, 'bg_img': bg_b64str}) +resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode()) +print(f"{api_url=}, {resp.text=}") + +api_url = f"{host}/slide/match/b64/json" +resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode()) +print(f"{api_url=}, {resp.text=}") + +target_file = open(r'compare_target.jpg', 'rb').read() +bg_file = open(r'compare_bg.jpg', 'rb').read() + +api_url = f"{host}/slide/compare/file" +resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file}) +print(f"{api_url=}, {resp.text=}") + +api_url = f"{host}/slide/compare/file/json" +resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file}) +print(f"{api_url=}, {resp.text=}") + +api_url = f"{host}/slide/compare/b64" +target_b64str = base64.b64encode(target_file).decode() +bg_b64str = base64.b64encode(bg_file).decode() +jsonstr = json.dumps({'target_img': target_b64str, 'bg_img': bg_b64str}) +resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode()) +print(f"{api_url=}, {resp.text=}") + +api_url = f"{host}/slide/compare/b64/json" +resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode()) +print(f"{api_url=}, {resp.text=}") + # 方式二 # 获取验证码图片