see word use baidu ai
#! /bin/env python# -*- coding: UTF-8 -*-import urllibimport urllib2import sysimport timeimport socketimport jsonimport base64from datetime import datetimeprint datetime.now()print sys.v...
·
#! /bin/env python
# -*- coding: UTF-8 -*-
import urllib
import urllib2
import sys
import time
import socket
import json
import base64
from datetime import datetime
print datetime.now()
print sys.version
domain = "aip.baidubce.com"
myaddr = socket.getaddrinfo(domain,'https')
print str(domain) + " = " + myaddr[0][4][0]
start = time.time()
# client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id = '------'
# print "client_id:" + client_id
client_secret = '-------'
# print "client_secret:" + client_secret
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s' % (client_id, client_secret)
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
access_token = (json.loads(content)['access_token'])
print "access_token:" + str(json.loads(content)['access_token'])
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + access_token
# 二进制方式打开图文件
f = open(r'D:\\workspace\\qt\\qtseeword\\demoo\\11.jpg', 'rb')
# 参数image:图像base64编码
img = base64.b64encode(f.read())
params = {"image": img,
# "recognize_granularity": "small",
# "words_type": "number",
# "probability": "true"
# "multi_detect": 'true'
# "accuracy ": "high"
# "detect_direction": "true"
# "recognize_granularity": "small",
# "probability": "true"
# "id_card_side": "front"
# "detect_risk": "true"
# "templateSign": "e1457d528af5db18573fa38c4aaf6683"
# "detect_direction": "true"
}
params = urllib.urlencode(params)
request = urllib2.Request(url, params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
# print response
content = response.read()
# for item in json.loads(content)['words_result']:
# print item['words']
#print "完整结果如下:"
# print json.dumps(json.loads(content), ensure_ascii=False)
print json.dumps(json.loads(content)['words_result'], ensure_ascii=False)
end = time.time()
print('Running time: %1.2f Seconds'%(end-start))
使用的是python2.x 这是百度云客服给出的代码。用post方式
更多推荐
已为社区贡献2条内容
所有评论(0)