FlanChanXwO commited on
Commit
aac8079
·
verified ·
1 Parent(s): c707cf6

Upload scripts/onnx_ocr_raw.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/onnx_ocr_raw.py +42 -3
scripts/onnx_ocr_raw.py CHANGED
@@ -1,3 +1,42 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:185b24d7fe21f80da38264e7c6d63796da6db523c8b1faf1611d3509c1c95e63
3
- size 1059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """用原始图训练的 ONNX 模型识别验证码。预处理: RGB 160x64 /255 -> (1,3,64,160) -> 4位数字
3
+ """
4
+ import json
5
+ import os
6
+ import sys
7
+
8
+ import numpy as np
9
+ import onnxruntime as ort
10
+ from PIL import Image
11
+
12
+ MODEL = os.path.expanduser("~/sp_captcha_assets/models/captcha_1000_raw.onnx")
13
+ _sess = None
14
+
15
+
16
+ def _get_sess():
17
+ global _sess
18
+ if _sess is None:
19
+ _sess = ort.InferenceSession(MODEL, providers=["CPUExecutionProvider"])
20
+ return _sess
21
+
22
+
23
+ def predict(path):
24
+ im = Image.open(path).convert("RGB").resize((160, 64), Image.BILINEAR)
25
+ a = np.asarray(im, dtype=np.float32) / 255.0
26
+ x = a.transpose(2, 0, 1)[None]
27
+ out = _get_sess().run(None, {"input": x})[0]
28
+ return "".join(str(int(out[0, p].argmax())) for p in range(4))
29
+
30
+
31
+ def main():
32
+ out = {}
33
+ for f in sys.argv[1:]:
34
+ try:
35
+ out[f] = {"onnx": predict(f)}
36
+ except Exception as e:
37
+ out[f] = {"onnx": "", "err": str(e)}
38
+ print(json.dumps(out, ensure_ascii=False))
39
+
40
+
41
+ if __name__ == "__main__":
42
+ main()