From 1d3ab64e1066e817c58a1826b1325ae935707b9a Mon Sep 17 00:00:00 2001 From: MoeShin Date: Sun, 2 Apr 2023 11:22:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8F=AF=E4=B8=8A=E4=BC=A0=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index 702a0c6..e1f6e7c 100644 --- a/app.py +++ b/app.py @@ -20,6 +20,8 @@ app = Flask(__name__) app.config['JSON_AS_ASCII'] = False +enable_image_path = True + # 失败的返回 def jsonFail(message): @@ -71,19 +73,19 @@ def ocrResultSort(ocr_result): # ocr解析 -def ocrProcess(imgPath, language): +def ocrProcess(img, language): if language == "JAP": - result = japOcr.ocr(imgPath, cls=False) + result = japOcr.ocr(img, cls=False) elif language == "ENG": - result = engOcr.ocr(imgPath, cls=False) + result = engOcr.ocr(img, cls=False) elif language == "KOR": - result = korOcr.ocr(imgPath, cls=False) + result = korOcr.ocr(img, cls=False) elif language == "RU": - result = ruOcr.ocr(imgPath, cls=False) + result = ruOcr.ocr(img, cls=False) elif language == "ZH": - result = zhOcr.ocr(imgPath, cls=False) + result = zhOcr.ocr(img, cls=False) else: - result = japOcr.ocr(imgPath, cls=False) + result = japOcr.ocr(img, cls=False) try: result = ocrResultSort(result) @@ -122,14 +124,22 @@ def handle_request(): }) try: - post_data = request.get_data() - post_data = json.loads(post_data.decode("utf-8")) - - languageList = ["JAP", "ENG", "KOR", "RU", "ZH"] - if post_data["Language"] not in languageList: - return jsonFail("Language {} doesn't exist".format(post_data["Language"])) - - res = ocrProcess(post_data["ImagePath"], post_data["Language"]) + if request.mimetype == "multipart/form-data": + post_data = request.form + image = request.files["image"].stream.read() + else: + if not enable_image_path: + return jsonFail("Disable image path") + post_data = request.get_data() + post_data = json.loads(post_data.decode("utf-8")) + image = post_data["ImagePath"] + language = post_data["Language"] + + language_list = ["JAP", "ENG", "KOR", "RU", "ZH"] + if language not in language_list: + return jsonFail("Language {} doesn't exist".format(language)) + + res = ocrProcess(image, language) return jsonSuccess(res) except Exception as err: @@ -145,12 +155,16 @@ def handle_request(): parser.add_argument("-h", "--host", type=str, default=host, help="监听的主机。默认:\"%s\"" % host) parser.add_argument("-p", "--port", type=int, default=port, help="监听的端口。默认:%d" % port) parser.add_argument("-P", "--path", type=str, default=path, help="监听的路径。默认:\"%s\"" % path) + parser.add_argument("-d", "--disable-image-path", type=bool, default=False, help="禁止图片路径。") parser.add_argument('--help', action='help', help='打印帮助。') args = parser.parse_args() host = args.host port = args.port path = args.path - print("监听:http://%s:%d%s" % (host, port, path)) + enable_image_path = not args.disable_image_path + + print('是否允许图片路径:%s' % enable_image_path) + print("接口:http://%s:%d%s" % (host, port, path)) app.add_url_rule(path, view_func=handle_request, methods=["POST", "HEAD"]) app.run(debug=False, host=host, port=port, threaded=False) From 97ec2b372c339c6fa7a8147aeedf05d0ac1be12c Mon Sep 17 00:00:00 2001 From: MoeShin Date: Sun, 2 Apr 2023 11:35:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index e1f6e7c..0e6c0a1 100644 --- a/app.py +++ b/app.py @@ -155,7 +155,7 @@ def handle_request(): parser.add_argument("-h", "--host", type=str, default=host, help="监听的主机。默认:\"%s\"" % host) parser.add_argument("-p", "--port", type=int, default=port, help="监听的端口。默认:%d" % port) parser.add_argument("-P", "--path", type=str, default=path, help="监听的路径。默认:\"%s\"" % path) - parser.add_argument("-d", "--disable-image-path", type=bool, default=False, help="禁止图片路径。") + parser.add_argument("-d", "--disable-image-path", action="store_true", help="禁止图片路径。") parser.add_argument('--help', action='help', help='打印帮助。') args = parser.parse_args() From 33b5efea08a04f9cec14d6d68e5462870f33d89a Mon Sep 17 00:00:00 2001 From: MoeShin Date: Tue, 4 Apr 2023 08:37:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=90=8D=E7=9A=84=E6=A0=BC=E5=BC=8F=E7=BB=9F=E4=B8=80=E4=B8=BA?= =?UTF-8?q?=E5=A4=A7=E9=A9=BC=E5=B3=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 0e6c0a1..f46654b 100644 --- a/app.py +++ b/app.py @@ -126,7 +126,7 @@ def handle_request(): try: if request.mimetype == "multipart/form-data": post_data = request.form - image = request.files["image"].stream.read() + image = request.files["Image"].stream.read() else: if not enable_image_path: return jsonFail("Disable image path")