天天看點

有道自然語言翻譯和文字識别OCR(圖檔文字識别)接口調用

官網 http://ai.youdao.com

文檔位址 

http://ai.youdao.com/docs/doc-ocr-api.s#p01

在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()