瀏覽代碼

新增脚本:test_api_call.py,用于调用OCR API并处理验证码图片

zhch158_admin 4 月之前
父節點
當前提交
1aed4f89db
共有 1 個文件被更改,包括 23 次插入0 次删除
  1. 23 0
      zhch/test_api_call.py

+ 23 - 0
zhch/test_api_call.py

@@ -0,0 +1,23 @@
+import base64
+import requests
+
+API_URL = "http://localhost:28888/ocr"
+file_path = "./sample_data/PictureCheckCode.jpeg"
+
+with open(file_path, "rb") as file:
+    file_bytes = file.read()
+    file_data = base64.b64encode(file_bytes).decode("ascii")
+
+payload = {"file": file_data, "fileType": 1}
+
+response = requests.post(API_URL, json=payload)
+
+assert response.status_code == 200
+result = response.json()["result"]
+for i, res in enumerate(result["ocrResults"]):
+    print(res["prunedResult"])
+    ocr_img_path = f"sample_data/验证码_ocr_{i}.jpg"
+    with open(ocr_img_path, "wb") as f:
+        f.write(base64.b64decode(res["ocrImage"]))
+    print(f"Output image saved at {ocr_img_path}")
+    print(f"rec_texts: {res['prunedResult']['rec_texts'][0]}")