12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- '''
- Author: zeke-chin zeke-chin@icloud.com
- Date: 2022-10-11 16:38:18
- LastEditors: zeke-chin zeke-chin@icloud.com
- LastEditTime: 2022-10-12 17:43:29
- FilePath: /test_script/HR_OCR/tools/convert_json.py
- Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
- '''
- from pathlib import Path
- import requests
- import json
- import base64
- from itertools import chain
- <<<<<<< Updated upstream
- url = 'http://192.168.199.27:18060'
- imgs_path = '/Users/zeke/work/sx/OCR/image_data/营业执照90'
- =======
- url = 'http://192.168.199.27:18040'
- imgs_path = '/Users/sxkj/test_script/HR_OCR/户口本测试样本1011-常住人口页'
- >>>>>>> Stashed changes
- def send_request(img_path):
- with open(img_path, 'rb') as f:
- img_str: str = base64.encodebytes(f.read()).decode('utf-8')
- data = {
- 'image': img_str,
- 'image_type': 0
- }
- <<<<<<< Updated upstream
- # idc_header = {
- # 'Content-Type': 'application/json',
- # 'Authorization': 'Bearer 4e00c444-620b-4d3c-85f4-777e64276f0e'
- # }
- # r = requests.post(f'{url}/ocr_system/business_license', json=data, headers=idc_header)
- r = requests.post(f'{url}/ocr_system/business_license', json=data)
- =======
- idc_header = {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer 4e00c444-620b-4d3c-85f4-777e64276f0e'
- }
- r = requests.post(f'{url}/ocr_system/regbook', json=data, headers=idc_header)
- # r = requests.post(f'{url}/hkbsbtest/regbook', json=data)
- >>>>>>> Stashed changes
- print(r.json())
- return r.json()
- def _parse_result(r):
- if r['status'] == '000':
- r = r['result']
- if r:
- del r['confidence']
- return {k: v['text'] if isinstance(v, dict) else v for k, v in r.items()}
- elif r['status'] == '101':
- return "101"
- if __name__ == '__main__':
- # 0
- # img_paths = chain(*[Path(root / imgs_path).rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']])
- img_paths = chain(*[Path(imgs_path).rglob(f'*.{ext}') for ext in ['jpg']])
- for img_path in img_paths:
- print(img_path)
- r = send_request(img_path)
- res = _parse_result(r)
- print(res)
- img_path: Path = img_path
- d = img_path.parent
- fn = f'{img_path.stem}.json'
- with (d / fn).open('w', encoding='utf-8') as f:
- json.dump(res, f, ensure_ascii=False, indent=4)
|