利用的API 首先查看下接口文档
一、首先是python2.7版本代码
#encoding=utf-8
import requests
import time
import string
import random
import base64
import hashlib
import urllib
from urllib import urlencode
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
CommonUrl="https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr"
app_id="填自己的"
AppKey="填自己的"
time_stamp=int(time.time())
nonce_str=''.join(random.sample(string.ascii_letters + string.digits, 15))
def MakeSign():
Dict ={'app_id': app_id, 'time_stamp': time_stamp, 'nonce_str': nonce_str, 'image': ImageBase64()}
T_str1 = ""
for key in sorted(Dict.keys()):
if key == 'app_key':
continue
T_str1 = T_str1 + "%s=%s&" % (key, urllib.quote(str(Dict[key]), safe=''))
T_sgin = T_str1 + 'app_key=' + AppKey
signraw=T_sgin.encode("utf-8")
hash_md5 = hashlib.md5()
hash_md5.update(signraw)
sign=hash_md5.hexdigest().upper()
return sign
def ImageBase64():
imagebase64=None
try:
with open("C:\\Users\\Administrator\\Desktop\\xiejiangpeng.pn1g", "rb") as f:
imagebase64 = base64.b64encode(f.read())
except Exception as ex:
print(ex)
exit()
return imagebase64
sign=MakeSign()#签名
image=ImageBase64()#图片
#调用OCR图片识别接口
TengXunAiCommonBody={'app_id':app_id,'time_stamp':time_stamp,'nonce_str':nonce_str,'sign':sign,'image':image}
TengXunAiCommon=requests.post(CommonUrl,TengXunAiCommonBody,verify=False)
try:
temp=TengXunAiCommon.json()["data"]["item_list"][0][