Python中的语音识别问题

时间:2020-06-10 10:17:07

标签: python

我正在使用Python3.7。我的计算机中还安装了Pyaudio和SpeechRecognition。但是运行代码后,输出将继续侦听,一段时间后它将停止侦听,并且不会提供任何类型的输出。

这是我正在使用的代码,我正在PyCharm IDE中运行此代码

import pyttsx3
import datetime
import speech_recognition as sr

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[1].id

def speak(audio):
    engine.say(audio)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak("Good Morning Sir")

    elif hour >= 12 and hour < 18:
        speak("Good Afternoon Sir")

    else:
        speak("Good Evening")

    speak("I am jarvis")


def takecommand():
    r = sr.Recognizer()
    with sr.Microphone() as   source:
        print("listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(query)

    except Exception as e:
        print(e)
        print("say that again plz....")
        return "None"
    return query


if __name__ == "__main__":
    wishMe()
    takecommand()

0 个答案:

没有答案