Here we are planning to say “forty eight multiplied by sixty seven” and get the math done for which we will have to do go through the following steps
- install packages
speech_recognition and win32com.client
pip install win32com.client pip install speech_recognition
- access mic
a = sr.Recognizer() with sr.Microphone() as source:
- correct the background noise
a.adjust_for_ambient_noise(source) audio = a.listen(source) - store the voice in a .wav file
with open("recorded_audio.wav", "wb") as f: f.write(audio.get_wav_data()) - read convert .wav into text
text = a.recognize_google(audio) bolo.speak("You said: " + text) - text into integer
- perform the math
- invoke the SAPI.spVoice and speak the final answer
bolo = win32com.client.Dispatch("SAPI.SpVoice") bolo.speak("")
Thus the final code for the whole program to give an audio input, process the math and recieve the audio output is:
import speech_recognition as sr
import win32com.client
bolo = win32com.client.Dispatch("SAPI.SpVoice")
bolo.speak("Speak your question")
a = sr.Recognizer()
with sr.Microphone() as source:
a.adjust_for_ambient_noise(source)
audio = a.listen(source)
try:
text = a.recognize_google(audio)
bolo.speak("You said: " + text)
print("You said: " , text)
with open("recorded_audio.wav", "wb") as f:
f.write(audio.get_wav_data())
except Exception as e:
print("Error: " + str(e))
l=text.split()
print(l)
if l[1]=='+':
s=int(l[0])+int(l[2])
bolo.speak("the sum is "+str(s))
print("the sum is ",str(s))
elif l[1]=='-':
s=int(l[0])-int(l[2])
bolo.speak("the difference is "+str(s))
print("the difference is ",str(s))
elif l[1].capitalize()=='X':
s=int(l[0])*int(l[2])
bolo.speak("the product is "+str(s))
print("the product is "+str(s))
elif l[1]=='/':
s=round(int(l[0])/int(l[2]),5)
bolo.speak("the quotient is"+str(s))
print("the quotient is"+str(s))
else:
bolo.speak("Sorry, Either you are not clear or I am a bit dumb")