这个版本基本完成了VAD+SAR
这个提交包含在:
128
R2T.py
128
R2T.py
@ -4,61 +4,81 @@ import sherpa_onnx
|
||||
|
||||
from config import Config
|
||||
|
||||
# 配置参数
|
||||
sample_rate = 16000
|
||||
is_recording = False
|
||||
audio_data = []
|
||||
sample_rate = 16000 # 必须与VAD和识别器配置的采样率匹配
|
||||
block_size = 512 # 每次回调处理的样本数,较小的值延迟低,但CPU占用高
|
||||
|
||||
def audio_callback(indata, frames, time_info, status):
|
||||
"""音频回调函数"""
|
||||
# global audio_data
|
||||
audio_data.extend(indata[:, 0].copy())
|
||||
vadconfig = sherpa_onnx.VadModelConfig()
|
||||
vadconfig.silero_vad.model = Config.vad
|
||||
vadconfig.silero_vad.threshold = 0.5
|
||||
vadconfig.silero_vad.min_silence_duration = 0.5 # seconds
|
||||
vadconfig.silero_vad.min_speech_duration = 0.25 # seconds
|
||||
vadconfig.silero_vad.max_speech_duration = 8 # seconds
|
||||
vadconfig.sample_rate = sample_rate
|
||||
|
||||
vad = sherpa_onnx.VoiceActivityDetector(vadconfig,buffer_size_in_seconds=85)
|
||||
|
||||
is_recording = False
|
||||
samples = np.array([], dtype=np.float32) # 用于累积一个语音段的样本
|
||||
|
||||
recognizer = sherpa_onnx.OfflineRecognizer.from_paraformer(
|
||||
tokens = Config.tokens,
|
||||
paraformer= Config.paraformer
|
||||
)
|
||||
|
||||
def audio_callback(indata, frames, time, status):
|
||||
global is_recording, samples
|
||||
|
||||
# indata 是一个 (frames, channels) 的 numpy 数组
|
||||
# 我们假设是单声道,并将其转换为一维数组
|
||||
audio_data = indata[:, 0].astype(np.float32)
|
||||
|
||||
# 1. 将音频数据送入 VAD
|
||||
vad.accept_waveform(audio_data)
|
||||
|
||||
# 2. 检查当前音频块是否有语音
|
||||
if vad.is_speech_detected():
|
||||
if not is_recording:
|
||||
# 语音刚开始,重置累积的样本
|
||||
print("Speech started, recording...")
|
||||
samples = np.array([], dtype=np.float32)
|
||||
is_recording = True
|
||||
|
||||
# 无论是否刚开始,有语音就累积数据
|
||||
samples = np.concatenate((samples, audio_data))
|
||||
|
||||
else:
|
||||
# 当前块没有检测到语音
|
||||
if is_recording:
|
||||
# 之前正在录制,说明语音段刚刚结束
|
||||
print("Speech ended. Decoding...")
|
||||
is_recording = False
|
||||
|
||||
# 3. 将累积的整个语音段样本传入识别器进行解码
|
||||
# 注意:sherpa_onnx的OfflineRecognizer使用decode()处理整个数组
|
||||
stream = recognizer.create_stream()
|
||||
stream.accept_waveform(sample_rate=16000, waveform=samples)
|
||||
recognizer.decode_stream(stream)
|
||||
result = stream.result.text
|
||||
print(f"Recognized: {result}")
|
||||
|
||||
# 可选:清空累积的样本以备下次使用
|
||||
samples = np.array([], dtype=np.float32)
|
||||
|
||||
def Record2Text():
|
||||
global is_recording, audio_data
|
||||
# 创建识别器
|
||||
recognizer = sherpa_onnx.OfflineRecognizer.from_paraformer(
|
||||
tokens = Config.tokens,
|
||||
paraformer= Config.paraformer
|
||||
)
|
||||
|
||||
try:
|
||||
print("1")
|
||||
# 打开音频流
|
||||
stream = sd.InputStream(
|
||||
samplerate=sample_rate,
|
||||
channels=1,
|
||||
dtype='float32',
|
||||
blocksize=1024,
|
||||
callback=audio_callback
|
||||
)
|
||||
|
||||
with stream:
|
||||
audio_data = []
|
||||
print("录音中... 请说话")
|
||||
input("按回车结束录音...")
|
||||
print("处理中...")
|
||||
|
||||
# 处理录音数据
|
||||
if audio_data:
|
||||
s = recognizer.create_stream()
|
||||
s.accept_waveform(sample_rate, np.array(audio_data, dtype=np.float32))
|
||||
recognizer.decode_stream(s)
|
||||
|
||||
result = s.result
|
||||
if result and result.text and result.text.strip():
|
||||
print(f"\n识别结果: {result.text}")
|
||||
return result.text
|
||||
else:
|
||||
print("\n未识别到有效语音")
|
||||
return ""
|
||||
else:
|
||||
print("\n没有录制到音频")
|
||||
return ""
|
||||
|
||||
pass
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n程序退出")
|
||||
|
||||
except Exception as e:
|
||||
print(f"发生错误: {e}")
|
||||
try:
|
||||
with sd.InputStream(callback=audio_callback,
|
||||
channels=1,
|
||||
samplerate=sample_rate,
|
||||
blocksize=block_size,
|
||||
dtype='float32'):
|
||||
print("Recording started. Press Enter to stop.")
|
||||
input() # 阻塞,等待用户输入以停止
|
||||
except KeyboardInterrupt:
|
||||
print("用户输入中断")
|
||||
|
||||
print("Recording stopped.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
Record2Text()
|
||||
17
R2Test.py
普通文件
17
R2Test.py
普通文件
@ -0,0 +1,17 @@
|
||||
import sounddevice as sd
|
||||
import numpy as np
|
||||
import sherpa_onnx
|
||||
|
||||
from config import Config
|
||||
|
||||
recognizer = sherpa_onnx.OfflineRecognizer.from_paraformer(
|
||||
tokens = Config.tokens,
|
||||
paraformer= Config.paraformer
|
||||
)
|
||||
|
||||
InS = sd.InputStream(
|
||||
samplerate=16000,
|
||||
channels=1,
|
||||
dtype='float32',
|
||||
blocksize=1024
|
||||
)
|
||||
在新工单中引用
屏蔽一个用户