语⾳活性检测器py-webrtcvad安装使⽤
⾕歌为WebRTC项⽬开发的VAD是⽬前最优秀、最先进和免费的产品之⼀。webrtcvad是WebRTC语⾳活动检测器(VAD)的python接⼝。兼容python2和python3。功能是将⼀段⾳频数据分为静⾳与⾮静⾳。它对于电话和语⾳识别很有⽤。
1、安装pip
yum -y install epel-release
yum -y install python-pip
2、安装webrtcvad
yum -y install python-devel
pip install webrtcvad
3、webrtcvad测试脚本(test_webrtcvad.py)
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.frames())
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
d(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')
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:])
[root@host-10-0-251-159 ~]# python test_webrtcvad.py 2 /home/73.wav
00000000000000000000000000000000000000000000000000000000000000000000000000111111+(2.1)11111111111111111111111100000000-(3.36)--end
00000000000111111+(3.57)111111111111111111111111111111111111100111111111111111111111111111111111111100000011111111111111111111111111111111111111111111111110111111111111000001111000000011111111111111111111
000000000000000000000000000000000000011+(15.3)111100000001110000-(16.14)--end 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111+(21.21)11111111111111111 00000000000111111+(22.68)111111111111111111111111111111111111111111100000000000-(24.6)--end
000000111111+(24.66)111111111111111111111111111111111111111111111111111110000000-(26.76)--end
1111111111+(26.76)1111111111111110000000000-(27.81)--endwebrtc浏览器
000000001111+(27.87)11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000-(31.38)--end
0001111111+(31.38)11111111111111111110111111111111111000000-(32.91)--end
00000001111111111111+(33.21)111000111111111111111111111111111111111110000000000-(35.04)--end
000000000000000000000000000111111+(35.73)1111111111111111111111111111111111111111111111111110000111111111111111111111111110000000111111111111111111111111111111111111111111111111111111111111111111111111111
000000000000000000000000000000000000000000000111111+(42.66)1111111111111111111110000000-(43.8)--end
000000001111111+(43.95)1111111111111111111111110011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 00000000111111+(51.15)1111111111111111111111111111111111111001111111111111111111111111111111111000000-(53.82)--end
0111111111+(53.82)111111111111111111111111111111111111111111110000111111111111111111111111110011111111111111111111111111111111111111111111111111110001111111111111111111111111111111111111111111111111111100 00000000000000000000000000111111+(60.51)11111111111111111111111111111111111000000111100111111111111111111111111111111111111111111111111111111111111111111111110011100000000-(64.74)--end 0000111000000000000000000001111111+(65.46)11111111111111111111111111111110011100000000000000-(67.26)--end
00000000111000000000111111+(67.74)111111111111111111111111111111111000100000000-(69.39)--end
00001111111+(69.42)11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100001111111111110001111111111111111111111111111111110000000-(74.55)--end 1111111111+(74.55)111111111111111111111111111111111111100000011111011111111111111111111111111111111111111111111111111111111111111111111111111111111111100111111111111111111111111111111111111111111111111111 0011111000000111111+(81.51)111111111111111111111111111111111111111111111111111111111111111111111100000000111111111111111111111111111111111111111111111111111111111111111001111111111111111111111111111111111 000000000001111111+(87.9)1111111111111111111111111111110111111000001100000000-(89.76)--end
000000000000000000000000000000000000000000000000111111+(91.08)1111111111111100000000-(92.04)--end
0000000000000111111+(92.31)11111111111111110111011111111111111111111111110001111111111111111111111111111111111111111000001111111111111111111111111111111111111111100000000-(96.9)--end 000000000000000111111+(97.23)11111111111111111111111111111111111111100111111001111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111100000000000000000-(102.27 000111000000111111+(102.51)111111111111111111111111111111111111111111111110000000-(104.43)--end
0000111111+(104.43)111111111111111111111111111111110000000-(105.9)--end
11100111100000000011111111+(106.38)111000000011111111111111111111111111111100000000-(108.12)--end
00001111000000000011110111111+(108.69)111111111111111111111111111111110000000-(110.16)--end
000000000000000000000000011100111000111111+(111.12)111111111111111111100001111111111111111111111111110000000-(113.13)--end
0001111111+(113.13)111111111111111111111111111111111110000010000000-(114.87)--end
0111011111+(114.87)1111111111111111111111111111111111100000011111111111111111111111111111111111111111111110110000000-(118.08)--end 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。