python-通过websockets流音频IBM无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-通过websockets流音频IBM无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2737字,纯文字阅读大概需要4分钟。
内容图文
![python-通过websockets流音频IBM无法正常工作](/upload/InfoBanner/zyjiaocheng/684/007e71e4c6aa46fdbf443b6ad02b6d2d.jpg)
我正在尝试使用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
内容总结
以上是互联网集市为您收集整理的python-通过websockets流音频IBM无法正常工作全部内容,希望文章能够帮你解决python-通过websockets流音频IBM无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。