可以在 python 中借助语音命令打开应用程序的语音助手
voice assistant that can open applications with the help of voice commands in python
大家好,
我一直在尝试构建一个语音助手,它可以在 python 中借助语音命令打开应用程序。由于我只是初学者,所以我只在 google 和维基百科上尝试过,但它不起作用。它只会打开维基百科,即使我告诉他打开 Google。谁能帮我解决这个问题?顺便说一句,我在 mac 上。
非常感谢
我的代码:
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...")
text = speech_engine.recognize_google(audio, language="de-DE")
print(text)
if speech_engine == "Open Google":
webbrowser.open("https.//google.com")
else:
webbrowser.open("https://wikipedia.org")´´´
在您的代码中,text
返回的是您的输出,而不是 speech_engine
,因此您应该更改条件,它会起作用!
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...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == "Open Google" or text=="Google":
webbrowser.open("https.//google.com")
else:
webbrowser.open("https://wikipedia.org")
输出:
Recording...
Recognizing...
Google
我真的不知道这是否是一个好的答案,但我发现助手在执行第一个 if 语句中使用的命令时遇到问题。 elif 语句中的所有其他命令都可以正常工作。这就是我的代码:
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...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == 'come on':
print("I am sorry, Sir")
elif text == 'open Wikipedia':
webbrowser.open("https://wikipedia.org")
print("opening Wikipedia")
elif text == 'open Google':
webbrowser.open("https://google.com")
print("opening Google")
elif text == 'open stack overflow':
webbrowser.open("https://whosebug.com")
print("opening Google")
else:
print("Unrecognized Command")
´´´
大家好, 我一直在尝试构建一个语音助手,它可以在 python 中借助语音命令打开应用程序。由于我只是初学者,所以我只在 google 和维基百科上尝试过,但它不起作用。它只会打开维基百科,即使我告诉他打开 Google。谁能帮我解决这个问题?顺便说一句,我在 mac 上。 非常感谢
我的代码:
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...")
text = speech_engine.recognize_google(audio, language="de-DE")
print(text)
if speech_engine == "Open Google":
webbrowser.open("https.//google.com")
else:
webbrowser.open("https://wikipedia.org")´´´
在您的代码中,text
返回的是您的输出,而不是 speech_engine
,因此您应该更改条件,它会起作用!
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...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == "Open Google" or text=="Google":
webbrowser.open("https.//google.com")
else:
webbrowser.open("https://wikipedia.org")
输出:
Recording...
Recognizing...
Google
我真的不知道这是否是一个好的答案,但我发现助手在执行第一个 if 语句中使用的命令时遇到问题。 elif 语句中的所有其他命令都可以正常工作。这就是我的代码:
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...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == 'come on':
print("I am sorry, Sir")
elif text == 'open Wikipedia':
webbrowser.open("https://wikipedia.org")
print("opening Wikipedia")
elif text == 'open Google':
webbrowser.open("https://google.com")
print("opening Google")
elif text == 'open stack overflow':
webbrowser.open("https://whosebug.com")
print("opening Google")
else:
print("Unrecognized Command")
´´´