-
大小: 7KB文件類型: .py金幣: 1下載: 1 次發(fā)布日期: 2021-06-08
- 語(yǔ)言: Python
- 標(biāo)簽: 語(yǔ)音識(shí)別??api??
資源簡(jiǎn)介
百度語(yǔ)音api實(shí)現(xiàn)語(yǔ)音識(shí)別小程序,通過(guò)判斷當(dāng)前音量大小自動(dòng)識(shí)別判斷是否該結(jié)束錄音,原理還是挺簡(jiǎn)單的,就是遇到一些小坑,自己學(xué)習(xí)了也分享給大家
代碼片段和文件信息
#!/usr/bin/env?python
#?-*-?coding:?UTF-8?-*-
from?time?import?sleep
from?aip?import?AipSpeech
from?numpy?import?frombuffershort
import?wave
#from?wave?import?open??#這樣寫就重名了
from?pyaudio?import?PyAudiopaInt16
#import?pyaudio
from?threading?import?Thread
import?pygame
import?os
import?tkinter
#由于百度語(yǔ)音識(shí)別最大時(shí)長(zhǎng)為60s,所以我們創(chuàng)建這個(gè)計(jì)時(shí)的方法
timeout?=?False
def?timeclock():
global?timeout
timeout?=?True
#循環(huán)里面的全局變量要加global
for?x?in?range(60):
print(‘ticking...‘?timeout)
sleep(1)
#如果標(biāo)志位為False,則停止計(jì)時(shí)
if?timeout?==?False:
return?0
#超過(guò)60秒賦值為false,停止錄音
timeout?=?False
#錄音
def?my_record(path?=?‘01.wav‘):
#規(guī)定聲音屬性
framerate=16000 #采樣頻率
NUM_SAMPLES=2000 #內(nèi)部緩存塊的大小,每次讀取的采樣數(shù)據(jù)塊的個(gè)數(shù)
channels=1 #聲道
sampwidth=2 #采樣大小/采樣寬度/位深2B?16bit
pa=PyAudio()
#創(chuàng)建輸入流
stream=pa.open(format?=?paInt16channels=1
rate=framerateinput=True
frames_per_buffer=NUM_SAMPLES)
#help(stream)
Buf_Data=[]
‘‘‘
#定時(shí)錄制語(yǔ)音模式,?錄音時(shí)間公式:1/頻率*采樣點(diǎn)數(shù)*20?=?1/16000*2000*20?=?2.5s
for?count?in?range(20):
string_audio_data?=?stream.read(NUM_SAMPLES)
#print(string_audio_data?‘\n‘)
#將錄制的數(shù)據(jù)存放到數(shù)組中
Buf_Data.append(string_audio_data)
print(‘....‘)
‘‘‘
#按需錄音模式,連續(xù)2.5秒不出聲,就停止錄音
#測(cè)試:
#datalistfile?=?‘‘
coutif?=?0
print(‘正在聆聽.....‘)
#計(jì)時(shí)功能啟動(dòng)
tc?=?Thread(target?=?timeclock?name?=?‘loopwall‘)
tc.deamon?=?True
tc.start()
#剛開始錄音的時(shí)候可能有比較長(zhǎng)的時(shí)間做準(zhǔn)備,所以我們先錄制1.25秒鐘
for?xtime?in?range(10):
string_audio_data?=?stream.read(NUM_SAMPLES)
Buf_Data.append(string_audio_data)
print(‘..‘)
#一般人這時(shí)候已經(jīng)開始說(shuō)話了
#當(dāng)沒(méi)超時(shí)錄音
global?timeout
while?timeout:
#循環(huán)一圈用時(shí)約0.125s
string_audio_data?=?stream.read(NUM_SAMPLES)
Buf_Data.append(string_audio_data)
print(‘..‘)
#將數(shù)據(jù)轉(zhuǎn)換為數(shù)組
data_list?=?frombuffer(string_audio_data?dtype=short)
#判斷是否有聲音
if?max(data_list)<5000: #5000:分貝閾值,小于5000視為環(huán)境噪音或靜音
coutif?+=?1
else:
coutif?=?0
#如果連續(xù)15個(gè)采樣點(diǎn)都小于5000,退出循環(huán),即連續(xù)1/16000*2000*15=1.875秒沒(méi)聲,就不錄音了
if?coutif?>?15:
timeout?=?False
break
#錄音結(jié)束
#timeout?=?True
‘‘‘
#用于測(cè)試
datalistfile?+=?str(max(data_list))
datalistfile?+=?‘\t‘
with?open(‘datalist.txt‘?‘a(chǎn)‘)?as?fd:
datalistfile?+=?‘\n‘
fd.write(datalistfile)
‘‘‘
#將存儲(chǔ)的數(shù)據(jù)保存成wav文件
#filepath?=?r‘E:\\python?practice\\{}.wav‘.format(time.strftime(“%Y%m%d%H%M%S“))
wf=wave.open(path‘wb‘) #注意,文件名中不能帶:等特殊符號(hào)
#wf=wave.open(‘.\\myrecord\\{}.wav‘.format(time.strftime(“%Y%m%d%H:%M:%S“))‘wb‘)
#設(shè)置聲道、采樣頻率、采樣大小
wf.setnchannels(channels)
#help(wave)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(b““.join(Buf_Data))
wf.close()
stream.close()
#chunk=2014
#播放wav文件
def?play_wav(path?=?‘01.wav‘):
wf=wave.open(path‘rb‘)
p=PyAudio()
#創(chuàng)建輸出流
stream=p.open(format=p.get_format_from_width(wf.getsampwidth())channels=
wf.getnchannels()rate=wf.getfr
評(píng)論
共有 條評(píng)論