简体   繁体   English

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

[英]How to fix “request contains an invalid argument error” when transcribing audio file with Google Cloud Speech

We are able to create unique buckets on each run of the program, however, it hits a road-block around the time it reaches the transcribe_gcs function. 我们能够在程序的每次运行中创建唯一的存储桶,但是,在到达transcribe_gcs函数时,它遇到了障碍。 We want the program to transcribe the audio file that gets uploaded to the bucket. 我们希望程序转录上传到存储桶的音频文件。 But the transcribe process isn't quite functioning. 但是转录过程运行不正常。

We changed the directory for the gcs_uri to be "gs://". 我们将gcs_uri的目录更改为“ gs://”。 This allows unique buckets to be created each time. 这样可以每次创建唯一的存储桶。

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)

Expected results: transcribes the audio file uploaded to the unique bucket 预期结果:转录上传到唯一存储桶的音频文件

Actual results: creates a bucket, but does not go further than that. 实际结果:创建一个存储桶,但没有比这更进一步。

ERROR: 错误:

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

Have you tried with Google Cloud's uri sample yet? 您是否尝试过Google Cloud的uri示例?

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

After making some changes to your code as @siamsot suggested in his comment I could reproduce the error that you got. 在对@siamsot在他的注释中建议的代码进行了一些更改之后,我可以重现您收到的错误。 It occurs only when you don't pass a valid gcs_uri . 仅当您未传递有效的gcs_uri时,它才会发生。

It should be of type string and with the format: 它应为string类型,格式为:

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

like the google sample that @Huy Nguyen posted in their answer: 就像@Huy Nguyen在其答案中张贴的google示例:

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

I suspect that you didn't specify the filename or the prefix gs:// in the gcs_uri . 我怀疑您没有在gcs_uri指定文件名或前缀gs:// I managed to transcribe the above sample file with your code. 我设法用您的代码抄录了上面的示例文件。 If you want to test it, change your imports to: 如果要测试,请将导入更改为:

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

and pass 'gs://gcs-test-data/vr.flac' as the gcs_uri to the transcribe_gcs function. 并将'gs://gcs-test-data/vr.flac'作为gcs_uri传递给transcribe_gcs函数。

As this file is different from what you are expecting in your code you should change the encoding and sample_rate_hertz properties of RecognitionConfig to 'FLAC' and 16000 respectively. 由于此文件与代码中的预期文件不同,因此应将RecognitionConfigencodingsample_rate_hertz属性分别更改为'FLAC'16000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于在音频文件中转录语音的开源软件 - Open Source Software For Transcribing Speech in Audio Files 语音请求错误中的Google的Cloud Speech API异常 - Google's Cloud speech API Exception in speech request error 与 Twilio“流”动词和 Websocket 一起使用时,Google Cloud Speech to Text 音频超时错误 - Google Cloud Speech to Text Audio Timeout Error when used with Twilio “Stream” verb and Websocket Python:如何使用 pyaudio 为 Google Cloud Speech API 获取原始音频文件 - Python: How to get raw audio file using pyaudio for Google Cloud Speech API Google App Script API 无法验证“请求包含无效参数” - Google App Script API cannot authenticate "Request contains an invalid argument" 谷歌云文本到语音音频到浏览器 - Google Cloud Text-to-Speech Audio to Browser 如何修复Django中的OSError(无效参数)错误 - How to fix OSError (invalid argument) error in django 当&#39;pip install google-cloud-speech&#39;发生错误 - error occurred when 'pip install google-cloud-speech' 从 google.cloud.speech 导入 SpeechClient 时出现属性错误 - Attribute Error when importing SpeechClient from google.cloud.speech 转录长音频文件不起作用 - Transcribing Long Audio File doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM