为什么谷歌语音输出只显示最后一个单词?

2024-04-20 13:49:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码来转录音频文件。当这个过程完成时,我只知道最后一句话。你知道吗

我已经尝试了flac和wav文件,并确保文件在我的桶。 同时验证了谷歌的服务帐户是否正常工作。但我不明白为什么我只能说最后一句话。你知道吗

#!/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重氮化.py你知道吗

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

Tags: thetofromimportinfowithresultaudio