我正在使用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接收信息。我究竟做错了什么?
最佳答案
我在我们的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
关于python - IBM Watson语音到文本Python,“DetailedResponse”对象没有属性“getResult”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52318423/