【问题标题】:how to read mp3 data from google cloud using python如何使用python从谷歌云读取mp3数据
【发布时间】:2020-02-12 11:40:46
【问题描述】:

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

下面是我的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:“操作”对象没有属性“结果”

您能否就我做错的事情分享您的专家建议? 感谢您的帮助。

【问题讨论】:

    标签: python python-2.7 google-cloud-speech


    【解决方案1】:

    对于这个帖子的作者来说太晚了。但是,将来为某人发布解决方案,因为我也有类似的问题。 改变 结果 = response.results[-1] 至 结果 = response.result().results[-1] 它会正常工作

    【讨论】:

      【解决方案2】:

      您可以访问存储桶中的 wav 文件吗?另外,这是整个代码吗?似乎缺少 sample_rate_hertz 和导入。在这里,您有从 google docs 示例中复制/粘贴的代码,但我对其进行了编辑以仅具有 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-cloud-speech 库:

      pip install --upgrade google-cloud-speech
      

      并且您需要将服务帐户的凭据保存在 json 文件中,您可以查看更多信息here

      【讨论】:

      • 嗨,亚历克斯,给定的代码不起作用,我尝试了同样的方法,它在结果 = response.results[-1] 行中失败,错误为 AttributeError: 'Operation' object has no attribute 'results' .
      • 你好@PP 你有最新版本的 google-cloud-speech 库吗?运行pip install --upgrade google-cloud-speech,还有,你设置了 GOOGLE_APPLICATION_CREDENTIALS json 吗?在这里查看更多信息:cloud.google.com/speech-to-text/docs/…。因为该代码对我来说非常有效,只需编辑变量
      • @AlexRiquelme,当我执行上述代码时,我得到的只是一行:“等待操作完成... word:'bye', speaker_tag:0”有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多