缩短回答延迟
Make delay of answer shorter
我正在 python 上制作语音助手。
这是代码:
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
if "hello" in text:
speak("Hi, how are you?")
当我说“你好”时,它会思考 2 秒钟,然后说“嗨,你好吗?”
也许是因为我正在保存 mp3 文件?如何让程序及时响应?
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
starttime = time.perf_counter()
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
elapsed=time.perf_counter()-starttime
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
if "hello" in text:
speak("Hi, how are you?")
我正在 python 上制作语音助手。
这是代码:
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
if "hello" in text:
speak("Hi, how are you?")
当我说“你好”时,它会思考 2 秒钟,然后说“嗨,你好吗?”
也许是因为我正在保存 mp3 文件?如何让程序及时响应?
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
starttime = time.perf_counter()
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
elapsed=time.perf_counter()-starttime
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
if "hello" in text:
speak("Hi, how are you?")