人工智能---语音合成识别图灵机械人语音交互
Posted kevin-red-heart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了人工智能---语音合成识别图灵机械人语音交互相关的知识,希望对你有一定的参考价值。
1 """ 你的 APPID AK SK """ 2 import os 3 from aip import AipSpeech, AipNlp 4 from ss import tuling 5 from ss.录音 import rec 6 7 APP_ID = ‘16027699‘ 8 API_KEY = ‘rZrseMjmXXHadrp0fbUAyb6Z‘ 9 SECRET_KEY = ‘0tFVGEW09PpPCMxXBqVrda59fQf3wY3c‘ 10 11 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) 12 client_NLp = AipNlp(APP_ID, API_KEY, SECRET_KEY) 13 14 15 #文字合成语音 16 def text_audio(text): 17 result = client.synthesis(text, ‘zh‘, 1, { 18 ‘vol‘: 5, 19 ‘per‘: 4, 20 ‘pit‘: 8, 21 ‘spd‘: 5, 22 }) 23 24 # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 25 if not isinstance(result, dict): 26 with open(‘audios1.mp3‘, ‘wb‘) as f: 27 f.write(result) 28 return ‘audios1.mp3‘ 29 30 #语音识别--文字 31 def audio_text(filename): 32 # 识别本地文件 33 res = client.asr(get_file_content(filename), ‘pcm‘, 16000, { 34 ‘dev_pid‘: 1536, 35 }) 36 return res.get(‘result‘)[0] 37 38 39 # -转换格式--读取文件 40 def get_file_content(filePath): 41 os.system(f"ffmpeg -y -i {filePath}.wav -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm ") 42 with open(f‘{filePath}.pcm‘, ‘rb‘) as fp: 43 return fp.read() 44 45 46 if __name__ == ‘__main__‘: 47 48 re=rec(‘file_name.wav‘) #录音,形成录音文件 49 text=audio_text(‘file_name‘) #录音转为文字 50 # print(text) 51 # score =client_NLp.simnet(text,‘今天天气‘).get(‘score‘) 52 # print(score) 53 # if score>0.58: 54 # print(‘1111‘) 55 # filename=text_audio(‘是呀,撩起呀!‘) 56 # print(filename) 57 # # os.system(f"ffplay {filename}") 58 # os.system(filename) 59 60 answer=tuling.get_help_from_tuling(text,‘mabing‘) #文字信息传递给tuling得到答复信息 61 filename = text_audio(answer) #答复信息再转为语音 62 os.system(filename) #播放
另外还有几个.py文件
tuling.py #与图灵机器人连接
1 import requests 2 3 def get_help_from_tuling(text,uid): 4 URL = ‘http://openapi.tuling123.com/openapi/api/v2‘ 5 6 data = { 7 "perception": { 8 "inputText": { 9 "text": ‘‘ 10 } 11 }, 12 "userInfo": { 13 "apiKey": "86fd93bd29e941eebe314766616a3f15", 14 "userId": "123456" 15 } 16 } 17 data["perception"]["inputText"]["text"] = text 18 data["userInfo"]["userId"] = uid 19 res = requests.post(URL, json=data) 20 print(res.json()) 21 return res.json().get(‘results‘)[0].get(‘values‘).get(‘text‘)
录音文件,通过执行函数,就会形成语音文件
1 import pyaudio 2 import wave 3 4 CHUNK = 1024 5 FORMAT = pyaudio.paInt16 6 CHANNELS = 2 7 RATE = 16000 8 RECORD_SECONDS = 6 9 # WAVE_OUTPUT_FILENAME = "Oldboy.wav" 10 def rec(file_name): 11 p = pyaudio.PyAudio() 12 13 stream = p.open(format=FORMAT, 14 channels=CHANNELS, 15 rate=RATE, 16 input=True, 17 frames_per_buffer=CHUNK) 18 19 print("开始录音,请说话......") 20 21 frames = [] 22 23 for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): 24 data = stream.read(CHUNK) 25 frames.append(data) 26 27 print("录音结束,请闭嘴!") 28 29 stream.stop_stream() 30 stream.close() 31 p.terminate() 32 33 wf = wave.open(file_name, ‘wb‘) 34 wf.setnchannels(CHANNELS) 35 wf.setsampwidth(p.get_sample_size(FORMAT)) 36 wf.setframerate(RATE) 37 wf.writeframes(b‘‘.join(frames)) 38 wf.close()
以上是关于人工智能---语音合成识别图灵机械人语音交互的主要内容,如果未能解决你的问题,请参考以下文章