📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
Add base64 input of api mode.
SWHL390cbd06bea0
diff --git a/ocrweb/README.md b/ocrweb/README.mdindex 850acea..23b2ef5 100755--- a/ocrweb/README.md+++ b/ocrweb/README.md@@ -73,21 +73,36 @@```bash$ rapidocr_api -ip 0.0.0.0 -p 9003```-3. 使用- ```python- # python 示例,本质就是发送一个POST请求,其他语言同理。- import requests+3. 使用(本质就是发送一个POST请求,其他语言同理)+ - 以文件的方式发送请求+ ```python+ import requests++ url = 'http://localhost:9003/ocr'+ img_path = '../python/tests/test_files/ch_en_num.jpg'++ with open(img_path, 'rb') as f:+ file_dict = {'image_file': (img_path, f, 'image/png')}+ response = requests.post(url, files=file_dict, timeout=60)++ print(response.json())+ ```+ - 以`base64`方式发送post请求+ ```python+ import base64+ import requests++ url = 'http://localhost:9003/ocr'+ img_path = '../python/tests/test_files/ch_en_num.jpg'- url = 'http://localhost:9003/ocr'- img_path = '../python/tests/test_files/ch_en_num.jpg'+ with open(img_path, 'rb') as fa:+ img_str = base64.b64encode(fa.read())- with open(img_path, 'rb') as f:- file_dict = {'image': (img_path, f, 'image/png')}- response = requests.post(url, files=file_dict, timeout=60)+ payload = {'image_data': img_str}+ resp = requests.post(url, data=payload)- res_rec = response.json()- print(res_rec)- ```+ print(resp.json())+ ```4. API输出- 示例结果:<details>diff --git a/ocrweb/rapidocr_web/api.py b/ocrweb/rapidocr_web/api.pyindex 909f863..01f0052 100644--- a/ocrweb/rapidocr_web/api.py+++ b/ocrweb/rapidocr_web/api.py@@ -2,13 +2,15 @@# @Author: SWHL# @Contact: [email protected]import argparse+import base64+import ioimport jsonfrom pathlib import Pathimport cv2import numpy as npimport uvicorn-from fastapi import FastAPI, UploadFile+from fastapi import FastAPI, Form, UploadFilefrom PIL import Imagefrom rapidocr_onnxruntime import RapidOCR@@ -42,9 +44,18 @@ async def root():@app.post('/ocr')-async def ocr(image: UploadFile):- image = Image.open(image.file)- ocr_res = processor(image)+async def ocr(image_file: UploadFile = None, image_data: str = Form(None)):+ if image_file:+ img = Image.open(image_file.file)+ elif image_data:+ img_bytes = str.encode(image_data)+ img_b64decode = base64.b64decode(img_bytes)+ img = Image.open(io.BytesIO(img_b64decode))+ else:+ raise ValueError(+ 'When sending a post request, data or files must have a value.')++ ocr_res = processor(img)return ocr_resdiff --git a/ocrweb/rapidocr_web/ocrweb.py b/ocrweb/rapidocr_web/ocrweb.pyindex 1b0a2d2..86de9dd 100644--- a/ocrweb/rapidocr_web/ocrweb.py+++ b/ocrweb/rapidocr_web/ocrweb.py@@ -6,6 +6,7 @@ from pathlib import Pathfrom wsgiref.simple_server import make_serverfrom flask import Flask, render_template, request+try:from rapidocr_web.task import OCRWebUtilsexcept: