Search code examples
pythonwebsocketibm-cloudibm-watson

Failed to open WebSocket and AttributeError: 'SpeechToTextClient' error (Watson on Python)


I am trying to get a web-socket connection open for a Speech to text recognition on Watson using Python v2.7

My code uses an API key plus a url. I have tried several ways to open a socket using this but I get a web socket error with a stream error as follows:

Failed to open WebSocket.
Failed to open WebSocket.
Traceback (most recent call last): File "jasmineV3.py", line 78, in <module>
    SpeechToTextClient().close() File "jasmineV3.py", line 68, in close
    self.stream_audio_thread.join()
AttributeError: 'SpeechToTextClient' object has no attribute 'stream_audio_thread'

Here is my code that I am attempting to make work:

#import os
#import time
#import json
import watson_developer_cloud
import speech_recognition as sr
from gtts import gTTS
from time import ctime
#from __future__ import print_function

from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud.websocket import RecognizeCallback
from ws4py.client.threadedclient import WebSocketClient
import base64, json, ssl, subprocess, threading, time

class SpeechToTextClient(WebSocketClient):
    def __init__(self):

        speech_to_text = SpeechToTextV1(iam_api_key = 'xxxxxxxx', url = 'xxxxxxx')

        self.listening = False

        try:
            WebSocketClient.__init__(self, speech_to_text)
            self.connect()
        except: print "Failed to open WebSocket."

    def opened(self):
        self.send('{"action": "start", "content-type": "audio/l16;rate=16000"}')
        self.stream_audio_thread = threading.Thread(target=self.stream_audio)
        self.stream_audio_thread.start()

    def received_message(self, message):
        message = json.loads(str(message))
        if "state" in message:
            if message["state"] == "listening":
                self.listening = True
        print "Message received: " + str(message)

    def stream_audio(self):
        while not self.listening:
            time.sleep(0.1)

        reccmd = ["arecord", "-f", "S16_LE", "-r", "16000", "-t", "raw"]
        p = subprocess.Popen(reccmd, stdout=subprocess.PIPE)

        while self.listening:
            data = p.stdout.read(1024)

            try: self.send(bytearray(data), binary=True)
            except ssl.SSLError: pass

        p.kill()

    def close(self):
        self.listening = False
        self.stream_audio_thread.join()
        WebSocketClient.close(self)

try:
    stt_client = SpeechToTextClient()
    #raw_input()
    speech_to_text = SpeechToTextV1(
        iam_api_key = 'xxxxxxxxx',
        url = 'xxxxxxxx')
finally:
    SpeechToTextClient().close()

I am a little lost as to what my mistake is. How can I resolve it?

Update

So after taking feedback in the answers that were posted below, I came up with the following code:

from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud.websocket import RecognizeCallback
from os.path import join, dirname

import watson_developer_cloud
import speech_recognition as sr
from gtts import gTTS
from time import ctime
from os.path import join, dirname
from watson_developer_cloud import SpeechToTextV1
from ws4py.client.threadedclient import WebSocketClient
import base64, json, ssl, subprocess, threading, time
import os
import json

speech_to_text = SpeechToTextV1(
    username='{username}',
    password='{password}',
    iam_api_key = 'B5AmAyElAbvr6Z6dvW-CufLPwYsmKndNtAiGp4btg6s3',
    url = 'https://gateway-wdc.watsonplatform.net/speech-to-text/api/v1/recognize')

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

    def on_data(self, data):
        print(json.dumps(data, indent=2))

    def on_error(self, error):
        print('Error received: {}'.format(error))

    def on_inactivity_timeout(self, error):
        print('Inactivity timeout: {}'.format(error))

myRecognizeCallback = MyRecognizeCallback()

with open(join(dirname(__file__), '/home/ironmantis7x/Documents/MaverickAITech/JasmineAI', 'audio.mp3'),
              'rb') as audio_file:
    speech_to_text.recognize_using_websocket(
        audio=audio_file,
        content_type='audio/mp3',
        model='en-US_BroadbandModel',
        recognize_callback=myRecognizeCallback,
        interim_results=False,
        keywords=['hello', 'hi', 'turn on', 'directions'],
        keywords_threshold=0.5,
        max_alternatives=3) 

But now I get the following error:

Traceback (most recent call last):
  File "jasmineV7.py", line 37, in <module>
    speech_to_text.recognize_using_websocket(
AttributeError: 'SpeechToTextV1' object has no attribute 'recognize_using_websocket'

I tried a web search on the error and it's not straightforward what the issue is, nor how to properly fix it.


Solution

  • I ended up redoing the code that employs a different approach to achieve what I need.

    That solution is here: Trouble passing string variable to return data from python function to be used globally anywhere in a python script or program - EDITED for clarity

    Thanks.