PP-StructureV3_1.0.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import os
  2. import re
  3. import base64
  4. import requests
  5. import config
  6. API_URL = "https://q2z8becfm967o4y7.aistudio-app.com/layout-parsing"
  7. TOKEN = "16455708d55afac2f074f4ae5a88fc6c7bae7920"
  8. file_path = "E:\\project\\arbitration_system\\appplication_extractor\\test\\刘正新\\刘正新-申请书.png"
  9. input_filename = os.path.splitext(os.path.basename(file_path))[0]
  10. with open(file_path, "rb") as file:
  11. file_bytes = file.read()
  12. file_data = base64.b64encode(file_bytes).decode("ascii")
  13. headers = {
  14. "Authorization": f"token {TOKEN}",
  15. "Content-Type": "application/json"
  16. }
  17. required_payload = {
  18. "file": file_data,
  19. "fileType": 1,
  20. }
  21. optional_payload = {
  22. "useDocOrientationClassify": False,
  23. "useDocUnwarping": False,
  24. "useTextlineOrientation": False,
  25. }
  26. payload = {**required_payload, **optional_payload}
  27. response = requests.post(API_URL, json=payload, headers=headers)
  28. assert response.status_code == 200
  29. result = response.json()["result"]
  30. os.makedirs("../PP-OCRv5/output", exist_ok=True)
  31. # 如果需要处理多个页面
  32. for i, res in enumerate(result.get("ocrResults", [])):
  33. if "prunedResult" in res:
  34. pruned_result = res["prunedResult"]
  35. if "rec_texts" in pruned_result:
  36. print(f"\n=== 页面 {i + 1} 的识别文本 ===")
  37. result_text = ""
  38. for j, text in enumerate(pruned_result["rec_texts"]):
  39. result_text = result_text+"\n"+text
  40. print(result_text)