diff --git a/speech_alarm/README.md b/speech_alarm/README.md new file mode 100644 index 000000000..5eb0000eb --- /dev/null +++ b/speech_alarm/README.md @@ -0,0 +1,27 @@ +# Alarm using speech recognition + +## Steps to set an alarm : +1. run the script `AlarmClock.py` using python3. + On Unix based systems the command looks like : + ```bash + python3 Alarm.py + ``` + or + ```bash + python Alarm.py + ``` + +2. After execution of the script you will be prompted to say the time to set an alarm in HH:MM format. Which looks something as shown below : + ``` + say "set alarm for (for eg)5:30am" + `` + +3. Then you will get a looping output of the current time and set time on the CLI as shown below : + ``` + set alarm for 5:30am + +4. When the alarm goes off you will __hear a sound__ and get the following lines on the prompt : + ``` + Time to Wake up + + ``` diff --git a/speech_alarm/script.py b/speech_alarm/script.py new file mode 100644 index 000000000..dc7d4c4d1 --- /dev/null +++ b/speech_alarm/script.py @@ -0,0 +1,70 @@ +import speech_recognition as sr +import winsound +import datetime +import pyttsx3 + + +engine = pyttsx3.init() + + +def speech(audio): + engine.setProperty('rate', 200) + voices = engine.getProperty('voices') + engine.setProperty('voice', voices[0].id) + engine.say(audio) + engine.runAndWait() + + +def takeCommand(): + + r = sr.Recognizer() + with sr.Microphone() as source: + r.adjust_for_ambient_noise(source, duration=1) + print() + print("Listening...") + print() + audio = r.listen(source) + + try: + print("Recognizing...") + print() + query = r.recognize_google(audio, language='en-in') + print(f"you said: {query}\n") + + except Exception as e: + print("Say that again please...") + print() + print(e) + return "None" + return query + + +def alarm(Timing): + + altime = str(datetime.datetime.now().strptime(Timing, "%I:%M %p")) + altime = altime[11:-3] + print(altime) + Horeal = altime[:2] + Horeal = int(Horeal) + Mireal = altime[3:5] + Mireal = int(Mireal) + + print(f"Done, alarm is set for {Timing}") + + while True: + if Horeal == datetime.datetime.now().hour and Mireal == datetime.datetime.now().minute: + print("alarm is running please exit the program") + winsound.PlaySound('abc', winsound.SND_LOOP) + + elif Mireal < datetime.datetime.now().minute: + break + + +speech("say set alarm for 5:30 am ") + +print("say set alarm for 5:30 am") +tt = takeCommand() +tt = tt.replace("set alarm to ", "") +tt = tt.replace(".", "") +tt = tt.upper() +alarm(tt)