gpt4 book ai didi

python-3.x - Google Cloud Speech to Text 中的 enable_speaker_diarization 标记出错

转载 作者:行者123 更新时间:2023-12-05 00:39:54 24 4
gpt4 key购买 nike

使用 Google-Speech-to-Text,我可以使用默认参数转录音频剪辑。但是,我在使用 enable_speaker_diarization 标签分析音频剪辑中的各个扬声器时收到一条错误消息。谷歌记录它here这是一个很长的识别音频剪辑,因此我使用 Google 推荐的异步请求 here

我的代码 -

def transcribe_gcs(gcs_uri):
from google.cloud import speech
from google.cloud import speech_v1 as speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()
audio = types.RecognitionAudio(uri = gcs_uri)
config = speech.types.RecognitionConfig(encoding=speech.enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz= 16000,
language_code = 'en-US',
enable_speaker_diarization=True,
diarization_speaker_count=2)

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

for word_info in words_info:
print("word: '{}', speaker_tag: {}".format(word_info.word, word_info.speaker_tag))

使用后-

transcribe_gcs('gs://bucket_name/filename.flac') 

我得到了错误

ValueError: Protocol message RecognitionConfig has no "enable_speaker_diarization" field.

我确定这与库有关,我已经使用了我能找到的所有变体

from google.cloud import speech_v1p1beta1 as speech
from google.cloud import speech

但我不断收到同样的错误。注意 - 在运行此代码之前,我已经使用 JSON 文件进行了身份验证。

最佳答案

speech.types.RecognitionConfig 中的 enable_speaker_diarization=True 参数目前仅在库 speech_v1p1beta1 中可用,因此,您需要导入该库以使用该参数,而不是默认语音。我对您的代码做了一些修改,对我来说效果很好。考虑到您需要使用服务帐户来运行此代码。

def transcribe_gcs(gcs_uri):
from google.cloud import speech_v1p1beta1 as speech
from google.cloud.speech_v1p1beta1 import enums
from google.cloud.speech_v1p1beta1 import types
client = speech.SpeechClient()
audio = types.RecognitionAudio(uri = gcs_uri)
config = speech.types.RecognitionConfig( language_code = 'en-US',enable_speaker_diarization=True, diarization_speaker_count=2)
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

else:
print("speaker {}: {}".format(tag,speaker))
tag=word_info.speaker_tag
speaker=""+word_info.word

print("speaker {}: {}".format(tag,speaker))

结果应该是这样的:

enter image description here

关于python-3.x - Google Cloud Speech to Text 中的 enable_speaker_diarization 标记出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54271749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com