[python]代码库
import speech_recognition as sr
import pyttsx3
engine = pyttsx3.init()
def process_command(command):
if "灯" in command:
if "开" in command:
print("开灯")
engine.say("已开灯")
engine.runAndWait()
elif "关" in command:
print("关灯")
engine.say("已关灯")
engine.runAndWait()
r = sr.Recognizer()
while True:
with sr.Microphone() as source:
print("请说话")
audio = r.listen(source)
try:
text = r.recognize_google(audio, language='zh-CN')
print(f"您说了: {text}")
process_command(text)
except Exception as e:
print(e)