如何打印并自动记录SpeechRecognition中adjust_for_ambient_noise设置的能量阈值?
获取Speech Recognition库中设置后的能量阈值
在SpeechRecognition库中,调用adjust_for_ambient_noise()方法后,计算出的能量阈值会直接存储在Recognizer实例的energy_threshold属性中,直接访问该属性就能获取当前已设置的阈值。
如果要完成多次测试并自动记录阈值,可以参考以下代码:
import speech_recognition as sr # 初始化识别器 r = sr.Recognizer() # 以麦克风作为音频源(也可替换为音频文件源) with sr.Microphone() as source: test_count = 3 # 设置测试次数 threshold_log = [] for idx in range(test_count): print(f"开始第{idx+1}次环境噪音采集...") # 调整环境噪音阈值 r.adjust_for_ambient_noise(source, duration=5) # 获取当前阈值并记录 current_threshold = r.energy_threshold threshold_log.append(current_threshold) print(f"第{idx+1}次测试的能量阈值: {current_threshold}") # 输出所有记录的阈值 print("\n所有测试的能量阈值记录:", threshold_log)
每次调用adjust_for_ambient_noise()时,r.energy_threshold会被更新为本次计算出的阈值,将其存入列表即可完成自动记录。
内容的提问来源于stack exchange,提问作者Simon Arthur
相关产品推荐
相关产品推荐

