使用Google Cloud Speech转录音频文件时如何解决“请求包含无效的参数错误”

时间:2019-07-28 05:35:44

标签: python google-cloud-storage google-speech-api

我们能够在程序的每次运行中创建唯一的存储桶,但是,它在到达transcribe_gcs函数时遇到了障碍。我们希望程序转录上传到存储桶的音频文件。但是转录过程运行不正常。

我们将gcs_uri的目录更改为“ gs://”。这样可以每次创建唯一的存储桶。

def transcribe_gcs(gcs_uri):
    """Asynchronously transcribes the audio file specified by the gcs_uri."""
    #from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    from google.cloud.speech_v1p1beta1 import enums
    from google.cloud.speech_v1p1beta1 import types
    audio = types.RecognitionAudio(uri=gcs_uri)
    config = types.RecognitionConfig(
        encoding='LINEAR16',
        sample_rate_hertz=44100,
        language_code='en-US',
        enable_speaker_diarization=True,
        diarization_speaker_count=2)

    client = speech.SpeechClient()

    ##response = client.recognize(config, audio)

    operation = client.long_running_recognize(config, audio)
    print('Waiting for operation to complete...')
    response = operation.result(timeout=3000)
    result = response.results[-1]

    words_info = result.alternatives[0].words

    tag = 1
    speaker = ""

    for word_info in words_info:
        if word_info.speaker_tag == tag:
            speaker = speaker + " " + word_info.word     #need to adjust how speakers are actually separated

        else:
            print("Speaker {}: {}".format(tag, speaker)) #get program to print entire transcript through here
            tag = word_info.speaker_tag
            speaker = "" + word_info.word                #make sentiment analysis work on each individual line


    # 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(u'Transcript: {}'.format(result.alternatives[0].transcript)) #this should be removed eventually but should be used somehow to modify the speaker portion

        transcribedSpeechFile = open('speechToAnalyze.txt', 'a+')  # this is where a text file is made with the transcribed speech

        transcribedSpeechFile.write(format(result.alternatives[0].transcript))

        transcribedSpeechFile.close()

        confidencePercentage = result.alternatives[0].confidence
        confidencePercentage = confidencePercentage * 100

        print("Confidence level of transcription: {}%".format(round(confidencePercentage, 2)))
# [END speech_transcribe_async_gcs]


if __name__ == '__main__':
    transcribe_gcs(gcs_uri)

预期结果:转录上传到唯一存储段的音频文件

实际结果:创建了一个存储桶,但没有比这更进一步了。

错误:

Traceback (most recent call last):
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\google\api_core\grpc_helpers.py", line 57, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\grpc\_channel.py", line 565, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\grpc\_channel.py", line 467, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
    status = StatusCode.INVALID_ARGUMENT
    details = "Request contains an invalid argument."
    debug_error_string = "{"created":"@1564207941.288000000","description":"Error received from peer ipv6:[2607:f8b0:4000:80e::200a]:443","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"Request contains an invalid argument.","grpc_status":3}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/Dave/Desktop/mizu/test.py", line 120, in <module>
    transcribe_gcs(gcs_uri)
  File "C:/Users/Dave/Desktop/mizu/test.py", line 80, in transcribe_gcs
    operation = client.long_running_recognize(config, audio)
  File "C:\Users\Dave\AppData\Local\Programs\Python\Python37\lib\site-packages\google\cloud\speech_v1p1beta1\gapic\speech_client.py", line 326, in long_running_recognize
    request, retry=retry, timeout=timeout, metadata=metadata
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\google\api_core\gapic_v1\method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\google\api_core\retry.py", line 273, in retry_wrapped_func
    on_error=on_error,
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\google\api_core\retry.py", line 182, in retry_target
    return target()
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\google\api_core\timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "C:\Users\Dave\AppData\Roaming\Python\Python37\site-packages\google\api_core\grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument

2 个答案:

答案 0 :(得分:1)

根据@siamsot在他的注释中建议对代码进行了一些更改之后,我可以重现您遇到的错误。仅当您未通过有效的gcs_uri时,它才会发生。

它应该是string类型,其格式为:

  

gs:// [BUCKET_NAME] / [PATH_TO_FILE] / [FILENAME]

就像@Huy Nguyen在其答案中张贴的google示例:

  

gs://gcs-test-data/vr.flac

我怀疑您没有在gs://中指定文件名或前缀gcs_uri。 我设法用您的代码抄录了上面的示例文件。 如果要测试,请将导入更改为:

from google.cloud import speechv1p1beta1 as speech
#from google.cloud.speech import enums
#from google.cloud.speech import types
#from google.cloud.speech_v1p1beta1 import enums
from google.cloud.speech_v1p1beta1 import types

,并将'gs://gcs-test-data/vr.flac'作为gcs_uri传递给transcribe_gcs函数。

由于此文件与代码中的预期不同,因此应将encoding的{​​{1}}和sample_rate_hertz属性更改为RecognitionConfig'FLAC'分别。

答案 1 :(得分:0)

您是否尝试过Google Cloud的uri示例?

  

gs://gcs-test-data/vr.flac