import pyaudio import numpy as np import time CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 def audioCheckAI(): # 麦克风检测ai global CHUNK,FORMAT,CHANNELS,RATE p = pyaudio.PyAudio() stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK) while True: data = stream.read(CHUNK) audio_data = np.fromstring(data,dtype=np.short) max_dB = np.max(audio_data) if max_dB >= 60: # 非常弱智的判断,后期改进 print('报警 ' + str(max_dB)) if __name__ == "__main__": audioCheckAI()