通过Websockets传输音频流IBM无法工作

3
我正在尝试使用IBM Watson通过Websockets流传麦克风的音频。我遇到了以下错误:

TypeError:系统找不到指定的文件

我认为是subprocess出了问题,请帮助我修正此问题/使其正常工作。
from ws4py.client.threadedclient import WebSocketClient
import base64, json, ssl, subprocess, threading, time

class SpeechToTextClient(WebSocketClient):
    def __init__(self):
        ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"

        username = "81bffb34-906b-4057-becf-72752e14e756"
        password= "jPwZAMMB5Fwp"
        auth_string = "%s:%s" % (username, password)
        base64string= base64.encodestring(auth_string).replace("\n", "")

        self.listening = False

        try:
            WebSocketClient.__init__(self, ws_url, headers=[("Authorization", "Basic %s" % base64string)])
            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_connection(self):
        self.listening = False
        self.stream_audio_thread.join()
        WebSocketClient.close(self)





if __name__ == "__main__":

    SpeechToTextClient()

以下是完整的错误输出:
C:\Python27\python.exe C:/Users/vetle/Desktop/testing_ibm.py

Message received: {u'state': u'listening'}
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
   self.run()
File "C:\Python27\lib\threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "C:/Users/vetle/Desktop/testing_ibm.py", line 38, in stream_audio
p = subprocess.Popen(reccmd, stdout=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
1个回答

4

Arecord是一个在Alsa框架下用于录制音频的Linux工具,它无法在Linux上工作。

您需要使用类似pyaudio的工具来进行录音。

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

       p = pyaudio.PyAudio()

       stream = p.open(format=pyaudio.paInt16,
                channels=1, 
                rate=16000, 
                input=True,
                frames_per_buffer=1024)

        while self.listening:
            data = stream.read(2048)

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

谢谢,这解决了整个问题!你太棒了。 - user7519553

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接