test_api_call.py 751 B

1234567891011121314151617181920212223
  1. import base64
  2. import requests
  3. API_URL = "http://localhost:28888/ocr"
  4. file_path = "./sample_data/PictureCheckCode.jpeg"
  5. with open(file_path, "rb") as file:
  6. file_bytes = file.read()
  7. file_data = base64.b64encode(file_bytes).decode("ascii")
  8. payload = {"file": file_data, "fileType": 1}
  9. response = requests.post(API_URL, json=payload)
  10. assert response.status_code == 200
  11. result = response.json()["result"]
  12. for i, res in enumerate(result["ocrResults"]):
  13. print(res["prunedResult"])
  14. ocr_img_path = f"sample_data/验证码_ocr_{i}.jpg"
  15. with open(ocr_img_path, "wb") as f:
  16. f.write(base64.b64decode(res["ocrImage"]))
  17. print(f"Output image saved at {ocr_img_path}")
  18. print(f"rec_texts: {res['prunedResult']['rec_texts'][0]}")