파이썬에서 음성을 이벤트로 받고싶어요.

조회수 1022회

입력된 음성을 텍스트로 변환후 알맞는 대답(텍스트로 된 대사)을 음성으로 다시 출력해주는 프로그램을 만들어 보고 있습니다

지금은 while 문으로 무한루프 돌려서 계속 받게 하고있는데 이렇게 말고 이벤트로 특정 단어가 들어왔을때 반응하도록 하고 싶은데 어떻게 해야 하나요?

import speech_recognition as sr

import pyttsx3
import requests
import urllib
from bs4 import BeautifulSoup

def Saying(msg):
    engine = pyttsx3.init()  # 보이스엔진 초기화
    voices = engine.getProperty('voices')
    volume = engine.getProperty('volume')
    rate = engine.getProperty('rate')
    print('rate = ',rate)
    engine.setProperty('rate', 180)
    engine.say(msg)
    engine.runAndWait()


def main():

    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("Speak:")
            try:
                audio = r.listen(source, timeout=0, phrase_time_limit=2.5)
                print("You said: " + r.recognize_google(audio, language="ko-KR"))
                if r.recognize_google(audio, language="ko-KR") == '날씨 알려 줘':
                    print('날씨 알려줘가 입력되었습니다.')
                    #get_weather()
                if r.recognize_google(audio, language="ko-KR") == '정원아':
                    print('정원아 가 입력되었습니다.')
                    Saying('부르셨나요')
                else:
                    print('알 수 없는 명령어입니다.')
                    Saying('무슨 말인지 모르겠어요')
            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")

#메인 부분
if __name__ == "__main__":
    main()

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)