karen_constantine

Untitled

Nov 21st, 2023
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #Сначала установите библиотеки:
  2. pip install SpeechRecognition
  3. pip install datetime
  4. pip install pyttsx3
  5.  
  6. #Далее сам код:
  7. from pyttsx3 import init
  8. from datetime import datetime
  9.  
  10. engine = init()
  11. engine.setProperty("rate", 120)
  12. engine.setProperty("volume", 0.9)
  13.  
  14. def talk(text):
  15. engine.say(text)
  16. engine.runAndWait()
  17.  
  18. def take_commands():
  19. recognizer = Recognizer()
  20. with Microphone() as source:
  21. print("Я слушаю...")
  22. audio = recognizer.listen(source)
  23.  
  24. try:
  25. print("Распознавание...")
  26. command = recognizer.recognize_google(audio, language="ru-RU")
  27. print(f"Вы сказали: {command}")
  28. return command.lower()
  29. except UnknownValueError:
  30. print("Извините, не могу распознать голос")
  31. return ""
  32.  
  33. while True:
  34. command1 = take_commands()
  35. print(command1)
  36. if "как дела" in command1:
  37. talk("Нормально")
  38. elif "стоп" in command1:
  39. talk("Ну пока")
  40. break
  41. elif "время" in command1:
  42. current_time = datetime.now().strftime("%I:%M %p")
  43. current_date = datetime.today().strftime("%d.%m.%Y")
  44. talk(f"Сейчас {current_time}, сегодня {current_date}")
  45. else:
  46. talk("")
Advertisement
Add Comment
Please, Sign In to add comment