Чего я пытаюсь достичь:
- Непрерывно записывать звук с помощью PyAudio и проверять, есть ли в аудиофрагментах речь, с помощью Silero VAD.
- Аудиофрагменты, содержащие речь, будут добавляться в список до тех пор, пока VAD не обнаружит звук. кусок без речь.
- Список аудиофрагментов с речью будет объединен в виде массива numpy и отправлен в WhisperX для транскрипции.
Повторяйте процесс, пока клавиатура не будет прервана.< /li>
Код: Выделить всё
device = "cuda" if torch.cuda.is_available() else "cpu"
model = load_silero_vad()
vad_iterator = VADIterator(model)
whisper_model = whisperx.load_model("base", device=device, compute_type="float32", language='en')
# initialize pyaudio
p = pyaudio.PyAudio()
# Define audio stream parameters
FORMAT = pyaudio.paInt16 # 16-bit format
CHANNELS = 1 # Mono audio
RATE = 16000 # Sampling rate (Hz)
CHUNK = 512 # Buffer size
# Open audio stream
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
speech_buffer = []
SILENCE_THRESHOLD = 80 # Number of consecutive non-speech frames to tolerate
silence_count = 0 # Counter for consecutive non-speech frames
try:
while True:
# Read audio data from the stream
audio_data = stream.read(CHUNK)
# Convert audio data to numpy array and normalize to float32
audio_np = np.frombuffer(audio_data, dtype=np.int16).astype(np.float32) / 32768.0
# Apply VAD to detect speech
speech_dict = vad_iterator(audio_np, RATE)
if speech_dict:
silence_count = 0
# Speech detected
print("Speech detected!")
speech_buffer.append(audio_np)
else: # no speech
silence_count += 1
print("Speech not detected.")
print(silence_count)
print("length of speech buffer: ", len(speech_buffer))
# Process audio if silence count exceeds threshold and there's buffered audio
if silence_count > SILENCE_THRESHOLD and speech_buffer:
print("Processing audio.")
speech_audio = np.concatenate(speech_buffer)
print(speech_audio)
# # whiserpx
result = whisper_model.transcribe(speech_audio)
print("Transcription: ", result)
# reset buffer
speech_buffer = []
except KeyboardInterrupt:
print("Terminating...")
finally:
# Close the stream gracefully
stream.stop_stream()
stream.close()
p.terminate()
Код: Выделить всё
ReproducibilityWarning: TensorFloat-32 (TF32) has been disabled as it might lead to reproducibility issues and lower accuracy.
It can be re-enabled by calling
>>> import torch
>>> torch.backends.cuda.matmul.allow_tf32 = True
>>> torch.backends.cudnn.allow_tf32 = True
See https://github.com/pyannote/pyannote-audio/issues/1370 for more details.
warnings.warn(
Could not locate cudnn_ops_infer64_8.dll. Please make sure it is in your library path!
Я хотел бы знать, правильна ли моя логика или я что-то упускаю.
И как это исправить? мой код, чтобы транскрипция работала?
Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/793 ... -vad-and-w