有道自然语言翻译和文字识别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()

 
原文地址:https://www.cnblogs.com/Tom-yi/p/8125384.html