我正在尝试从谷歌云读取mp3/wav数据,并尝试实现音频数字化技术。问题是我无法读取google在变量响应中传递的结果。
下面是我的python代码
speech_file = r'gs://pp003231/a4a.wav'
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
language_code='en-US',
enable_speaker_diarization=True,
diarization_speaker_count=2)
audio = speech.types.RecognitionAudio(uri=speech_file)
response = client.long_running_recognize(config, audio)
print response
result = response.results[-1]
print result控制台上显示的输出是回溯(最近一次调用):文件"a1.py",第131行,在打印response.results AttributeError:‘操作’对象没有属性‘结果’
关于我做错了什么,你能分享你的专家意见吗?谢谢你的帮助。
发布于 2020-02-12 03:40:46
对于这篇文章的作者来说太晚了。然而,张贴的解决方案在未来的人,因为我也有类似的问题。将结果=response.Resuls-1更改为response.result().结果-1,它将正常工作
发布于 2018-11-27 10:36:08
你能访问你桶里的wav文件吗?还有,这就是整个代码?看来sample_rate_hertz和进口品不见了。这里有来自google示例的代码复制/粘贴,但我对其进行了编辑,使其只具有diarization功能。
#!/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://<YOUR_BUCKET/<YOUR_WAV_FILE>")
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()要运行代码,只需将其命名为diarization.py并使用以下命令:
python diarization.py此外,您还必须安装最新的google云语音库:
pip install --upgrade google-cloud-speech如果您需要将服务帐户的凭据保存在json文件中,则可以查看更多信息( 这里 )。
https://stackoverflow.com/questions/53490557
复制相似问题