我有一个正在使用pyaudio的实时麦克风流,并且正在通过机器学习模型运行。它从麦克风获取实时输入,将其转换为Melspectrogram,并在多个帧后将melspectrogram的快照另存为JPG,然后通过ML模型发送。
我只能让程序在流停止之前通过模型发送1个jpg。
我如何连续进行此操作?
rate = 32000
CHUNK = rate // 4
def Processing(stream):
frames = []
fig = plt.figure(figsize=(10, 4))
do_melspec = librosa.feature.melspectrogram
pwr_to_db = librosa.core.power_to_db
while True:
start = time.time()
data = stream.read(CHUNK)
data = np.fromstring(data, dtype=np.float32)
melspec = do_melspec(y=data, sr=rate, n_mels=256, fmax=16000)
norm_melspec = pwr_to_db(melspec, ref=np.max)
frames.append(norm_melspec)
if len(frames) == 20:
#converts to mel spectrogram then JPG then JPG through model
if __name__=="__main__":
p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paFloat32,channels=1,
rate=rate,input=True,
output=True,
input_device_index=0,frames_per_buffer=CHUNK)
for i in range(sys.maxsize**10):
Processing(stream)
stream.stop_stream()
stream.close()
p.terminate()