我正在創建一個語音控制機器人來控制我的 PC。我最近從內置 STT 的 Windows 切換到 Amazon Polly,并開始注意到某些函式在函式被觸發和回傳之間會有延遲。我確實注意到,如果我繞過語音轉文本功能、麥克風和谷歌識別器,所有功能都可以毫無延遲地運行。但是,例如,當我將語音轉文本用于時間函式時,在啟動時間函式和回傳時間之間有超過 4 分鐘的時間。
當心我是一個相當業余和自學成才的程式員,所以我的代碼有點亂。這是我處理語音轉文本的主要部分。我懷疑它可能會與事物互動并產生問題,但老實說不知道。
我什至不確定如何確定是否是問題所在。因為沒有真正的“問題”,只是需要很長時間才能完成。
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
import speech_recognition as sr
import run_commands as commands
import speech_engine as engine
import time
import signal
from command_triggers import *
from playsound import playsound
import threading
import random
r = sr.Recognizer()
m = sr.Microphone(0)
# create all combinations of aiNames and triggerWords
for name in aiNames:
aiTriggers.append(name)
for word in aiGreetings:
aiTriggers.append(word " " name)
# print(aiTriggers)
# checks if string starts with series of strings from list.
# returns matching string, else blank
def startsWithList(text, list):
for each in list:
if text.startswith(each):
# print(each)
return each
return ""
def callback(recognizer, audio):
try:
recognizedText = recognizer.recognize_google(audio).lower()
print("Google Speech Recognition thinks you said " recognizedText)
# check if spoken text uses ai keywords. If yes remove trigger from text then run command.
output = recognizedText.replace(startsWithList(recognizedText, aiTriggers), "").strip()
# print(output)
if output != "" and startsWithList(recognizedText, aiTriggers) != "":
print("Google Speech Recognition thinks the command was " output)
# engine.speakText(command)
commands.run(output)
elif output == "":
engine.speakText(random.choice(aiGreetings))
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))
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source, duration=2)
r.dynamic_energy_threshold = False
r.energy_threshold = 400
stop_listening = r.listen_in_background(m, callback)
# print("Hi, I am ava. How can I help.")
# playsound(dir_path '\startup.mp3')
engine.speakText("Hi, I am ava. How can I help.")
# commands.run("what is the weather")
while True:
pass
這是時間命令。我不知道有什么東西會導致它有這么長時間的延遲,但也許有什么東西?它自己運行良好。
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from datetime import datetime, timezone
import pytz
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
import random
import speech_engine as engine
timeSpeakingOptions=["Its currently", "Its", "the time is", "the local time is"]
def getTime(location):
print("time running")
textToSpeak = ""
if location == "":
geolocator = Nominatim(user_agent="Your_Name")
place = geolocator.geocode("battle ground, washington")
timezone = TimezoneFinder().timezone_at(lng=place.longitude, lat=place.latitude)
currentTime = datetime.now(pytz.timezone(timezone)).strftime("%I:%M %p")
textToSpeak = "The current time is " currentTime
# print(textToSpeak)
elif location != "":
geolocator = Nominatim(user_agent="Your_Name")
place = geolocator.geocode(location)
timezone = TimezoneFinder().timezone_at(lng=place.longitude, lat=place.latitude)
currentTime = datetime.now(pytz.timezone(timezone)).strftime("%I:%M %p")
textToSpeak = ("the time in " location " is " currentTime)
# print(textToSpeak)
engine.speakText(textToSpeak)
# getTime("new york")
# getTime("")
我不一定要試圖解決問題,盡管它會很好。我想學習如何嘗試解決這個問題。截至目前,我沒有足夠的知識來知道它是否是代碼,是否與我正在使用的服務有關,或者其他什么。謝謝!
uj5u.com熱心網友回復:
我沒有測驗過它,但while True: pass會消耗大量的 CPU。這可以解釋為什么其他一切都那么慢。相反,請確保在這樣的回圈中包含一個暫停,讓 CPU 喘口氣。例子:
while True:
time.sleep(1)
此外,如果識別器正在后臺偵聽,我認為您需要呼叫某些函式來停止它,以避免越來越多的后臺行程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/529206.html
下一篇:實體組態檔未添加到EC2實體
