当前位置: 首页 > 知识库问答 >
问题:

google cloud speech api同步语音识别文档出错

鲜于凯康
2023-03-14

我从https://cloud.google.com/speech/docs/sync-recognize的谷歌云语音api同步语音识别文档中复制了信息到我的代码中,但是当我运行代码时,我收到了很多错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/retry.py", line 121, in inner
    return to_call(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/retry.py", line 68, in inner
    return a_func(*updated_args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 487, in __call__
    return _end_unary_response_blocking(state, call, False, deadline)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 437, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, sample_rate_hertz (16000) in RecognitionConfig must either be omitted or match the value in the WAV header ( 44100).)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "TESTINGAUDIO.py", line 27, in <module>
    print (transcribe_file("output.wav"))
  File "TESTINGAUDIO.py", line 20, in transcribe_file
    response = client.recognize(config, audio)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/gapic/speech/v1/speech_client.py", line 201, in recognize
    return self._recognize(request, options)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/api_callable.py", line 452, in inner
    return api_caller(api_call, this_settings, request)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/api_callable.py", line 438, in base_caller
    return api_call(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/api_callable.py", line 376, in inner
    return a_func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/retry.py", line 127, in inner
    ' classified as transient', exception)
google.gax.errors.RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, sample_rate_hertz (16000) in RecognitionConfig must either be omitted or match the value in the WAV header ( 44100).)>)

这是我的代码:

import os
import io
def transcribe_file(speech_file):
    """Transcribe the given audio file."""
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    client = speech.SpeechClient()

    with io.open(speech_file, 'rb') as audio_file:
        content = audio_file.read()

    audio = types.RecognitionAudio(content=content)
    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=16000,
        language_code='en-US')

    response = client.recognize(config, audio)
    # Each result is for a consecutive portion of the audio. Iterate through
    # them to get the transcripts for the entire audio file.
    for result in response.results:
        # The first alternative is the most likely one for this portion.
        print('Transcript: {}'.format(result.alternatives[0].transcript))

print (transcribe_file("output.wav"))

有人能帮我修复这个错误吗?

以下是采纳索拉克建议后的第二批错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/retry.py", line 121, in inner
    return to_call(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/retry.py", line 68, in inner
    return a_func(*updated_args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 487, in __call__
    return _end_unary_response_blocking(state, call, False, deadline)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 437, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Must use single channel (mono) audio, but WAV header indicates 2 channels.)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "TESTINGAUDIO.py", line 26, in <module>
    print (transcribe_file("output.wav"))
  File "TESTINGAUDIO.py", line 20, in transcribe_file
    response = client.recognize(config, audio)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/gapic/speech/v1/speech_client.py", line 201, in recognize
    return self._recognize(request, options)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/api_callable.py", line 452, in inner
    return api_caller(api_call, this_settings, request)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/api_callable.py", line 438, in base_caller
    return api_call(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/api_callable.py", line 376, in inner
    return a_func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/gax/retry.py", line 127, in inner
    ' classified as transient', exception)
google.gax.errors.RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.INVALID_ARGUMENT, Must use single channel (mono) audio, but WAV header indicates 2 channels.)>)

共有1个答案

景景胜
2023-03-14

错误消息似乎在抱怨:

sample_rate_hertz=16000

因为它指定的采样率与44100的标准wav采样率相冲突。它说这个值

must either be omitted or match the value in the WAV header

。。。那么如果你去掉那条线会怎么样?

 类似资料:
  • 由于连接到不同的API,我目前正在开发一个工具,允许我阅读所有的通知。 它工作得很好,但现在我想用一些声音命令来做一些动作。 就像当软件说“一封来自Bob的邮件”时,我想说“阅读”或“存档”。 我的软件是通过一个节点服务器运行的,目前我没有任何浏览器实现,但它可以是一个计划。 在NodeJS中,启用语音到文本的最佳方式是什么? 我在它上面看到了很多线程,但主要是使用浏览器,如果可能的话,我希望在一

  • 语音识别是以语音为研究对象,通过语音信号处理和模式识别让机器自动识别和理解人类口述的语言。语音识别技术就是让机器通过识别和理解过程把语音信号转变为相应的文本或命令的高技术。语音识别是一门涉及面很广的交叉学科,它与声学、语音学、语言学、信息理论、模式识别理论以及神经生物学等学科都有非常密切的关系。语音识别技术正逐步成为计算机信息处理技术中的关键技术,语音技术的应用已经成为一个具有竞争性的新兴高技术产

  • 识别简单的语句。

  • 光环板内置的麦克风和Wi-Fi功能相结合,可以实现语音识别相关的应用。通过接入互联网,可以使用各大主流科技公司提供的语音识别服务,像是微软语音识别服务。使用联网功能需要登陆慧编程账号。 注册/登陆慧编程 点击工具栏右侧的登陆/注册按钮,依据提示登陆/注册账号。 启用上传模式 点击启用上传模式。 新建语音识别项目 我们将新建一个语音识别项目,使用语音来点亮光环板的LED灯。 连接网络 1. 添加事件

  • 1.1. ASR(语音识别) HTTP接口文档 1.1.1. 概述 1.1.2. 服务地址 1.1.3. 协议详解 1.1.4. HTTP API 接入参考Demo 1.1.5. 协议概述 1.1. ASR(语音识别) HTTP接口文档 1.1.1. 概述 本文档目的是描述Rokid云ASR(语音识别)Http接口协议,面向想要了解ASR细节,并具有一定开发能力的开发者或用户。 1.1.2. 服务

  • 1.1. ASR(语音识别) WebSocket接口文档 1.1.1. 概述 1.1.2. 服务地址 1.1.3. 协议详解 1.1.4. 协议地址 1.1.5. 协议概述 1.1.6. ASR 云端一些细节 1.1. ASR(语音识别) WebSocket接口文档 1.1.1. 概述 本文档目的是描述Rokid云ASR(语音识别)WebSocket接口协议,面向想要了解ASR细节,并具有一定开发