一.关于使用百度的接口
首先需要下载sdk环境配置
pip install baidu-aip
1.进入百度ai开放平台,创建一个语音合成,读取SDK文件文档
from aip import AipSpeech """ 你的 APPID AK SK """ APP_ID = '你的 App ID' API_KEY = '你的 Api Key' SECRET_KEY = '你的 Secret Key' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
result = client.synthesis('你好百度', 'zh', 1, { 'vol': 5, }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result)
from aip import AipSpeech """ 你的 APPID AK SK """ APP_ID = '15420917' API_KEY = 'eP25XkG4vhdRQk7A6ZW3Zf2C' SECRET_KEY = 'RxwrVkWUodowcBWcyLwS7DWI8B9XE4cH ' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis('一人饮酒醉','zh',1,{ 'vol':5, }) if not isinstance(result,dict): with open("aduio.mp3","wb") as f: f.write(result)
这样简单的语音合成就实现了
2.语音识别
同上
1.进入百度ai开放平台,创建一个语音合成,读取SDK文件文档
from aip import AipSpeech """ 你的 APPID AK SK """ APP_ID = '你的 App ID' API_KEY = '你的 Api Key' SECRET_KEY = '你的 Secret Key' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 读取文件 def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 识别本地文件 client.asr(get_file_content('audio.pcm'), 'pcm', 16000, { 'dev_pid': 1536, })
注意:这里识别的文件格式最好是pcm格式,所以使用 前提下载FFmpep 放到环境变量中后,重启pycharm
os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") #os.system(commind)这里面使用终端命令
进行在线文件格式转换,成pcm格式
from aip import AipSpeech import os """ 你的 APPID AK SK """ APP_ID = '15420917' API_KEY = 'eP25XkG4vhdRQk7A6ZW3Zf2C' SECRET_KEY = 'RxwrVkWUodowcBWcyLwS7DWI8B9XE4cH ' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 读取文件 def get_file_content(filePath): os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() # 识别本地文件 res=client.asr(get_file_content('ots.m4a'), 'pcm', 16000, { 'dev_pid': 1536, }) print(res) print(res.get("result")[0])
3.进行语音合成与语音语音识别
from aip import AipSpeech import time,os """ 你的 APPID AK SK """ APP_ID = '15420336' API_KEY = 'VwSGcqqwsCl282LGKnFwHDIA' SECRET_KEY = 'h4oL6Y9yRuvmD0oSdQGQZchNcix4TF5P' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 读取文件 def get_file_content(filePath): os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") with open(f"{filePath}.pcm", 'rb') as fp: return fp.read() def audio2text(filepath): # 识别本地文件 res = client.asr(get_file_content(filepath), 'pcm', 16000, { 'dev_pid': 1536, }) print(res.get("result")[0]) return res.get("result")[0] def text2audio(text): filename = f"{time.time()}.mp3" result = client.synthesis(text, 'zh', 1, { 'vol': 5, "spd": 3, "pit": 7, "per": 4 }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open(filename, 'wb') as f: f.write(result) return filename text = audio2text("wyn.wma") filename = text2audio(text) os.system(filename)
4.图灵智能语音
作用:通过庞大的数据库进行对话
1.创建一个图灵智能语音,获取api
2.查看使用文档
接口地址 发送post请求
http://openapi.tuling123.com/openapi/api/v2
3.使用实例
{ "reqType":0, "perception": { "inputText": { "text": "附近的酒店" }, "inputImage": { "url": "imageUrl" }, "selfInfo": { "location": { "city": "北京", "province": "北京", "street": "信息路" } } }, "userInfo": { "apiKey": "", "userId": "" } }
代码;
import requests args = { "reqType":0, "perception": { "inputText": { "text": "北京" } }, "userInfo": { "apiKey": "9a9a026e2eb64ed6b006ad99d27f6b9e", "userId": "1111" } } url = "http://openapi.tuling123.com/openapi/api/v2" res = requests.post(url,json=args) text = res.json().get("results")[0].get("values").get("text")