AI响应时间延迟python

时间:2019-09-15 14:16:20

标签: python artificial-intelligence speech-recognition mixer gtts

我一直在使用Google的文字到语音功能以python开发AI。但是,当AI与我对话时,语音响应时间非常慢。我不明白为什么。

import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
import webbrowser
from pygame import mixer
from tempfile import TemporaryFile

def speak(audioString):
 print(audioString)
 mixer.init()
 tts = gTTS(text=audioString, lang='en')
 sf = TemporaryFile()
 tts.write_to_fp(sf)
 sf.seek(0)
 mixer.music.load(sf)
 mixer.music.play()


def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone() as source:
     print("Say something!")
     audio = r.listen(source)

    # Speech recognition using Google Speech Recognition
    data = ""
    try:
        # Uses the default API key
        # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        data = r.recognize_google(audio)
        print("You said: " + data)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data

def jarvis(data):
 if "how are you" in data:
  speak("I am fine")

 if "what time is it" in data:
  speak(ctime())

 if "where is" in data:
  data = data.split(" ")
  location = data[2]
  speak("Hold on Charlie, I will show you where " + location + " is.")
  url ="https://www.google.nl/maps/place/" + location
  chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
        #os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")
  webbrowser.get(chrome_path).open(url)

# initialization
time.sleep(2)
speak("Hi Charlie, what can I do for you?")
while 1:
    data = recordAudio()
    jarvis(data) 

我认为这与将数据存储在临时文件中并通过混合器库读取数据有关。任何帮助将不胜感激。

0 个答案:

没有答案