调用有道API实现图片翻译
- 1.作者介绍
- 2.关于理论方面的知识介绍
- 3.调用有道API实现图片翻译实验
- 3.1官方代码与结果
- 3.2解码
- 3.3换行输出
- 3.4 示例展示
1.作者介绍
周慧龙,男,西安工程大学电子信息学院,2022级研究生
研究方向:机器视觉与人工智能
电子邮件:2419979656@qq.com
王泽宇,男,西安工程大学电子信息学院,2022级研究生,张宏伟人工智能课题组
研究方向:机器视觉与人工智能
电子邮件:2717124491@qq.com
2.关于理论方面的知识介绍
API介绍
图片翻译服务,基于文字识别与文本翻译技术,只需要通过调用图片翻译API,传入图片的地址,指定源语言与目标语言,通过POST请求方式,就可以识别图片中的文字并进行翻译。
3.调用有道API实现图片翻译实验
3.1官方代码与结果
# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import base64
import hashlib
from imp import reload
reload(sys)
YOUDAO_URL = 'https://openapi.youdao.com/ocrtransapi'
APP_KEY = '*' # 应用ID
APP_SECRET = '*' # 应用密钥
def truncate(q):
if q is None:
return None
size = len(q)
return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]
def encrypt(signStr):
hash_algorithm = hashlib.md5()
hash_algorithm.update(signStr.encode('utf-8'))
return hash_algorithm.hexdigest()
def do_request(data):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
return requests.post(YOUDAO_URL, data=data, headers=headers)
def connect():
f = open(r'C:/Users/NO.1/Desktop/28.jpg', 'rb') # 二进制方式打开图文件
q = base64.b64encode(f.read()).decode('utf-8') # 读取文件内容,转换为base64编码
f.close()
data = {}
data['from'] = 'en' # 图片源语言
data['to'] = 'zh-CHS'
data['type'] = '1'
data['q'] = q
salt = str(uuid.uuid1())
signStr = APP_KEY + q + salt + APP_SECRET
sign = encrypt(signStr)
data['appKey'] = APP_KEY
data['salt'] = salt
data['sign'] = sign
response = do_request(data)
results = response.content
print(results)
if __name__ == '__main__':
connect()
实验结果
存在问题:图片中的英文成功识别,但是转换成中文后未进行解码
3.2解码
# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import base64
import hashlib
from imp import reload
reload(sys)
YOUDAO_URL = 'https://openapi.youdao.com/ocrtransapi'
APP_KEY = '*' # 应用ID
APP_SECRET = '*' # 应用密钥
def truncate(q):
if q is None:
return None
size = len(q)
return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]
def encrypt(signStr):
hash_algorithm = hashlib.md5()
hash_algorithm.update(signStr.encode('utf-8'))
return hash_algorithm.hexdigest()
def do_request(data):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
return requests.post(YOUDAO_URL, data=data, headers=headers)
def connect():
f = open(r'C:/Users/NO.1/Desktop/28.jpg', 'rb') # 二进制方式打开图文件
q = base64.b64encode(f.read()).decode('utf-8') # 读取文件内容,转换为base64编码
f.close()
data = {}
data['from'] = 'en' # 图片源语言
data['to'] = 'zh-CHS'
data['type'] = '1'
data['q'] = q
salt = str(uuid.uuid1())
signStr = APP_KEY + q + salt + APP_SECRET
sign = encrypt(signStr)
data['appKey'] = APP_KEY
data['salt'] = salt
data['sign'] = sign
response = do_request(data)
results = response.content.decode()
print(results)
if __name__ == '__main__':
connect()
实验结果
存在问题:可以成功将图片中的英文转换成中文,但是结果太过凌乱,影响读者观看,所以对结果再次处理
3.3换行输出
# -*- coding: utf-8 -*-
import sys
import uuid
import requests
import base64
import hashlib
from imp import reload
reload(sys)
YOUDAO_URL = 'https://openapi.youdao.com/ocrtransapi'
APP_KEY = '*' # 应用ID
APP_SECRET = '*' # 应用密钥
def truncate(q):
if q is None:
return None
size = len(q)
return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]
def encrypt(signStr):
hash_algorithm = hashlib.md5()
hash_algorithm.update(signStr.encode('utf-8'))
return hash_algorithm.hexdigest()
def do_request(data):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
return requests.post(YOUDAO_URL, data=data, headers=headers)
def connect():
f = open(r'C:/Users/NO.1/Desktop/28.jpg', 'rb') # 二进制方式打开图文件
q = base64.b64encode(f.read()).decode('utf-8') # 读取文件内容,转换为base64编码
f.close()
data = {}
data['from'] = 'en' # 图片源语言
data['to'] = 'zh-CHS'
data['type'] = '1'
data['q'] = q
salt = str(uuid.uuid1())
signStr = APP_KEY + q + salt + APP_SECRET
sign = encrypt(signStr)
data['appKey'] = APP_KEY
data['salt'] = salt
data['sign'] = sign
response = do_request(data)
results = response.content.decode()
sp1 = results.split('",')
aticle1 = str(sp1).replace("', '","\"\n")
sp2 = results.split(',"')
aticle2 = str(sp2).replace("', '","\"\n")
print(aticle2)
if __name__ == '__main__':
connect()
实验结果
3.4 示例展示
对上图进行翻译,翻译结果如下