如何解决如何在python中用单词激活语音识别
目前,我正在从事Virtual Assistant项目 其中,它接受语音命令并执行。
现在,我将他命名为'MARK'
现在,我已为该代码提供了访问我的麦克风的权限
这是代码.....
import speech_recognition as sr
def takeCommand():
# It is the recognizer
r=sr.Recognizer()
# Kept the microphone as source
with sr.Microphone() as source: # It should keep listening but only when I say 'Mark'
print("Listening...")
r.pause_threshold=1
audio = r.listen(source)
try:
print("Reconizing...")
query = r.recognize_google(audio,language="en-in")
print(f"You said : {query}\n")
except Exception as error:
# print(error)
print('Say that again please')
在此功能的此处,他听到了一切,然后将其转换为文本
但是我不希望他一直在听我讲话,我的意思是,他应该听,但是只有在我说“ MARK”时才回答。
为此,我更改了这样的代码。...
def listen_to_navdeep():
# It is the recognizer
r=sr.Recognizer()
# Kept the microphone as source
with sr.Microphone() as source: # It should keep listening but only when I say 'Mark'
r.pause_threshold=1
audio = r.listen(source)
while True:
try:
query = r.recognize_google(audio,language="en-in")
query=query.lower()
if "mark" in query:
return query
break
else:
pass
except:
pass
# Now I am outside the function....
while True:
command=listen_to_navdeep
# Some process runs over here.....
现在,即使我这样做了,也会有一些时光倒流
所以我想做的是,他应该积极地聆听声音,但仍然应该做出响应(也不要打印没有任何语音的异常)或将其转换为文本并运行一些过程,实际上我告诉“马克”的那一刻,他应该开始录制声音,然后将其处理为文本并执行分配的任何操作(请不要担心它将要执行的任务)
另一个解释我的问题的好例子是“ Alexa”的工作方式。 Alexa会继续聆听家里或周围的一切,但没有反应。每当我说“ Alexa”时,她只会做出反应。这就是我想要的....在我的AI助手中。
请告诉我如何实现。...
期待获得帮助。...
解决方法
您可以为他说一个语音功能,您将必须导入pyttsx3模块。 通过在终端中运行此命令来安装它。
pip install pyttsx3
speak函数的工作原理如下
speak("Thank Me Later For this and upvote")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。