【问题标题】:IBM Watson Speech-to-Text Python, 'DetailedResponse' object has no attribute 'getResult'IBM Watson Speech-to-Text Python,“DetailedResponse”对象没有属性“getResult”
【发布时间】:2018-09-13 16:56:12
【问题描述】:

我正在使用 Python 设计一个 Flask 项目,该项目利用了 IBM Watson 的 Speech-to-Text 功能。我现在要做的就是加载一个 FLAC 文件 (0001.flac),通过 Watson 解释该文件,然后将结果打印到我的控制台。到目前为止,我已经编写了以下代码(我替换了示例中的用户名和密码):

from werkzeug import secure_filename
import pprint, json, os
from watson_developer_cloud import SpeechToTextV1

. . .

speech_to_text = SpeechToTextV1(
username='My username is here',
password='My password is here')

with open(os.path.join(os.path.dirname(__file__), '0001.flac'), 'rb') as audio_file:
    speech_to_text.set_detailed_response(True)
    outthis = speech_to_text.recognize(
          audio_file, content_type='audio/flac', timestamps=True)
    pprint.pprint(json.dumps(outthis.getResult(), indent=2))

这是我的输出:

[2018-09-13 11:46:36,553] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\ehill\source\repos\FlaskWebProject1\FlaskWebProject1\FlaskWebProject1\views.py", line 31, in home
    pprint.pprint(json.dumps(outthis.getResult(), indent=2))
AttributeError: 'DetailedResponse' object has no attribute 'getResult'

根据 Watson 文档 (https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/python.html?python#introduction),我应该能够通过详细响应对象上的 getResult 接收信息。我做错了什么?

【问题讨论】:

    标签: python python-3.x ibm-watson


    【解决方案1】:

    我在 CI 环境中看到了同样的情况,它从干净的环境中运行“pip install”。它看起来像是 watson_developer_cloud v2.0.0 (https://pypi.org/project/watson-developer-cloud/2.0.0/#changes-for-v2.0) 引入的重大更改。

    我暂时通过强制使用 1.7.1 版本来解决问题,直到我可以更深入地查看代码更改。看起来这可能是一个很小的变化(从 response.get 到 response.get_result,但我不能确定)。

    仅供参考 - 以下是 2.0 版本中的重大更改列表:https://github.com/watson-developer-cloud/python-sdk/wiki/Migration

    【讨论】:

      【解决方案2】:

      为了遵守PEP8 conventions for function and variable names,在v2.0中getResult方法被重命名为get_result

      【讨论】:

        猜你喜欢
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多