为什么google speech_v1p1beta1输出仅显示最后一个单词?

时间:2019-02-15 05:56:59

标签: python speech-recognition speech-to-text google-speech-api

我正在使用下面的代码来转录音频文件。该过程完成后,我只能说最后一句话。

我尝试了flac和wav文件,并确保文件在我的存储桶中。 另外,已验证的服务帐户是Google正常运行。但是无法弄清楚为什么我只能说最后一句话。

#!/usr/bin/env python
"""Google Cloud Speech API sample that demonstrates enhanced models
and recognition metadata.
Example usage:
    python diarization.py
"""

import argparse
import io



def transcribe_file_with_diarization():
    """Transcribe the given audio file synchronously with diarization."""
    # [START speech_transcribe_diarization_beta]
    from google.cloud import speech_v1p1beta1 as speech
    client = speech.SpeechClient()


audio = speech.types.RecognitionAudio(uri="gs://MYBUCKET/MYAudiofile")

config = speech.types.RecognitionConfig(
    encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=8000,
    language_code='en-US',
    enable_speaker_diarization=True,
    diarization_speaker_count=2)

print('Waiting for operation to complete...')
response = client.recognize(config, audio)

# The transcript within each result is separate and sequential per result.
# However, the words list within an alternative includes all the words
# from all the results thus far. Thus, to get all the words with speaker
# tags, you only have to take the words list from the last result:
result = response.results[-1]

words_info = result.alternatives[0].words

# Printing out the output:
for word_info in words_info:
    print("word: '{}', speaker_tag: {}".format(word_info.word,
                                               word_info.speaker_tag))
# [END speech_transcribe_diarization_beta]



if __name__ == '__main__':

    transcribe_file_with_diarization()
运行代码后,

结果显示在这里:

python diarazation.py

Waiting for operation to complete...
word: 'bye', speaker_tag: 0

0 个答案:

没有答案