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

Mac环境下Python语音助手语音指令匹配异常求助:指令打开谷歌却跳转维基百科

Fixing Your Voice Assistant's Google/Wikipedia Redirect Issue

Hey there! Let's get your voice assistant working right—there are a few straightforward issues in your code that are causing it to always open Wikipedia instead of Google. Let's break them down and fix them step by step:

1. The Core Logic Mistake

Your conditional check is comparing the speech recognizer object (speech_engine) to the string "Open Google"—this will never be true, no matter what you say! You need to check the actual text that was recognized (the text variable) instead.

2. Mismatched Language Setting

You're using language="de-DE" (German) for speech recognition, but you're giving English commands like "Open Google". The recognizer will misinterpret your English speech as German, leading to incorrect text output. Switch to an English language code like en-US or en-GB.

3. Typo in Google's URL

You wrote https.//google.com—that dot should be a colon! Correct it to https://google.com.

4. Add Error Handling (Bonus)

Speech recognition can fail for lots of reasons (background noise, unclear speech), so wrapping the recognition part in a try-except block will prevent your program from crashing unexpectedly.

Corrected Full Code

import speech_recognition as sr
import webbrowser

speech_engine = sr.Recognizer()
with sr.Microphone() as micro:
    print("Recording...")
    audio = speech_engine.record(micro, duration=5)
    print("Recognizing...")
    try:
        # Use English language for recognition
        text = speech_engine.recognize_google(audio, language="en-US")
        print(f"Recognized: {text}")
        
        # Check the recognized text (case-insensitive for better usability)
        if "open google" in text.lower():
            webbrowser.open("https://google.com")
        else:
            webbrowser.open("https://wikipedia.org")
    except sr.UnknownValueError:
        print("Sorry, I couldn't understand what you said.")
    except sr.RequestError as e:
        print(f"Couldn't request results from Google Speech Recognition service; {e}")

Key Improvements Explained

  • We check text.lower() so the command works regardless of capitalization (e.g., "Open Google", "open google", "OPEN GOOGLE" all trigger the correct action).
  • Added try-except blocks to handle common speech recognition errors gracefully instead of crashing.
  • Fixed the language setting and URL typo that were breaking functionality.

Try running this corrected code—when you say "Open Google", it should now launch Google instead of Wikipedia!

内容的提问来源于stack exchange,提问作者Erik Eicke

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 18:02:29