如何在 Python 中进行实时语音活动检测?

Nic*_*eli 15 python speech-recognition speech speech-to-text pyaudio

我正在对录制的音频文件执行语音活动检测,以检测波形中的语音与非语音部分。

分类器的输出看起来像(突出显示的绿色区域表示语音):

在此处输入图片说明

我在这里面临的唯一问题是使其适用于音频输入流(例如:来自麦克风)并在规定的时间范围内进行实时分析。

我知道PyAudio可用于动态记录来自麦克风的语音,并且有几个波形、频谱、频谱图等的实时可视化示例,但找不到与以近乎实时的方式进行特征提取相关的任何内容.

igr*_*nis 17

您应该尝试使用 Python 绑定到Google 的 webRTC VAD。它是基于 GMM 建模的轻量级、快速且提供非常合理的结果。由于决策是按帧提供的,因此延迟最小。

# Run the VAD on 10 ms of silence. The result should be False.
import webrtcvad
vad = webrtcvad.Vad(2)

sample_rate = 16000
frame_duration = 10  # ms
frame = b'\x00\x00' * int(sample_rate * frame_duration / 1000)
print('Contains speech: %s' % (vad.is_speech(frame, sample_rate))
Run Code Online (Sandbox Code Playgroud)

另外,这篇文章可能对你有用。

  • 谢谢你的“奥迪托克”。不知道它的存在 (2认同)

Ang*_*Tay 5

我发现LibROSA可能是您问题的解决方案之一。Medium上有一个简单的教程,介绍使用 Microphone 流式传输实现实时预测。

让我们使用短时傅立叶变换 (STFT) 作为特征提取器,作者解释说:

为了计算 STFT,使用快速傅立叶变换窗口大小 (n_fft) 作为 512。根据方程 n_stft = n_fft/2 + 1,在 512 的窗口大小上计算了 257 个频率区间 (n_stft)。窗口移动了跳跃长度为 256,以便在计算 STFT 时有更好的窗口重叠。

stft = np.abs(librosa.stft(trimmed, n_fft=512, hop_length=256, win_length=512))

# Plot audio with zoomed in y axis
def plotAudio(output):
    fig, ax = plt.subplots(nrows=1,ncols=1, figsize=(20,10))
    plt.plot(output, color='blue')
    ax.set_xlim((0, len(output)))
    ax.margins(2, -0.1)
    plt.show()

# Plot audio
def plotAudio2(output):
    fig, ax = plt.subplots(nrows=1,ncols=1, figsize=(20,4))
    plt.plot(output, color='blue')
    ax.set_xlim((0, len(output)))
    plt.show()

def minMaxNormalize(arr):
    mn = np.min(arr)
    mx = np.max(arr)
    return (arr-mn)/(mx-mn)

def predictSound(X):
    clip, index = librosa.effects.trim(X, top_db=20, frame_length=512, hop_length=64) # Empherically select top_db for every sample
    stfts = np.abs(librosa.stft(clip, n_fft=512, hop_length=256, win_length=512))
    stfts = np.mean(stfts,axis=1)
    stfts = minMaxNormalize(stfts)
    result = model.predict(np.array([stfts]))
    predictions = [np.argmax(y) for y in result]
    print(lb.inverse_transform([predictions[0]])[0])
    plotAudio2(clip)

CHUNKSIZE = 22050 # fixed chunk size
RATE = 22050

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32, channels=1, 
rate=RATE, input=True, frames_per_buffer=CHUNKSIZE)

#preprocessing the noise around
#noise window
data = stream.read(10000)
noise_sample = np.frombuffer(data, dtype=np.float32)
print("Noise Sample")
plotAudio2(noise_sample)
loud_threshold = np.mean(np.abs(noise_sample)) * 10
print("Loud threshold", loud_threshold)
audio_buffer = []
near = 0

while(True):
    # Read chunk and load it into numpy array.
    data = stream.read(CHUNKSIZE)
    current_window = np.frombuffer(data, dtype=np.float32)
    
    #Reduce noise real-time
    current_window = nr.reduce_noise(audio_clip=current_window, noise_clip=noise_sample, verbose=False)
    
    if(audio_buffer==[]):
        audio_buffer = current_window
    else:
        if(np.mean(np.abs(current_window))<loud_threshold):
            print("Inside silence reign")
            if(near<10):
                audio_buffer = np.concatenate((audio_buffer,current_window))
                near += 1
            else:
                predictSound(np.array(audio_buffer))
                audio_buffer = []
                near
        else:
            print("Inside loud reign")
            near = 0
            audio_buffer = np.concatenate((audio_buffer,current_window))

# close stream
stream.stop_stream()
stream.close()
p.terminate()
Run Code Online (Sandbox Code Playgroud)

代码来源:Chaturanga Siriwardhana

完整代码可以在这里找到。