如何修改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
相关产品推荐
相关产品推荐

