utils.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import json
  2. import requests
  3. from pathlib import Path
  4. from YQ_OCR.configs.config import *
  5. # 发送请求 带正确答案参数
  6. def send_request(img_path: Path, img_json: str):
  7. file = {'file': (img_path.name, open(img_path, 'rb'), img_path)}
  8. payload = {'docDataStr': img_json}
  9. r = requests.post(url + url_path, files=file, data=payload)
  10. return r.json()
  11. # 编辑距离
  12. def Levenshtein_Distance(str1, str2):
  13. matrix = [[i + j for j in range(len(str2) + 1)] for i in range(len(str1) + 1)]
  14. for i in range(1, len(str1) + 1):
  15. for j in range(1, len(str2) + 1):
  16. d = 0 if (str1[i - 1] == str2[j - 1]) else 1
  17. matrix[i][j] = min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + d)
  18. return matrix[len(str1)][len(str2)]
  19. # 处理返回结果
  20. def parse_result(r): # sourcery skip: dict-comprehension
  21. if r['status'] == '000':
  22. result = r['result']
  23. res = {}
  24. for field in keyDict:
  25. if field in result:
  26. res[field] = result[field]
  27. res['noKeyList'] = result['noKeyList']
  28. res['logoList'] = result['logoList']
  29. res['tableList'] = result['tableList']
  30. logoFileName = [log['logoFileName'] for log in res['logoList']]
  31. res['logoList'] = logoFileName
  32. return res
  33. elif r['status'] == '101':
  34. return "101"
  35. # 打开正确的json文件
  36. def open_true_json(j_path):
  37. with j_path.open('r', encoding='utf-8') as f:
  38. j_dict = json.load(f)
  39. j_json_str = json.dumps(j_dict, ensure_ascii=False)
  40. return j_dict, j_json_str