0

所以,问题是标题,我可以为一个用户创建语音到文本流,效果很好,但是当我尝试连接第二个用户时,它对所有人都不好,有时它对一个用户很好,所以问题是,可以我使用一个 GOOGLE_APPLICATION_CREDENTIALS 同时为两个或多个用户创建语音到文本流,还是每个用户都需要开始自己的项目?

4

1 回答 1

2

您应该能够使用同一个客户端创建多个StreamingRecognize()线程,这些客户端可用于并行发送请求。你可以看看这个这个Github 上讨论这个话题的帖子。

我建议您尝试这种替代方法并验证您是否可以通过创建 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)
于 2020-02-28T04:31:17.050 回答