python實現人工智能之路--語音識別加語音合成
阿新 • • 發佈:2018-11-26
name lap ons nsa aud str play text not
代碼如下
from aip import AipSpeech import os APP_ID = ‘14940732‘ API_KEY = ‘tm1jAzv8dnDQM5yzFUk0ED9j‘ SECRET_KEY = ‘2WsPOg8k7GHtPnN2PBIsuf9LRpozYK8A‘ client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) def audio_to_text(filename): filename = filename.split(‘.‘)[0] os.system(f"ffmpeg -y -i {filename}.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filename}.pcmaudio_and_text") with open(f"{filename}.pcm", ‘rb‘) as fp: file_content = fp.read() result = client.asr(file_content, ‘pcm‘, 16000, {‘dev_pid‘: 1536}) print(result) result_str = result.get(‘result‘)[0] return result_str def text_to_audio(result_str): result= client.synthesis(result_str, ‘zh‘, 1, { ‘vol‘: 5, ‘spd‘: 3, ‘pit‘: 8, ‘per‘: 4 }) # 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼if not isinstance(result, dict): with open(‘audio.mp3‘, ‘wb‘) as f: f.write(result) os.system(‘audio.mp3‘) a = audio_to_text(‘audio.mp3‘) if ‘你好我的世界‘ in a: text_to_audio(a) else: text_to_audio(f‘你剛才是不是在說, {a}嗎?‘)
提前需下載pip install baidu=aip
以及ffmpeg地址如下https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg 提取碼為w6hk
python實現人工智能之路--語音識別加語音合成