Преглед изворни кода

add convert_json and some_pic

zeke-chin пре 2 година
родитељ
комит
18f9272307

+ 55 - 0
convert_json.py

@@ -0,0 +1,55 @@
+from pathlib import Path
+
+import requests
+import json
+import base64
+from itertools import chain
+
+url = 'http://localhost:8080'
+
+
+def send_request(img_path, image_type=0):
+    with open(img_path, 'rb') as f:
+        img_str: str = base64.encodebytes(f.read()).decode('utf-8')
+        data = {
+            'image': img_str,
+            'image_type': image_type
+        }
+        r = requests.post(f'{url}/ocr_system/idcard', json=data)
+        print(r.json())
+        return r.json()
+
+
+def _parse_result(r):
+    if r['status'] == '000':
+        r = r['result']
+        if r:
+            del r['confidence']
+        res = {}
+        for k, v in r.items():
+            if isinstance(v, dict):
+                res[k] = v['text']
+            else:
+                res[k] = v
+        return res
+    elif r['status'] == '101':
+        return "101"
+
+
+if __name__ == '__main__':
+
+    # 0
+    root = Path(__file__).parent
+    print(root)
+    img_paths = chain(*[Path(root / 'images/all/7.20/new/').rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']])
+    for img_path in img_paths:
+        print(img_path)
+        r = send_request(img_path, 1)
+        res = _parse_result(r)
+        print(res)
+        img_path: Path = img_path
+        d = img_path.parent
+        fn = img_path.stem + '.json'
+
+        with (d / fn).open('w', encoding='utf-8') as f:
+            json.dump(res, f, ensure_ascii=False, indent=4)

BIN
images/all/7.20/10084309_居民身份证(人像面).jpg


BIN
images/all/7.20/error/身份证正面-王磊.png


BIN
images/all/7.20/new/04_90.jpg


BIN
images/all/7.20/new/img.png


BIN
images/all/7.20/彭贤端-身份证正面.jpg


BIN
images/all/7.20/正面.jpg


BIN
images/all/7.20/甘红军-身份证照片1.jpg


BIN
images/all/7.20/身份证-正.jpg


BIN
images/all/7.20/身份证人像.jpg


BIN
images/all/7.20/通燕磊_居民身份证(人像面).jpg


BIN
images/all/7.20/附件3:身份证(正面).jpg


BIN
images/all/7.20/黄志昌-身份证人像面.jpg