我想使用 pyaudio 和 IBM Bluemix 服务实现简单的语音转文本工具。目前我需要录制音频,将其保存到磁盘,然后再次加载以将其发送到 Bluemix。
RATE=44100
RECORD_SECONDS = 10
CHUNKSIZE = 1024
# initialize portaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE)
frames = [] # A python-list of chunks(numpy.ndarray)
print("Please speak!")
for _ in range(0, int(RATE / CHUNKSIZE * RECORD_SECONDS)):
data = stream.read(CHUNKSIZE)
frames.append(np.fromstring(data, dtype=np.int16))
#Convert the list of numpy-arrays into a 1D array (column-wise)
numpydata = np.hstack(frames)
# close stream
stream.stop_stream()
stream.close()
p.terminate()
# save audio to disk
wav.write('out.wav',RATE,numpydata)
# Open audio file(.wav) …Run Code Online (Sandbox Code Playgroud) 我有一个形状为[n,m] 的numpy矩阵A和一个长度为n 的数组b.我需要的是采取的总和b [I]的第i行的至少元件甲.所以代码可能如下所示:
A = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
b = np.array([2,3,1])
sums = magic_function() #sums = [3, 15, 7]
Run Code Online (Sandbox Code Playgroud)
我考虑过np.apply_along_axis()函数,但在这种情况下,你的函数似乎只能依赖于行本身.