我正在使用Python制作像jarvis这样的个人助理,但是我在语音响应方面遇到了问题。当我说出命令时,它会回答,但只能是文本。没有声音。
我目前正在学习Python。希望你能帮助我解决它,谢谢。
这些是我使用的要求:
certifi==2017.11.5
chardet==3.0.4
gTTS==1.2.2
gTTS-token==1.1.1
idna==2.6
mpg123==0.4
PyAudio==0.2.11
pytz==2017.3
SpeechRecognition==3.8.1
urllib3==1.2
这是我用来运行项目的代码:
from gtts import gTTS
import speech_recognition as sr
import os
import webbrowser
import smtplib
import pyttsx3
def talkToMe(audio):
print(audio)
for line in audio.splitlines():
os.system("say " + audio)
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('waiting for your command sir')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.listen(source)
try:
command = r.recognize_google(audio).lower()
print('You said: ' + command + '/n')
except sr.UnknownValueError:
print('Your last command couldn\'t be heard')
command = myCommand();
return command
def assistant(command):
if 'hello' in command:
talkToMe('Im here sir')
talkToMe('I am online and ready sir')
#loop to continue executing multiple commands
while True:
assistant(myCommand())
答案 0 :(得分:0)
你可以试试这个(如果你在 Windows 机器上)
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
顺便说一下,我在 line os.system("say " + audio)
之前看到了一个额外的空格。