diff --git a/api/demo.py b/api/demo.py index a3af37b94..865292d45 100644 --- a/api/demo.py +++ b/api/demo.py @@ -2,13 +2,28 @@ # @Author: SWHL # @Contact: liekkaskono@163.com +# 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) \ No newline at end of file +print(response.json()) diff --git a/api/rapidocr_api/api.py b/api/rapidocr_api/api.py index e7902252e..9b553a73d 100644 --- a/api/rapidocr_api/api.py +++ b/api/rapidocr_api/api.py @@ -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()