Python流音频到Google云

2024-03-29 09:01:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图流音频在一个Int16数组格式从Flask服务器到谷歌云语音Api。由于某些原因,它会自动失败,可能与我如何使用流生成器或以错误的格式发送数据有关

googlecloud文档说这个流应该是一个产生大量音频数据的生成器。不太清楚,我试着造一个发电机。你知道吗

为什么我没有得到谷歌云的回应?你知道吗

这是主代码

@ws.route('/websocket')
def audio(ws):
    total_msg = ""
    sample_rate = 16000
    while True:
        def generator():
            msg = ws.receive()                  
            if msg is not None:
                audio_as_int_array = numpy.frombuffer(msg, 'i2')
                yield(audio_as_int_array)
        stream = [generator()]
        requests = (types.StreamingRecognizeRequest(audio_content=chunk)
                for chunk in stream)
        language_code = 'en-US'  # a BCP-47 language tag
        client = speech.SpeechClient()
        config = types.RecognitionConfig(
            encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz=sample_rate,
            language_code=language_code)
        streaming_config = types.StreamingRecognitionConfig(
        config=config,
        interim_results=True)

Tags: sampleconfigtruewsratedefas格式