Python - IBM Watson Speech to Text 'NoneType' 对象没有属性 'get_result'

Python - IBM Watson Speech to Text 'NoneType' object has no attribute 'get_result'

我正在使用 IBM Watson Speech to Text 开发一个程序,目前使用的是 Python 2.7。这是一些开发代码的存根:

class MyRecognizeCallback(RecognizeCallback):
    def __init__(self):
        RecognizeCallback.__init__(self)

    def on_data(self, data):
        pass

    def on_error(self, error):
        pass

    def on_inactivity_timeout(self, error):
        pass

speech_to_text = SpeechToTextV1(username='*goes here*', password='*goes here*')
speech_to_text.set_detailed_response(True)

f = '/home/user/file.wav'

rate, data = wavfile.read(f)
work = data.tolist()

with open(f, 'rb') as audio_file:

                # Get IBM Watson analytics
                currentModel = "en-US_NarrowbandModel" if rate <= 8000 else "en-US_BroadbandModel"

                x = ""

                print(" - " + f)

                try:

                    # Callback info
                    myRecognizeCallback = MyRecognizeCallback()

                    # X represents the responce from Watson
                    audio_source = AudioSource(audio_file)
                    my_result = speech_to_text.recognize_using_websocket(
                                    audio_source,
                                    content_type='audio/wav',
                                    timestamps=True,
                                    recognize_callback=myRecognizeCallback,
                                    model=currentModel,
                                    inactivity_timeout=-1,
                                    max_alternatives=0)
                    x = json.loads(json.dumps(my_result, indent=2), object_hook=lambda d: n
                    namedtuple('X', d.keys())(*d.values()))

我期望返回的是一个 JSON 对象,其中包含给定上述参数的文件的结果。相反,我收到的是一个如下所示的错误:

Error received: 'NoneType' object has no attribute 'connected'

这就是整个回溯——除此之外没有其他错误。但是,当我尝试在进一步的代码中访问 JSON 对象时,出现此错误:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/watson_developer_cloud/websocket/recognize_listener.py", line 96, in run
    chunk = self.audio_source.input.read(ONE_KB)
ValueError: I/O operation on closed file

我是不是忘记了什么或者放错地方了?

编辑:

我的原始代码中有一个错误,我已自行修复。无论如何,我仍然收到原始错误。这是更新:

my_result = speech_to_text.recognize_using_websocket(
                                    audio_source,
                                    content_type='audio/wav',
                                    timestamps=True,
                                    recognize_callback=myRecognizeCallback,
                                    model=currentModel,
                                    inactivity_timeout=None,
                                    max_alternatives=None).get_result()
                    x = json.loads(json.dumps(my_result, indent=2), object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))

看看object_hook=lambda d: n,在python中lambda d: n表示"a function that takes d, ignores d, and returns n"。

我猜 n 在其他地方设置为 None。

如果这不起作用,将 lambda 分解成一个单独的函数可能更容易调试,def to_named_tuple(object): 也许。