【发布时间】:2018-04-08 13:43:42
【问题描述】:
我正在尝试创建一个小型 Python 程序,它可以让我使用来自 Watson 服务器的麦克风实时获取文本,类似于 here 的工作方式。
这是我想出的代码,但在我完成录制后它会得到文本:
import pyaudio
import json
from watson_developer_cloud import SpeechToTextV1
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 10
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
data_feed = b''.join(frames)
speech_to_text = SpeechToTextV1(
username='secret',
password='secret too',
x_watson_learning_opt_out=False
)
result = speech_to_text.recognize(data_feed,
content_type="audio/l16;rate=44100;channels=2",
word_confidence=True,
max_alternatives=4,
word_alternatives_threshold=0.5,
model="en-US_BroadbandModel",
continuous=True)
j = json.dumps(result, indent=2)
print(j)
【问题讨论】:
-
您是否正在尝试解决某个特定问题?您是否尝试过逐行浏览代码?
-
嗨@alex,目前python的SDK仅限于使用音频文件,而不是通过麦克风获取直接音频。我目前正在开展一个项目,该项目可以在使用麦克风时获取实时文本。
-
我现在正在使用 websockets 来做这件事,我想我明天可能会有一些可靠的东西。
标签: python machine-learning websocket speech-to-text ibm-watson