是否可以从一个帐户同时执行两个语音到文本流? [英] Is it possible to perform two speech-to-text streams at same time from one account?

本文介绍了是否可以从一个帐户同时执行两个语音到文本流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,问题就在标题上,我可以为一个用户创建语音到文本流,效果很好,但当我尝试连接第二个用户时,它对所有人都不好,有时对一个用户很好,所以问题是,我是否可以使用一个Google_APPLICATION_Credentials同时为两个或更多用户创建Sppech到文本流,或者每个用户都需要启动自己的项目?

推荐答案

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

我建议您尝试此方法,并验证您是否可以通过创建两个不同的对象或客户端来执行这些流调用,例如:

client = speech.SpeechClient()

responses = client.streaming_recognize()
responses2 = client.streaming_recognize()

另一方面,如果您想对批量进行音频识别,建议使用synchronousasynchronous方法。

更新。在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)

这篇关于是否可以从一个帐户同时执行两个语音到文本流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆