2017-02-05 1 views
2

Python의 speech_recognition에 내장 된 IBM Speech to Text API를 사용하는 스크립트가 있습니다. 그것은 매우 느립니다. 응답 시간은 나에게 응답하는 약 5 초입니다. True 루프가 의심 스럽기 때문에 CPU가 소모되는 원인이되지만 잘 모르겠습니다.Python 음성 인식 응답이 매우 느림

import speech_recognition as sr 
import pyttsx 
import time 
import requests 

engine = pyttsx.init() 

time_check = time.strftime("%H") 
time_check = int(time_check) 

# current weather 

url = "http://api.openweathermap.org/data/2.5/weather?lat=59.13&lon=10.22&APPID=8f605c186309e3d8f60bb7b2f31ba75c&units=metric" \ 
      "" 

r = requests.get(url) 
response_dict = r.json() 

#daily forecast 

url_daily = "http://api.openweathermap.org/data/2.5/forecast/daily?q=Sandefjord&mode=JSON&units=metric&cnt=7&appid=8f605c186309e3d8f60bb7b2f31ba75c" \ 
     "" 

r_daily = requests.get(url_daily) 
response_dict_daily = r_daily.json() 
days = response_dict_daily["list"] 
tomorrow = days[1] 





#weather variables 

main = response_dict["main"] 

wind = response_dict["wind"] 

description = response_dict["weather"] 



url_1 = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=d35bd4b8699b444fbcd3661e39c0bf49" 

f = requests.get(url_1) 
response = f.json() 
articles = response["articles"] 
article_1 = articles[0] 
article_2 = articles[1] 
article_3 = articles[2] 
article_4 = articles[3] 

title_1 = article_1["title"] 
title_2 = article_2["title"] 
title_3 = article_3["title"] 
title_4 = article_4["title"] 





while True: 

    r = sr.Recognizer() 
    with sr.Microphone() as source: 
     print("Say something!") 
     audio = r.listen(source) 

    try: 


     if "hello" in r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD): 
     if time_check < 9: 
      engine.say("Good morning sir, did you sleep well?") 
      engine.runAndWait() 
     elif time_check < 16: 
      engine.say("Good afternoon sir") 
      engine.runAndWait() 

     elif time_check > 16: 
      engine.say("Good evening sir") 
      engine.runAndWait() 

    if "update" in r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD): 
     print("Initializing news listing...") 
     engine.say(title_1) 
     engine.say(title_2) 
     engine.say(title_3) 
     engine.say(title_4) 
     engine.runAndWait() 

    if "time" in r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD): 
     time_say = time.strftime("%H:%M") 
     engine.say(time_say) 
     engine.runAndWait() 

    if "date" in r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD): 
     time_saydate = time.strftime("%B %d") 
     engine.say(time_saydate) 
     engine.runAndWait() 

    if "weather" in r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD): 
     temp = main["temp"] 
     windspeed = wind["speed"] 
     umbrella = description[1] 
     description = description["description"] 
     engine.say("Here is a quick overview of current weather. The temperature is at " + temp + "degrees. The wind speed is at " + windspeed + "meters per second. Overall it is " + description) 

     if umbrella == "Rain": 
      engine.say("I recommend you bring an umbrella sir, it is raining") 


    if "tomorrow" in r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD): 
     temp = tomorrow["temp"] 
     temp_1 = temp["day"] 
     temp_1 = int(temp_1) 
     desc = tomorrow["weather"] 
     desc_1 = desc[0] 
     desc_2 = desc_1["description"] 

     engine.say("The weather for tomorrow is " + str(temp_1) + " degrees. The description is " + desc_2) 
     engine.runAndWait() 

     if desc_2 == "rain": 
      engine.say("It is going to rain, I would recommend bringing an umbrella") 
      engine.runAndWait() 












    print("Watson thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD)) 
except sr.UnknownValueError: 
    print("IBM Speech to Text could not understand audio") 
    engine.say("Couldnt recognize, please try again sir") 
    engine.runAndWait() 
except sr.RequestError as e: 
    print("Could not request results from IBM Speech to Text service; {0}".format(e)) 

답변

2

가 먼저 오디오를 녹음 한 다음 서버로 보내 근본적으로 잘못된 일이있다 : 여기

는 코드입니다. 기록하는 데 시간이 걸리고 데이터를 보내고 응답을 되 찾는 데 시간이 걸립니다.

좋은 상호 작용을 원할 경우 HTTP 요청으로 보내지 않고 웹 소켓을 사용하여 오디오를 서버로 스트리밍해야합니다. 녹음이 끝날 때까지 서버는 디코딩 결과를 다시 보냅니다.

speech_recognition 라이브러리는 잘 설계되지 않았으므로, IBM 인터페이스를 python websocket 모듈과 함께 직접 사용해야합니다. 파이썬 예제는 여기에 있습니다 :

https://github.com/watson-developer-cloud/speech-to-text-websockets-python

+1

예이 정확히 무엇을해야하는지, 링크의 코드를 시도하십시오 알려 주시기입니다! 감사 니콜라이 –