-
Notifications
You must be signed in to change notification settings - Fork 4
/
hanvon.py
55 lines (40 loc) · 1.53 KB
/
hanvon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# github.com/se4
import requests
import base64
import json
class Hanvon(object):
def __init__(self, appcode):
self.appcode = appcode
def get_pic_base64(self, pic_name, ext='jpg'):
f = open('./{pic_name}.{ext}'.format(pic_name=pic_name, ext=ext), 'rb')
ls_f = base64.b64encode(f.read())
f.close()
return ls_f
def get_text(self, pic_name, ext='jpg'):
body = {'uid': '118.12.0.12', 'lang': 'chns', 'image': '/9j/4AAQSkZJRgABAgAAAQABAAD'}
headers = {'Authorization': 'APPCODE ' + self.appcode,
'Content-Type': 'application/octet-stream'}
addr = 'http://text.aliapi.hanvon.com/rt/ws/v1/ocr/text/recg'
body['image'] = self.get_pic_base64(pic_name=pic_name, ext=ext).decode()
resp = requests.post(addr, headers=headers, data=json.dumps(body)).text
if 'textResult' in resp:
return eval(resp)['textResult']
else:
return ''
#appcode = ''
#ocr = Havon(appcode)
#print(ocr.get_text('3', 'png'))
"""
appcode在https://market.aliyun.com/products/57124001/cmapi011523.html获取
上文例子是提交同目录下zs.png进行识别
若在阿里云市场开启过IP验证,请释放下列注释,运行时间加0.2秒
import socket
import re
body['uid'] = get_ip()
def get_ip():
names, aliases, ips = socket.gethostbyname_ex(socket.gethostname())
for ip in ips:
if not re.match('^192', ip):
return ip
"""