Python智能语音助手运行异常:输出奇怪文本问题排查求助
智能语音助手代码异常排查:奇怪文本输出问题
我尝试用Python开发一款智能语音助手,但运行代码时出现奇怪的文本输出。预期功能是通过麦克风获取输入,依据intents.json文件执行对应操作并进行语音反馈。
原代码:
from email.mime import audio from logging.config import listen import re from neuralintents import GenericAssistant import speech_recognition import sys import pyttsx3 as tts recognizer = speech_recognition.Recognizer() speaker = tts.init() speaker.setProperty('rate',150) def create_note(): global recognizer speaker.say("What do you want to say?") speaker.runAndWait() done=False while not done: try: with speech_recognition.Microphone() as mic: recognizer.adjust_for_ambient_noise(mic, duration=0.2) audio= recognizer.listen(mic) note = recognizer.recognize_google(audio) note = note.lower() speaker.say("choose a file name") speaker.runAndWait recognizer.adjust_for_ambient_noise(mic, duration=.2) audio = listen(mic) filename = recognizer.recognize_google(audio) filename = filename.lower() with open(filename,'w' ) as f: f.write(note) done= True speaker.say={f"I saved the note {filename}"} speaker.runAndWait() except speech_recognition.UnknownValueError: recognizer = speech_recognition.Recognizer() speaker.say("I dont got that, please say it again") speaker.runAndWait() mappings = {'greeting': create_note} assitant = GenericAssistant('intents.json',intent_methods=mappings) assitant.train_model()
intents.json内容(根据提供的图片整理):
{ "intents": [ { "tag": "greeting", "patterns": ["hello", "hi", "hey", "what's up"], "responses": ["Hello! How can I help you?", "Hi there! What do you need?"] } ] }
问题排查与修正:
- 错误的
listen导入:原代码导入了from logging.config import listen,但实际需要调用的是speech_recognition.Recognizer的listen方法,当前导入的日志模块listen函数会触发错误,产生奇怪输出。修正:删除该导入语句,改用recognizer.listen(mic)。 - 未执行语音播放方法:
speaker.runAndWait缺少括号,应改为speaker.runAndWait(),否则语音不会播放,程序逻辑会卡住或异常。 - 错误的语音赋值语法:
speaker.say={f"I saved the note {filename}"}使用了赋值符号=,正确调用方式是speaker.say(f"I saved the note {filename}"),原代码把字典赋值给say方法会导致错误输出。 - 缺少模型监听逻辑:代码仅训练了模型,但没有启动语音输入监听循环,无法触发意图匹配和对应操作,需添加循环获取语音输入的逻辑。
- 不必要的
recognizer重置:捕获UnknownValueError时重新初始化recognizer无意义,反而可能导致识别异常,直接重新监听即可。
修正后的完整代码:
import speech_recognition import pyttsx3 as tts from neuralintents import GenericAssistant # 初始化识别器和语音引擎 recognizer = speech_recognition.Recognizer() speaker = tts.init() speaker.setProperty('rate', 150) def create_note(): speaker.say("What do you want to say?") speaker.runAndWait() done = False while not done: try: with speech_recognition.Microphone() as mic: recognizer.adjust_for_ambient_noise(mic, duration=0.2) # 获取笔记内容 audio = recognizer.listen(mic) note = recognizer.recognize_google(audio).lower() # 获取文件名 speaker.say("Choose a file name") speaker.runAndWait() recognizer.adjust_for_ambient_noise(mic, duration=0.2) audio = recognizer.listen(mic) filename = recognizer.recognize_google(audio).lower() # 保存文件 with open(f"{filename}.txt", 'w') as f: f.write(note) done = True speaker.say(f"I saved the note as {filename}.txt") speaker.runAndWait() except speech_recognition.UnknownValueError: speaker.say("I didn't catch that, please say it again") speaker.runAndWait() # 意图映射与模型初始化 mappings = {'greeting': create_note} assistant = GenericAssistant('intents.json', intent_methods=mappings) assistant.train_model() # 启动语音监听循环 while True: try: with speech_recognition.Microphone() as mic: recognizer.adjust_for_ambient_noise(mic, duration=0.2) audio = recognizer.listen(mic) text = recognizer.recognize_google(audio).lower() assistant.request(text) except speech_recognition.UnknownValueError: recognizer = speech_recognition.Recognizer()
内容的提问来源于stack exchange,提问作者Ankur Pathak
相关产品推荐
相关产品推荐

