You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below function get the input_device i.e device_index to listen the input stream, Is it possible to run this function in two threads and get the input from two different device index.
I have tried it in streamlit but it is not working since streamlit have issues with multi threading
In python, it is not working as expected for two input streams while we run through threading
Thank you in advance
`
def speech_to_text(input_device):
# The last time a recording was retrieved from the queue.
phrase_time = None
# Thread safe Queue for passing data from the threaded recording callback.
data_queue = Queue()
# We use SpeechRecognizer to record our audio because it has a nice feature where it can detect when speech ends.
recorder = sr.Recognizer()
recorder.energy_threshold = 4000
recorder.dynamic_energy_threshold = False
if 'linux' in platform:
mic_name = args.default_microphone
if not mic_name or mic_name == 'list':
print("Available microphone devices are: ")
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print(f"Microphone with name \"{name}\" found")
return
else:
for index, name in enumerate(sr.Microphone.list_microphone_names()):
if mic_name in name:
source = sr.Microphone(sample_rate=16000, device_index=index)
break
else:
device = input_device
source = sr.Microphone(sample_rate=16000, device_index=device)
print(input_device)
record_timeout = 3
phrase_timeout = 3
transcription = ['']
with source:
recorder.adjust_for_ambient_noise(source)
def record_callback(_, audio:sr.AudioData) -> None:
"""
Threaded callback function to receive audio data when recordings finish.
audio: An AudioData containing the recorded bytes.
"""
# Grab the raw bytes and push it into the thread safe queue.
data = audio.get_raw_data()
data_queue.put(data)
# Create a background thread that will pass us raw audio bytes.
# We could do this manually but SpeechRecognizer provides a nice helper.
recorder.listen_in_background(source, record_callback, phrase_time_limit=record_timeout)
# Cue the user that we're ready to go.
print("Model loaded.\n")
st.session_state['run'] = True
while st.session_state['run']:
try:
now = datetime.utcnow()
# Pull raw recorded audio from the queue.
if not data_queue.empty():
phrase_complete = False
# If enough time has passed between recordings, consider the phrase complete.
# Clear the current working audio buffer to start over with the new data.
if phrase_time and now - phrase_time > timedelta(seconds=phrase_timeout):
phrase_complete = True
# This is the last time we received new audio data from the queue.
phrase_time = now
# Combine audio data from queue
audio_data = b''.join(data_queue.queue)
data_queue.queue.clear()
# Convert in-ram buffer to something the model can use directly without needing a temp file.
# Convert data from 16 bit wide integers to floating point with a width of 32 bits.
# Clamp the audio stream frequency to a PCM wavelength compatible default of 32768hz max.
audio_np = np.frombuffer(audio_data, dtype=np.int16).astype(np.float32) / 32768.0
# Read the transcription.
if selected_language:
print(language_codes[selected_language])
#whisper plus
start = datetime.now()
result = model(audio_path=audio_np, language="english",return_timestamps=False,)
end = datetime.now()
print(end-start)
text = result['text'].strip()
# If we detected a pause between recordings, add a new item to our transcription.
# Otherwise edit the existing one.
if phrase_complete:
transcription.append(text)
else:
transcription[-1] = text
print(text)
print(sr.Microphone.list_working_microphones())
# return final_text
else:
# Infinite loops are bad for processors, must sleep.
sleep(0.25)
except KeyboardInterrupt:
break
`
Below snippet to run in python thread1 = threading.Thread(target = speech_to_text(0) ).start() thread2 = threading.Thread(target = speech_to_text(2) ).start() thread1.join() thread2.join() print("waiting")
The text was updated successfully, but these errors were encountered:
Steps to reproduce
Below function get the input_device i.e device_index to listen the input stream, Is it possible to run this function in two threads and get the input from two different device index.
I have tried it in streamlit but it is not working since streamlit have issues with multi threading
In python, it is not working as expected for two input streams while we run through threading
Thank you in advance
`
def speech_to_text(input_device):
# The last time a recording was retrieved from the queue.
phrase_time = None
# Thread safe Queue for passing data from the threaded recording callback.
data_queue = Queue()
# We use SpeechRecognizer to record our audio because it has a nice feature where it can detect when speech ends.
recorder = sr.Recognizer()
recorder.energy_threshold = 4000
recorder.dynamic_energy_threshold = False
`
Below snippet to run in python
thread1 = threading.Thread(target = speech_to_text(0) ).start() thread2 = threading.Thread(target = speech_to_text(2) ).start() thread1.join() thread2.join() print("waiting")
The text was updated successfully, but these errors were encountered: