首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python从google云读取mp3数据

如何使用python从google云读取mp3数据
EN

Stack Overflow用户
提问于 2018-11-26 23:19:25
回答 2查看 1K关注 0票数 0

我正在尝试从谷歌云读取mp3/wav数据,并尝试实现音频数字化技术。问题是我无法读取google在变量响应中传递的结果。

下面是我的python代码

代码语言:javascript
复制
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:‘操作’对象没有属性‘结果’

关于我做错了什么,你能分享你的专家意见吗?谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-12 03:40:46

对于这篇文章的作者来说太晚了。然而,张贴的解决方案在未来的人,因为我也有类似的问题。将结果=response.Resuls-1更改为response.result().结果-1,它将正常工作

票数 1
EN

Stack Overflow用户

发布于 2018-11-27 10:36:08

你能访问你桶里的wav文件吗?还有,这就是整个代码?看来sample_rate_hertz和进口品不见了。这里有来自google示例的代码复制/粘贴,但我对其进行了编辑,使其只具有diarization功能。

代码语言:javascript
复制
#!/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并使用以下命令:

代码语言:javascript
复制
python diarization.py

此外,您还必须安装最新的google云语音库:

代码语言:javascript
复制
pip install --upgrade google-cloud-speech

如果您需要将服务帐户的凭据保存在json文件中,则可以查看更多信息( 这里 )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53490557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档