1,语音的合成,识别
后端代码:
from aip import AipSpeech, AipNlp import os # 语音合成 """ 你的 APPID AK SK """ # 申请的Ai.baidu.com的ID,接口,密钥 APP_ID = '15217709' API_KEY = 'eNiP5QUsgBh6QwpbNv8Qmsy3' SECRET_KEY = 'gwhM3wDo0Kjjd1PDIxqqW4Bfex10Y4f3' # 实例化AipSpeech,AipNlp对象 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) nlp_client = AipNlp(APP_ID, API_KEY, SECRET_KEY) # 调节发音的会泽的 # 第一个可以放要转化吃那个语音的文字 result = client.synthesis('', 'zh', 1, { "per": 4, # 表示是男音还是女音 "spd": 8, # 表示说话的速度 "pit": 6, "vol": 5, }) # 识别正确返回语音二进制 错误则返回dict ,这时返回的是文件文本 if not isinstance(result, dict): with open('auido.mp3', 'wb') as f: f.write(result) # 把二进制语音写入到文件中 # 定义一个读取文件的函数 def get_file_content(filePath): # 把wma格式的文件转化为.pcm格式的文件 os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.wma.pcm") # 把转化了的格式保存到当前目录 with open(f"{filePath}.wma.pcm", 'rb') as fp: # 返回这个文件读取的内容 return fp.read() # 并把这个文件返回给调用者 # get_file_content("cg.m4a") # 识别本地文件, 把本地的语音文件转成pcm个格式的文件并把语音转化成二进制文件 res = client.asr(get_file_content('xh.m4a'), 'pcm', 16000, { 'dev_pid': 1536, }) print(res, type(res)) # {'corpus_no': '6637053740205578210', 'err_msg': 'success.', 'err_no': 0, 'result': ['给我讲个笑话'], 'sn': '757488757051545309494'}, <class 'dict'> Q = res.get("result")[0] # 取到输入的主要内容 print(1,Q) # 1 给我讲个笑话 # 判断是不是问的是名字,是拿Q和"你叫什么"做相似度匹配如果大于0.7,则表明用户表达的是这个意思 if nlp_client.simnet(Q, "你叫什么?").get("score") >= 0.7: A = "我的名字叫雪雪" result = client.synthesis(A, "zh", 1, { "per": 4, "pit": 8, "spd": 4, "vol": 5, }) # 如果不存在result,就打开audio.mp3的文件 if not isinstance(result, dict): with open("audio.mp3", "wb") as f: f.write(result) os.system("audio.mp3") else: # 调用图灵机器人 import go_tuling # 传2个参数,一个是用户输入的内容,并赋值给A A = go_tuling.tl(Q, "asd") # 结果赋值给result,并读取这个文件 result = client.synthesis(A, "zh", 1, { "per": 4, "pit": 8, "spd": 4, "vol": 5, }) if not isinstance(result, dict): with open("audio.mp3", "wb") as f: f.write(result) os.system("audio.mp3")
调用图灵的代码:
import requests url = "http://openapi.tuling123.com/openapi/api/v2" data_dict = { "reqType": 0, "perception": { "inputText": { "text": "北京" }, }, "userInfo": { "apiKey": "96dfe320eec549519c5168093f93b2dc", "userId": "asd", } } def tl(text, uid): # 给字典赋值text,这个text是传过来的用户输入的内容 data_dict["perception"]["inputText"]["text"] = text # 并给字典赋值是哪个用户的要求 data_dict["userInfo"]["userInfo"] = uid # 把这个消息数据反送给图灵 res = requests.post(url, json=data_dict) # 会得到一个响应值,去json的方法 res_json = res.json() print("res:", res, type(res)) # res: <Response [200]> <class 'requests.models.Response'> print("res_json:", res_json,type(res_json)) # res_json: {'intent': {'actionName': '', 'code': 10006, 'intentName': ''}, 'results': [{'groupType': 1, 'resultType': 'text', 'values': {'text': '猪猪与爸爸 小猪与爸爸在谈话小猪说:爸爸为什么上个月有人来要钱你说没有,这个月那个人来要钱你说又没有?小猪爸爸:哎呀,爸爸要讲信用嘛!'}}]} <class 'dict'> # 返回图灵相应的数据 return res_json.get("results")[0]["values"]["text"]
来源:https://www.cnblogs.com/ljc-0923/p/10265894.html