Python結(jié)合百度語(yǔ)音識(shí)別實(shí)現(xiàn)實(shí)時(shí)翻譯軟件的實(shí)現(xiàn)
pip install PyAudiopip install SpeechRecognitionpip install baidu-aippip install Wavepip install Wheelpip install Pyinstaller二、百度官網(wǎng)申請(qǐng)服務(wù)
import pyaudioimport wavefrom aip import AipSpeechimport time# 用Pyaudio庫(kù)錄制音頻# out_file:輸出音頻文件名# rec_time:音頻錄制時(shí)間(秒)def audio_record(out_file, rec_time): CHUNK = 1024 FORMAT = pyaudio.paInt16 # 16bit編碼格式 CHANNELS = 1 # 單聲道 RATE = 16000 # 16000采樣頻率 p = pyaudio.PyAudio() # 創(chuàng)建音頻流 stream = p.open(format=FORMAT, # 音頻流wav格式 channels=CHANNELS, # 單聲道 rate=RATE, # 采樣率16000 input=True, frames_per_buffer=CHUNK) print('開始記錄語(yǔ)音{0}秒后開始識(shí)別...'.format(rec_time)) frames = [] # 錄制的音頻流 # 錄制音頻數(shù)據(jù) for i in range(0, int(RATE / CHUNK * rec_time)): data = stream.read(CHUNK) frames.append(data) # 錄制完成 stream.stop_stream() stream.close() p.terminate() print('結(jié)束識(shí)別') # 保存音頻文件 wf = wave.open(out_file, ’wb’) wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b’’.join(frames)) wf.close()def audio_recog(recogFile): # 讀取文件 def get_file_content(filePath): with open(filePath, ’rb’) as fp: return fp.read() # 識(shí)別本地文件 result = client.asr(get_file_content(recogFile), ’wav’, 16000, {’dev_pid’: 1537,}) return resultdef write_file(file,text): import time time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) f = open(file, ’a’) f.write(time+’:’+text+’n’) f.close()audioFile='audio.wav'textFile='識(shí)別結(jié)果.txt'''' 你的 APPID AK SK '''APP_ID = ’你的APP_ID’API_KEY = ’你的API_KEY’SECRET_KEY = ’你的SECRET_KEY’client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)if __name__ == ’__main__’: while True: audio_record(audioFile, 5) textResult = audio_recog('audio.wav') if textResult[’err_msg’] =='success.': print(textResult[’result’]) write_file(textFile,str(textResult[’result’]))四、打包成軟件
進(jìn)入到目錄執(zhí)行下面命令:
pyinstaller -F main.py
到此這篇關(guān)于Python結(jié)合百度語(yǔ)音識(shí)別實(shí)現(xiàn)實(shí)時(shí)翻譯軟件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python 實(shí)時(shí)翻譯軟件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JavaScript Tab菜單實(shí)現(xiàn)過程解析2. python opencv 實(shí)現(xiàn)讀取、顯示、寫入圖像的方法3. javascript設(shè)計(jì)模式 ? 建造者模式原理與應(yīng)用實(shí)例分析4. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢5. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容6. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)7. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考8. Android實(shí)現(xiàn)圖片自動(dòng)切換功能(實(shí)例代碼詳解)9. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟10. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼
