You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何打印并自动记录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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:10:35