官網: ai.youdao.com
文件地址 :ai.youdao.com/docs/doc-oc…
在Python中呼叫api.
#/usr/bin/env python
#coding=utf8
import httplib
import md5
import urllib
import urllib2
import random
import json
import base64
appKey = '應用程式key'
secretKey = '應用程式祕鑰'
httpClient = None
try:
f=open(r'd:\1.png','rb') #二進位制方式開啟圖檔案 需要使用者在d:\1.png 放這個圖片並且裡面得有文字
img=base64.b64encode(f.read()) #讀取檔案內容,轉換為base64編碼
f.close()
detectType = '10011'
imageType = '1'
langType = 'zh-en'#en
salt = random.randint(1, 65536)
sign = appKey+img+str(salt)+secretKey
m1 = md5.new()
m1.update(sign)
sign = m1.hexdigest()
data = {'appKey':appKey,'img':img,'detectType':detectType,'imageType':imageType,'langType':langType,'salt':str(salt),'sign':sign}
data = urllib.urlencode(data)
req = urllib2.Request('http://openapi.youdao.com/ocrapi',data)
#response是HTTPResponse物件
response = urllib2.urlopen(req)
readJson = response.read()
print unicode(readJson, "utf-8")
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
複製程式碼