FlanChanXwO commited on
Commit
0b767e2
·
verified ·
1 Parent(s): ea12560

Upload scripts/vision_ocr.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/vision_ocr.py +49 -3
scripts/vision_ocr.py CHANGED
@@ -1,3 +1,49 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:fdcdc5154f8a6f015f271fd1ccc7d1f5329503d787b07559f4bff7b9892bcd3d
3
- size 1889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """单图验证码识别,走 pie-xian command-a-vision。输出格式兼容 harness 的 recognize()。
3
+ 用法: vision_ocr.py file.png ... -> {"file.png": {"vision": "code"}}
4
+ """
5
+ import base64
6
+ import json
7
+ import re
8
+ import ssl
9
+ import sys
10
+ import urllib.request
11
+
12
+ API = "https://api.pie-xian.com/v1/chat/completions"
13
+ KEY = "sk-Rel8iERHrmu9NDbvQhnhwTowEUnGqVvCFXMatASH8q5O4gGP"
14
+ MODEL = "command-a-vision-07-2025"
15
+ UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/126.0.0.0 Safari/537.36"
16
+ PROMPT = "识别这张验证码图片中的字符。只输出字符本身,不要任何解释或标点。这是登录验证码,可能包含1-4个数字。"
17
+
18
+ _CTX = ssl.create_default_context()
19
+ _CTX.check_hostname = False
20
+ _CTX.verify_mode = ssl.CERT_NONE
21
+
22
+
23
+ def read(path):
24
+ b64 = base64.b64encode(open(path, "rb").read()).decode()
25
+ body = json.dumps({"model": MODEL, "messages": [{"role": "user", "content": [
26
+ {"type": "text", "text": PROMPT},
27
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64," + b64}}]}],
28
+ "max_tokens": 20, "temperature": 0}).encode()
29
+ req = urllib.request.Request(API, data=body, headers={
30
+ "Authorization": "Bearer " + KEY, "Content-Type": "application/json", "User-Agent": UA})
31
+ d = json.loads(urllib.request.urlopen(req, timeout=90, context=_CTX).read())
32
+ return re.sub(r"[^0-9A-Za-z]", "", d["choices"][0]["message"]["content"])
33
+
34
+
35
+ def main():
36
+ out = {}
37
+ for f in sys.argv[1:]:
38
+ try:
39
+ # 2 次读取,一致才返回;不一致返回空(harness 跳过登录)
40
+ r1, r2 = read(f), read(f)
41
+ code = r1 if r1 == r2 else ""
42
+ out[f] = {"vision": code}
43
+ except Exception as e:
44
+ out[f] = {"vision": "", "err": str(e)}
45
+ print(json.dumps(out, ensure_ascii=False))
46
+
47
+
48
+ if __name__ == "__main__":
49
+ main()