我的本意是想在Xilinx PYNQ上实现语音翻译,做一个类似翻译宝的应用,由于百度智能云目前仅支持安卓和IOS上的语音翻译,所以想要在嵌入式设备上实现该功能,就需要顺序调用短语音识别API和通用翻译API,下面记录使用方法。
1.进入百度智能云网站注册账号并登录:百度智能云-智能时代基础设施 (baidu.com)
登录界面:
2.登录之后在上面一堆选项栏里选择产品 —> 人工智能 —> 短语音识别
如果您要求注册的开发人员注册(需要进行ID号验证),请单击此简短的语音识别选项。
[En]
If you ask a registered developer to register (ID number authentication is required), then click on this short speech recognition option.
3.点立即使用,如下图
4.点领取免费资源,能领的都领上,如下图
5.领完之后回到页面,点创建应用
按照指引填就行了
6.填完回来再点管理应用,这里就显示了调用API要用到的三个关键字符串:AppID、API Key 和 Secret Key,如图点击左侧的技术文档选项。
7.如图,在这里就能找到API的使用方法
我的两个代码张贴在这里供参考:
[En]
Two of my codes are posted here for reference:
from aip import AipSpeech
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.asr(get_file_content('mic1.wav'), 'wav', 16000, {
'dev_pid': 1737,
})
import audioop
from aip import AipSpeech
from soundfile import SoundFile
cap_cnt = 44100 #采样频率
file = SoundFile('recong.wav')
temp_data = bytes(cap_cnt * 50)
file.buffer_read_into(temp_data, dtype='int16');
data = audioop.ratecv(temp_data, 2, 1, 44100, 16000, None) # 转换采样频率到16000
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.asr(data[0], 'wav', 16000, {
'dev_pid': 1737,
})
result里返回一个字典包括很多数据,用 result[‘result’][0] 返回识别结果。
8.下面是将识别到的结果送到百度翻译
点击网址:百度AI开放平台-全球领先的人工智能服务平台 (baidu.com)
选择上面的开发能力选项,点击常用文本翻译选项,如下图所示
[En]
Select the development capability option above, and click the common text translation option, as shown in the following figure
如果您需要登录,只需使用您的语音识别帐户,然后单击即可使用。
[En]
If you need to log in, just use your voice recognition account and click to use it immediately.
创建一个应用程序并根据指导原则填写它
[En]
Create an application and fill it in according to the guidelines
点管理应用,点技术文档
9.这里需要说明的是,文本翻译功能使用的是HTTP的方式进行调用,所以要先根据技术文档获取access_token,文档里有教:
import requests
client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
print(response.json())
10.获取到access_token后,就能使用翻译功能了:
import requests
url = 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=【刚刚获取的access_token】'
headers = {"Content-Type":"json;charset=utf-8"}
params = {"q":"hello","from":"auto","to":"zh"}
r = requests.post(url=url,params=params,headers=headers)
a = r.json()
print(a['result']['trans_result'])
通过将语音识别的输出送入文本翻译的输入端,即可实现语音翻译。
[En]
By sending the output of speech recognition to the input of text translation, speech translation can be realized.
Original: https://blog.csdn.net/duanduann/article/details/119300775
Author: DUANDAUNNN
Title: 调用百度aip实现短语音翻译(附代码)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/512566/
转载文章受原作者版权保护。转载请注明原作者出处!