IBM Watson语音到文本Python,“ DetailedResponse”对象没有属性“ getResult”

时间:2018-09-13 16:56:12

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

我正在使用Python在Flask中设计一个项目,该项目利用了IBM Watson的语音转文本功能。我现在要做的只是加载一个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),我应该能够通过DetailsResponse对象上的getResult接收信息。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

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