Skip to content

Commit

Permalink
fix(rapidocr_api): Optim code
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Jul 11, 2024
1 parent 420b097 commit f5f2de2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
27 changes: 21 additions & 6 deletions api/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
# @Author: SWHL
# @Contact: [email protected]

# 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.text)

import base64

import requests

url = 'http://localhost:9003/ocr'
img_path = '/tmp/page1_image1.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_file': (img_path, f, 'image/png')}
response = requests.post(url, files=file_dict, timeout=60)
payload = {"image_data": img_str}
response = requests.post(url, data=payload, timeout=60)

print(response.text)
print(response.json())
23 changes: 6 additions & 17 deletions api/rapidocr_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,15 @@ def __call__(self, img: Image.Image) -> Dict:
return {}

out_dict = {
str(i): {"rec_txt": rec, "dt_boxes": dt_box, "score": score}
str(i): {
"rec_txt": rec,
"dt_boxes": dt_box,
"score": f"{score:.4f}",
}
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 f5f2de2

Please sign in to comment.