安装使用webrtcvad
2019-07-24 本文已影响0人
Colleen_oh
webrtc 的vad使用GMM(Gaussian Mixture Model)对语音和噪声建模,通过相应的概率来判断语音和噪声。这种算法是无监督的,不需要严格的训练。
参考:https://www.cnblogs.com/zhenyuyaodidiao/p/9288455.html
环境:linux 7
安装依赖
yum -y install epel-release
yum -y install python-pip
yum -y install python-devel
pip install webrtcvad
安装webrtcvad时出现以下报错:
unable to execute 'gcc': No such file or directory
error: command 'gcc' failed with exit status 1
我的解决方法如下,安装gcc:
yum install -y libffi-devel openssl-devel #安装其他依赖
yum -y install gcc#安装gcc
然后再次安装
pip install webrtcvad
成功了
使用webrtvad
代码是从上面的参考上摘抄下来的。
import collections
import contextlib
import sys
import wave
import webrtcvad
def read_wave(path):
with contextlib.closing(wave.open(path, 'rb')) as wf:
num_channels = wf.getnchannels()
assert num_channels == 1
sample_width = wf.getsampwidth()
assert sample_width == 2
sample_rate = wf.getframerate()
assert sample_rate in (8000, 16000, 32000)
pcm_data = wf.readframes(wf.getnframes())
return pcm_data, sample_rate
def write_wave(path, audio, sample_rate):
with contextlib.closing(wave.open(path, 'wb')) as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(audio)
class Frame(object):
def __init__(self, bytes, timestamp, duration):
self.bytes = bytes
self.timestamp = timestamp
self.duration = duration
def frame_generator(frame_duration_ms, audio, sample_rate):
n = int(sample_rate * (frame_duration_ms / 1000.0) * 2)
offset = 0
timestamp = 0.0
duration = (float(n) / sample_rate) / 2.0
while offset + n < len(audio):
yield Frame(audio[offset:offset + n], timestamp, duration)
timestamp += duration
offset += n
def vad_collector(sample_rate, frame_duration_ms,
padding_duration_ms, vad, frames):
num_padding_frames = int(padding_duration_ms / frame_duration_ms)
ring_buffer = collections.deque(maxlen=num_padding_frames)
triggered = False
voiced_frames = []
for frame in frames:
sys.stdout.write(
'1' if vad.is_speech(frame.bytes, sample_rate) else '0')
if not triggered:
ring_buffer.append(frame)
num_voiced = len([f for f in ring_buffer
if vad.is_speech(f.bytes, sample_rate)])
if num_voiced > 0.9 * ring_buffer.maxlen:
sys.stdout.write('+(%s)' % (ring_buffer[0].timestamp,))
triggered = True
voiced_frames.extend(ring_buffer)
ring_buffer.clear()
else:
voiced_frames.append(frame)
ring_buffer.append(frame)
num_unvoiced = len([f for f in ring_buffer
if not vad.is_speech(f.bytes, sample_rate)])
if num_unvoiced > 0.9 * ring_buffer.maxlen:
sys.stdout.write('-(%s)' % (frame.timestamp + frame.duration))
triggered = False
yield b''.join([f.bytes for f in voiced_frames])
ring_buffer.clear()
voiced_frames = []
if triggered:
sys.stdout.write('-(%s)' % (frame.timestamp + frame.duration))
sys.stdout.write('\n')
if voiced_frames:
yield b''.join([f.bytes for f in voiced_frames])
def main(args):
if len(args) != 2:
sys.stderr.write(
'Usage: example.py <aggressiveness> <path to wav file>\n')
sys.exit(1)
audio, sample_rate = read_wave(args[1])
vad = webrtcvad.Vad(int(args[0]))
frames = frame_generator(30, audio, sample_rate)
frames = list(frames)
segments = vad_collector(sample_rate, 30, 300, vad, frames)
for i, segment in enumerate(segments):
#path = 'chunk-%002d.wav' % (i,)
print('--end')
#write_wave(path, segment, sample_rate)
if __name__ == '__main__':
main(sys.argv[1:])
把上面的代码存为webrtc_vad.py文件,然后再linux下运转。下面代码中的2是敏感系数,vad检测的敏感系数共四种模式,用数字0~3来区分,激进程度与数值大小正相关。0: Normal,1:low Bitrate, 2:Aggressive;3:Very Aggressive 可以根据实际更改。;第二个参数为wav文件存放路径,目前仅支持8K,16K,32K的采样率。
python3 webrtc_vad.py 2 123456_1.wav
转成功后。会有以下结果
1111111111+(0.0)111111111111111000011111111111111111111111111111111111111111111111111111111111111111111101111111111111111000011111111111111111111111111111111111111111111111-(4.979999999999997)
--end