ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-通过websockets流音频IBM无法正常工作

2019-10-26 07:58:07  阅读:280  来源: 互联网

标签:speech-to-text ibm-watson websocket speech-recognition python


我正在尝试使用IBM Watson通过websockets通过麦克风流式传输音频.我收到以下错误:

TypeError: The system cannot find the file specified

我认为子流程有问题,请帮助我纠正此问题/使其正常工作.

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

解决方法:

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

标签:speech-to-text,ibm-watson,websocket,speech-recognition,python
来源: https://codeday.me/bug/20191026/1935086.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有