Может ли кто-нибудь сказать, что не так с этим скриптом, который должен обрабатывать входной сигнал моего микрофона?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Может ли кто-нибудь сказать, что не так с этим скриптом, который должен обрабатывать входной сигнал моего микрофона?

Сообщение Anonymous »

Я пытаюсь создать приложение, которое записывает звук моего микрофона в один поток и создает 5-секундные фрагменты. Затем он помещает каждый из этих фрагментов в очередь, которая отправляет его во второй поток для расшифровки аудио в текст, а затем добавляет текст во вторую очередь. Наконец, он переходит к третьему потоку, который будет выполнять различные операции со строкой. На данный момент он просто печатает его в целях отладки.

Код: Выделить всё

import sounddevice as sd
import numpy as np
import threading
import queue
import time
from faster_whisper import WhisperModel
audio_model = WhisperModel(model_path)  # Initialize faster_whisper model
fs = 44100  # Sampling rate
audio_q = queue.Queue()  # Queue to communicate raw audio data between threads
text_q = queue.Queue()  # Queue to communicate transcribed text between threads
short_chunk_duration = 0.1  # Short chunk duration in seconds (e.g., 100ms)
long_chunk_duration = 5  # Desired chunk duration in seconds (5 seconds)

# --- Thread Functions ---
def record_audio():
"""Continuously records audio and adds 5-second chunks to the queue."""

accumulated_buffer = []

def callback(indata, frames, time, status):
nonlocal accumulated_buffer
if status:
print(status)

accumulated_buffer.append(indata.copy())

# Check if we have accumulated enough data for a 5-second chunk
if (
len(accumulated_buffer) * int(short_chunk_duration * fs)
>= long_chunk_duration * fs
):
# Concatenate the accumulated chunks into one array
long_chunk = np.concatenate(accumulated_buffer)
audio_q.put(long_chunk)  # Add the 5-second chunk to the queue
accumulated_buffer = []  # Reset the buffer

with sd.InputStream(samplerate=fs, channels=1, callback=callback):
while True:
sd.sleep(int(short_chunk_duration * 1000))

def transcribe_audio():
"""Takes audio from the queue, transcribes it, and puts text in the queue."""

while True:
audio_data = audio_q.get()
audio_array = np.frombuffer(audio_data, dtype=np.float32)

# Confirming audio_array fits the requirements of faster_whisper
if audio_array.ndim == 1:
audio_array = np.expand_dims(audio_array, axis=0)

# Transcribe with faster_whisper
segments, _ = audio_model.transcribe(audio_array)

# The result structure is different; let's collect all transcriptions
text = ""
for segment in segments:
text += segment.text

print(f"Transcribed: {text}")

audio_q.task_done()
text_q.put(text)  # Put transcribed text into the queue

def process_text():
pass
# --- Start Threads ---
if __name__ == "__main__":
recording_thread = threading.Thread(target=record_audio)
transcribing_thread = threading.Thread(target=transcribe_audio)
processing_thread = threading.Thread(target=process_text)

recording_thread.daemon = True
transcribing_thread.daemon = True
processing_thread.daemon = True

recording_thread.start()
transcribing_thread.start()
processing_thread.start()

try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Terminating threads...")
Он должен расшифровать мой микрофон частями по 5 секунд, а затем распечатать его на консоли, однако он выдает мне ошибку, сообщающую, что он пытается выделить 960 ГБ оперативной памяти для numpy. Я не часто использовал numpy, поэтому предполагаю, что это как-то связано с логикой, которая расширяет аудиообъект в массив.

Подробнее здесь: https://stackoverflow.com/questions/786 ... microphone
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»