Python语音识别响应很慢

标签 python speech-to-text ibm-watson

我有一个脚本,该脚本使用 Python 中的 voice_recognition 中构建的 IBM Speech to Text API。它非常慢。回复时间大概是5秒左右就回复我了。我怀疑while True循环,导致CPU烧毁,但我不知道。

这是代码:

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))

最佳答案

先录制音频然后将其发送到服务器是一个根本错误的做法。记录需要时间,发送数据并获取响应也需要时间。

如果您想要良好的交互,您需要使用 Websockets 将音频流式传输到服务器,而不是通过 HTTP 请求发送。录制结束时服务器会向您返回解码结果。

speech_recognition库在这里设计得不好,你应该直接使用IBM接口(interface)和python websocket模块。 Python 示例在这里:

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

关于Python语音识别响应很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054151/

相关文章:

python - 将 yield 与 dict 理解一起使用

python - 防止 TextIOWrapper 以 Py2/Py3 兼容的方式关闭 GC

android - 使用音频文件进行语音识别?

android - 将任何语言的语音转换为 Android 中的文本

python - 由exec(variable =)在for循环中分配的变量范围?

machine-learning - IBM Watson 语音识别准确率较低

python - 类型错误 : Object of type 'Entities' is not JSON serializable IBM Cloud natural language understanding

python - 几分钟后 Google App Engine 内部服务器错误

python - Python 和 Node.js 的不同 WPI v3 结果

python - 有没有办法在 Python 中生成带有迭代变量的语句 block ?