convert_json.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from pathlib import Path
  2. import requests
  3. import json
  4. import base64
  5. from itertools import chain
  6. url = 'http://192.168.199.249:38811'
  7. def send_request(img_path, image_type=0):
  8. with open(img_path, 'rb') as f:
  9. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  10. data = {
  11. 'image': img_str,
  12. 'image_type': image_type
  13. }
  14. r = requests.post(f'{url}/ocr_system/regbook', json=data)
  15. print(r.json())
  16. return r.json()
  17. def _parse_result(r):
  18. if r['status'] == '000':
  19. r = r['result']
  20. if r:
  21. del r['confidence']
  22. res = {}
  23. for k, v in r.items():
  24. if isinstance(v, dict):
  25. res[k] = v['text']
  26. else:
  27. res[k] = v
  28. return res
  29. elif r['status'] == '101':
  30. return "101"
  31. if __name__ == '__main__':
  32. # 0
  33. # root = Path(__file__).parent
  34. # print(root)
  35. # img_paths = chain(
  36. # *[Path(root / 'images/clean/0').rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']])
  37. # for img_path in img_paths:
  38. # print(img_path)
  39. # r = send_request(img_path, 0)
  40. # res = _parse_result(r)
  41. # print(res)
  42. # img_path: Path = img_path
  43. # d = img_path.parent
  44. # fn = img_path.stem + '.json'
  45. #
  46. # with (d / fn).open('w', encoding='utf-8') as f:
  47. # json.dump(res, f, ensure_ascii=False, indent=4)
  48. root = Path(__file__).parent
  49. print(root)
  50. img_paths = chain(
  51. *[Path(root / 'images/clean/1').rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']])
  52. for img_path in img_paths:
  53. print(img_path)
  54. r = send_request(img_path, 1)
  55. res = _parse_result(r)
  56. print(res)
  57. img_path: Path = img_path
  58. d = img_path.parent
  59. fn = img_path.stem + '.json'
  60. with (d / fn).open('w', encoding='utf-8') as f:
  61. json.dump(res, f, ensure_ascii=False, indent=4)