Вы должны иметь возможность создавать несколько потоков потокового , используя один и тот же клиент StreamingRecognize()
, который можно использовать для параллельной отправки запросов. Вы можете посмотреть сообщения , и , Github, где обсуждается эта тема c.
Я предлагаю вам попробовать этот вариант и проверить, можете ли вы выполнять эти потоковые вызовы, создав 2 различных объекта или клиента, таких как:
client = speech.SpeechClient()
responses = client.streaming_recognize()
responses2 = client.streaming_recognize()
С другой стороны, если вы хотите для распознавания звука в пакетном режиме рекомендуется использовать синхронный или асинхронный метод.
Обновление. Добавление примера с несколькими потоками в python
import io
import os
import copy
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
import threading
# Instantiates a client
# The name of the audio file to transcribe
file_name = os.path.join(
os.path.dirname(__file__),
'resources',
<Audio_File_Name>)
#Using same client
client = speech.SpeechClient()
def streaming(thread):
#Using different clients
#client = speech.SpeechClient()
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
content2 = copy.deepcopy(content)
# In practice, stream should be a generator yielding chunks of audio data.
stream = [content]
requests = (types.StreamingRecognizeRequest(audio_content=chunk)
for chunk in stream)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code='en-US',
max_alternatives=1,
enable_automatic_punctuation=True,
model='video',
enable_word_time_offsets=True)
streaming_config = types.StreamingRecognitionConfig(
config=config,
interim_results=True)
# Detects speech in the audio file
responses = client.streaming_recognize(streaming_config, requests)
for response in responses:
for result in response.results:
print('{}: Transcript: {}'.format(thread, result.alternatives[0].transcript))
#print('isFinal: {}'.format(result.is_final))
thread1 = threading.Thread(name="thread1", target=streaming, args=('1',))
thread1.start()
thread2 = threading.Thread(name="thread2", target=streaming, args=('2',))
thread2.start()
print(threading.enumerate())
thread1.join()
thread2.join()
exit(0)