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

如何修改Python语音交互程序的语音输出语言?

修改语音识别语言的实现方法

你的原始代码如下:

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'whisper' in command:
                command = command.replace('whisper', '')
                print(command)
    except:
        pass
    return command

实现步骤:

  • 给recognize_google()方法添加language参数,指定目标语言的语言代码即可修改识别语言。常见语言代码示例:
    • 简体中文:zh-CN
    • 美式英语:en-US
    • 日语:ja-JP
    • 法语:fr-FR

修改后的完整代码(以简体中文为例):

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            # 添加language参数指定为简体中文
            command = listener.recognize_google(voice, language='zh-CN')
            command = command.lower()
            if 'whisper' in command:
                command = command.replace('whisper', '')
                print(command)
    except:
        pass
    return command

说明:

  • 若需切换其他语言,只需将language参数的值替换为对应语言的标准代码即可。
  • 识别准确率依赖于麦克风输入的语音与指定语言匹配,需确保输入语音为目标语言。

内容的提问来源于stack exchange,提问作者Windows Master

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 02:28:08