[英]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. 由于此文件与代码中的预期文件不同,因此应将
RecognitionConfig
的encoding
和sample_rate_hertz
属性分别更改为'FLAC'
和16000
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.