convert_json.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. '''
  2. Author: zeke-chin zeke-chin@icloud.com
  3. Date: 2022-10-11 16:38:18
  4. LastEditors: zeke-chin zeke-chin@icloud.com
  5. LastEditTime: 2022-10-12 17:43:29
  6. FilePath: /test_script/HR_OCR/tools/convert_json.py
  7. Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. '''
  9. from pathlib import Path
  10. import requests
  11. import json
  12. import base64
  13. from itertools import chain
  14. <<<<<<< Updated upstream
  15. url = 'http://192.168.199.27:18060'
  16. imgs_path = '/Users/zeke/work/sx/OCR/image_data/营业执照90'
  17. =======
  18. url = 'http://192.168.199.27:18040'
  19. imgs_path = '/Users/sxkj/test_script/HR_OCR/户口本测试样本1011-常住人口页'
  20. >>>>>>> Stashed changes
  21. def send_request(img_path):
  22. with open(img_path, 'rb') as f:
  23. img_str: str = base64.encodebytes(f.read()).decode('utf-8')
  24. data = {
  25. 'image': img_str,
  26. 'image_type': 0
  27. }
  28. <<<<<<< Updated upstream
  29. # idc_header = {
  30. # 'Content-Type': 'application/json',
  31. # 'Authorization': 'Bearer 4e00c444-620b-4d3c-85f4-777e64276f0e'
  32. # }
  33. # r = requests.post(f'{url}/ocr_system/business_license', json=data, headers=idc_header)
  34. r = requests.post(f'{url}/ocr_system/business_license', json=data)
  35. =======
  36. idc_header = {
  37. 'Content-Type': 'application/json',
  38. 'Authorization': 'Bearer 4e00c444-620b-4d3c-85f4-777e64276f0e'
  39. }
  40. r = requests.post(f'{url}/ocr_system/regbook', json=data, headers=idc_header)
  41. # r = requests.post(f'{url}/hkbsbtest/regbook', json=data)
  42. >>>>>>> Stashed changes
  43. print(r.json())
  44. return r.json()
  45. def _parse_result(r):
  46. if r['status'] == '000':
  47. r = r['result']
  48. if r:
  49. del r['confidence']
  50. return {k: v['text'] if isinstance(v, dict) else v for k, v in r.items()}
  51. elif r['status'] == '101':
  52. return "101"
  53. if __name__ == '__main__':
  54. # 0
  55. # img_paths = chain(*[Path(root / imgs_path).rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']])
  56. img_paths = chain(*[Path(imgs_path).rglob(f'*.{ext}') for ext in ['jpg']])
  57. for img_path in img_paths:
  58. print(img_path)
  59. r = send_request(img_path)
  60. res = _parse_result(r)
  61. print(res)
  62. img_path: Path = img_path
  63. d = img_path.parent
  64. fn = f'{img_path.stem}.json'
  65. with (d / fn).open('w', encoding='utf-8') as f:
  66. json.dump(res, f, ensure_ascii=False, indent=4)