import speech_recognition as sr
r = sr.Recognizer()
import os
import subprocess as sp
print("Voice Recognition Software\n\n***********************************\n")
success = False
while True:
r.energy_threshold = 4000
t = None
with sr.Microphone() as source:
print (">")
audio = r.listen(source)
try:
print("Processing...")
t = r.recognize_google(audio)
print (": " + t)
except sr.UnknownValueError:
print("Unknown input")
continue
except sr.RequestError as e:
print("An error occured at GAPI\nA common cause is lack of internet connection")
continue
if "open" in t:
t = t.replace("open","")
t = t.replace(" ","")
t = t + ".exe"
print (t)
for a,d,f in os.walk("C:\\"):
for files in f:
if files == t.lower() or files == t.capitalize() or files == t.upper():
pat = os.path.join(a,files)
print (pat)
sp.call([pat])
success = True
if success == True:
continue
我有上面的代码,我的个人项目有关语音识别和命令。它不是完美或完整的,我知道,我一会儿就开始蟒蛇了。
问题是,有时代码会在>
之后或Processing
之后挂起。
没有错误消息,有时它会在一两分钟后自行开始工作。
我不知道这个问题的原因,如果你们能帮助我,那就太棒了。否则代码正在运行,它理解我并且可以执行我编写的唯一命令。
我试图使用信号模块和SIGALRM,所以当代码没有响应超过几秒钟时它会重启,但意识到它与Windows 10不兼容。我已经检查了相同的方法从各种其他线程执行此操作,但我不太了解它们或如何在我的代码中实现它们。