Skip to content

Commit

Permalink
fix(rapidocr_api): Fixed returned formatting errors (#200)
Browse files Browse the repository at this point in the history
* rapidocr api
rapidocr_onnxruntime 版本 1.3.23  api返回格式需要格式化
  • Loading branch information
benben17 authored Jul 11, 2024
1 parent efa113c commit 420b097
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import requests

url = 'http://localhost:9003/ocr'
img_path = '../python/tests/test_files/ch_en_num.jpg'
img_path = '/tmp/page1_image1.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.text)
print(response.text)
19 changes: 17 additions & 2 deletions api/rapidocr_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,32 @@ def __init__(self) -> None:

def __call__(self, img: Image.Image) -> Dict:
img = np.array(img)

ocr_res, _ = self.ocr(img)

if not ocr_res:
return {}

out_dict = {
str(i): {"rec_txt": rec, "dt_boxes": dt_box, "score": score}
for i, (dt_box, rec, score) in enumerate(ocr_res)
}
return out_dict

return self.convert_to_json(out_dict)

def convert_to_json(self, data):
"""Recursively convert NumPy types to Python types."""
if isinstance(data, np.ndarray):
return data.tolist()
elif isinstance(data, np.generic):
return data.item()
elif isinstance(data, (list, tuple)):
return [self.convert_to_json(item) for item in data]
elif isinstance(data, dict):
return {k: self.convert_to_json(v) for k, v in data.items()}
elif isinstance(data, np.bool_):
return bool(data)
else:
return data

app = FastAPI()
processor = OCRAPIUtils()
Expand Down

0 comments on commit 420b097

Please sign in to comment.