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

12岁编程新手求助:MacOS Catalina下Python语音识别程序卡顿报错

Fixing Speech Recognition Freeze on macOS Catalina

Hey there! As someone who’s tackled similar macOS speech recognition hiccups, let’s get your script working smoothly. That KeyboardInterrupt error means your program got stuck waiting for audio input—here’s why that’s happening and how to fix it:

Common Causes & Solutions

1. macOS Catalina Microphone Permissions (Most Likely Culprit)

macOS has super strict privacy controls, and your Python environment (like PyCharm) probably doesn’t have access to your microphone yet:

  • Open System Preferences > Security & Privacy > Privacy > Microphone
  • Look for your code editor (PyCharm) or terminal in the list. If it’s there, make sure the checkbox is checked.
  • If it’s not listed, run your script again—macOS should pop up a permission request. Click "Allow" to grant access.

2. Improve Script Robustness

Your original code doesn’t handle initialization delays or background noise, which can cause it to freeze. Update your code with these tweaks:

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    print("ADJUSTING FOR BACKGROUND NOISE...")
    # Let the recognizer adapt to ambient noise for 1 second
    r.adjust_for_ambient_noise(source, duration=1)
    print("SAY SOMETHING")
    
    # Add timeout to avoid infinite waiting, limit phrase length to 10 seconds
    audio = r.listen(source, timeout=5, phrase_time_limit=10)
    print("THANK YOU")

try:
    print("TEXT: " + r.recognize_google(audio))
except sr.UnknownValueError:
    print("SORRY I DON'T KNOW WHAT YOU MEAN")
except sr.RequestError:
    print("OOPS, CAN'T CONNECT TO GOOGLE'S RECOGNITION SERVICE RIGHT NOW")
except Exception as e:
    print(f"UNEXPECTED ERROR: {e}")
  • adjust_for_ambient_noise helps the recognizer ignore background sounds
  • timeout and phrase_time_limit prevent the script from freezing indefinitely
  • Specific exception handling tells you exactly what went wrong (e.g., no internet for Google’s service)

3. Verify Microphone & PyAudio Setup

If permissions aren’t the issue, check your hardware and dependencies:

  • List available microphones to make sure your system detects your mic:
    import speech_recognition as sr
    for index, name in enumerate(sr.Microphone.list_microphone_names()):
        print(f"Microphone Index: {index} | Name: {name}")
    
    If your mic shows up, initialize Microphone with its index (e.g., sr.Microphone(device_index=0)).
  • Reinstall PyAudio if it’s outdated or incompatible:
    1. First install PortAudio (required for PyAudio) via Homebrew:
      brew install portaudio
      
    2. Then reinstall PyAudio:
      pip install --upgrade pyaudio
      

Give these steps a try—start with the permission check, since that’s the most common fix for Catalina. Let me know if you run into any other snags!

内容的提问来源于stack exchange,提问作者Ashish Nagariya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:05:50