【问题标题】:Not getting the expected result with IBM Watson Speech To Text使用 IBM Watson Speech To Text 未获得预期结果
【发布时间】:2020-08-24 09:09:04
【问题描述】:

尝试在标准 IBM Watson S2T 模型上测试 mp3 文件时,我得到以下输出:

<bound method DetailedResponse.get_result of <ibm_cloud_sdk_core.detailed_response.DetailedResponse object at 0x00000250B1853700>>

这不是错误,但也不是我想要的输出。

这是我的代码:

api = IAMAuthenticator(api_key)
speech_to_text = SpeechToTextV1(authenticator=api)

speech_to_text.set_service_url(url)

with open(mp3-file, "rb") as audio_file:
    result = speech_to_text.recognize(
        model='de-DE_BroadbandModel', audio=audio_file, content_type="audio/mp3"
    ).get_result

print(result)

我对这个话题很陌生,还没有真正弄清楚参数是什么。我希望有这样的输出

{'result': [...]}

我关注了this tutorial。 我做错了什么?

【问题讨论】:

    标签: python ibm-watson speech-to-text


    【解决方案1】:

    get_result 是一个方法,所以你需要调用它。您正在打印该方法,而不是调用它。因此你的输出显示

    <bound method DetailedResponse.get_result ...
    

    几个括号应该可以解决它。

    with open(mp3-file, "rb") as audio_file:
        result = speech_to_text.recognize(
            model='de-DE_BroadbandModel', audio=audio_file, content_type="audio/mp3"
        ).get_result()
    

    【讨论】:

      【解决方案2】:

      这是使用示例 audio_file2.mp3 为我工作的代码

      import json
      from os.path import join, dirname
      from ibm_watson import SpeechToTextV1
      from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
      
      authenticator = IAMAuthenticator('{api_key}')
      speech_to_text = SpeechToTextV1(
          authenticator=authenticator
      )
      
      speech_to_text.set_service_url('{url}')
      
      with open(join(dirname(__file__), './.', 'audio-file2.mp3'),
                     'rb') as audio_file:
          speech_recognition_results = speech_to_text.recognize(
              audio=audio_file,
              content_type='audio/mp3',
              word_alternatives_threshold=0.9
          ).get_result()
      print(json.dumps(speech_recognition_results, indent=2))
      

      步骤:

      1. 一旦您创建了 Watson Speech-to-Text service
      2. 将 Python 代码中的 {url}{api_key} 替换为语音转文本服务凭据。
      3. 将文件保存为speech-to-text.py
      4. 在命令提示符或终端中,运行 pip install ibm-watson,然后运行 ​​python speech-to-text.py 以查看类似于下图的结果

      请参阅speech-to-text api docs 了解更多选项。

      {
        "result_index": 0,
        "results": [
          {
            "final": true,
            "alternatives": [
              {
                "transcript": "a line of severe thunderstorms with several possible tornadoes is approaching Colorado on Sunday ",
                "confidence": 1.0
              }
            ],
            "word_alternatives": [
              {
                "start_time": 0.2,
                "end_time": 0.35,
                "alternatives": [
                  {
                    "word": "a",
                    "confidence": 0.94
                  }
                ]
              },
              {
                "start_time": 0.35,
                "end_time": 0.69,
                "alternatives": [
                  {
                    "word": "line",
                    "confidence": 0.94
                  }
                ]
              },
              {
                "start_time": 0.69,
                "end_time": 0.78,
                "alternatives": [
                  {
                    "word": "of",
                    "confidence": 1.0
                  }
                ]
              },
              {
                "start_time": 0.78,
                "end_time": 1.13,
                "alternatives": [
                  {
                    "word": "severe",
                    "confidence": 1.0
                  }
                ]
              },
              {
                "start_time": 1.13,
                "end_time": 1.9,
                "alternatives": [
                  {
                    "word": "thunderstorms",
                    "confidence": 1.0
                  }
                ]
              },
              {
                "start_time": 4.0,
                "end_time": 4.18,
                "alternatives": [
                  {
                    "word": "is",
                    "confidence": 1.0
                  }
                ]
              },
              {
                "start_time": 4.18,
                "end_time": 4.63,
                "alternatives": [
                  {
                    "word": "approaching",
                    "confidence": 1.0
                  }
                ]
              },
              {
                "start_time": 4.63,
                "end_time": 5.21,
                "alternatives": [
                  {
                    "word": "Colorado",
                    "confidence": 0.93
                  }
                ]
              },
              {
                "start_time": 5.21,
                "end_time": 5.37,
                "alternatives": [
                  {
                    "word": "on",
                    "confidence": 0.93
                  }
                ]
              },
              {
                "start_time": 5.37,
                "end_time": 6.09,
                "alternatives": [
                  {
                    "word": "Sunday",
                    "confidence": 0.94
                  }
                ]
              }
            ]
          }
        ]
      }
      

      【讨论】:

      • 使用这个解决方案我得到了错误raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type method is not JSON serializable
      • 您使用的是哪个版本的 Python?
      • Python 3.8 版,在 windows 上运行
      • 您可以运行print(speech_recognition_results) 并在此处粘贴响应吗?
      • &lt;bound method DetailedResponse.get_result of &lt;ibm_cloud_sdk_core.detailed_response.DetailedResponse object at 0x000001E27CBC3700&gt;&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多