Python Voice Assistant Tutorial #2 - Getting Microphone Input

"WEBVTTKind: captionsLanguage: enhello everybody and welcome back so that is what we did in the last tutorial video is figure out how to make Python say or just you know output some kind of stuff throughout the speaker so that we can actually hear it so we're gonna be doing in this video is actually getting some input from the user through the microphone so we're gonna allow them to speak into the microphone we're gonna figure out what it is that they said turn that into some kind of text and then do something with that but if you guys missed the last video I mean this is the code for it is pretty straightforward but it also goes through just how to set up and get all these modules installed so definitely watch that if you haven't already all right so we're gonna get started right away here and we're gonna create a function to get some audio from the user so I'm just gonna call this function actually get underscore audio and what it's gonna do is just return the text version of whatever we say into the microphone so first of all I just want to make sure that if you guys don't have a microphone obviously this is not going to work you need a microphone and you're probably gonna get an error if you try to do what I'm gonna do without a microphone but to change your microphone settings if you just type in audio in this bottom bar and go to manage audio devices you can go here to recording and then pick what microphone you want to use you can see I have a few different ones to select here from but again you know pick what one and then that way you can actually get some kind of output or I guess give input to the computer alright so we're gonna start by doing is creating what we call a recognizer object from speech recognition module and we're gonna say s r dot recognizer so R equals s r dot recognizer now what we're gonna do is use the microphone to get some kind of inputs we're gonna say with SR dot microphone as source what we're gonna do is use this to listen so we're gonna say audio equals r dot listen source that all of course just means you know use our speech recognizer to listen to the source which is going to be the microphone and you could use something else as a source like you could use the text file or something like that and now what we're gonna do is try to figure out what they actually said so I'm gonna create a variable called said which is equal to a blank string and what I'm gonna do is create a try except Clause here and try to figure out what it is that they said so I'm going to say said equals are recognized underscored sound or not sound sorry recognize underscored Google and audio so what this is gonna do essentially is use the Google API to recognize what it is that we said and that's what you know speech recognition does that's what the module uses so we're gonna say our to recognize underscore Google and then what we're gonna do is print said to the screen so essentially we will be able to see what it is we said in a text form and now what I'm gonna do is put an accept here I'm gonna say accept exception as e because sometimes when you do this you get an error so let's say that microphone does get any input for a while you're gonna get an error or it doesn't understand what you said you're gonna get an error so we need to have an exception here where essentially we can tell a user you know we didn't understand what it is that you said or you know this error occurred so that we can just continue moving with our program we don't crash if something goes wrong so what I'm gonna do is just print a exception plus string II and what that's gonna do is just tell us what the exception was and actually I'll do it pull in here in a space we can see what it is and have a look at it so what I'm gonna do now at the end of this is return set so after we you know create this we're just going to return set and obviously if we get an error here then said it will just be equal to a blank string and we shouldn't have any issues so what I'm gonna do is start by speaking hello everybody and welcome back and then I'm else okay actually I don't want to say this whole thing again let's just say hello and then what I'm gonna do is just call this get audio function and we're gonna look and see if it can actually get what it is that we said so let's try this hello just testing here and there you go you can see that a prince here just testing here and this is really nice because the module is also actually able to detect when you're speaking and when you're not speaking just by using a basic volume level so as soon as you have a long enough pause in your sentence then what it does is it will stop recording and a execute but if you keep speaking then it will keep recording what you say so you can say longer strains or longer messages as well awesome so that is how that works so now that we've done that we're just gonna do a few small things to essentially you know speak back to the user if they say certain things now we're gonna get a little bit more complicated here in the future when we're doing things but I just want to show you what we can actually do so I'm gonna say now here text equals get audio and now we're gonna parse this text since you know if it contains hello if it as this if it has that then we're gonna say a certain thing back to the user so I'm just gonna say here if text should I do this actually I'm just gonna say if hello in text then what we're gonna do is say hello back to the user so let's try this we don't you speak hello how are you question right okay so let's just see if this actually works and let's go control B hello there hello how are you so there you go you can see how this is actually gonna work we're gonna say something we're gonna figure out if what we're saying you know like if we have a certain word in the message or if it is a certain thing and then we're gonna respond back to the user appropriately now we can also do another one like if what is your name in text then we're gonna print out or not print I guess we'll speak my name is Tim I mean we haven't named this guy yet but let just say that and let's try this one what is your name robot my name is Tim and there you go so you can see you can make as many commands as you want and I think this is kind of cool in my opinion how we can just really easily check if certain strings are in this text and then if they are we can say things back to the user so anyways I'm gonna leave the video here and I was a bit shorter but this is kind of all we need to accomplish today in terms of just getting some audio from the user as well as you know printing certain things out I welcome you guys to continue to mess around with this make this as complicated as you want and in the next few days we're going to be adding some actually useful features that aren't just you know text responding back to the user as always if you guys enjoyed make sure you leave a like and subscribe to the channel and leave any comments for future things you'd like to seehello everybody and welcome back so that is what we did in the last tutorial video is figure out how to make Python say or just you know output some kind of stuff throughout the speaker so that we can actually hear it so we're gonna be doing in this video is actually getting some input from the user through the microphone so we're gonna allow them to speak into the microphone we're gonna figure out what it is that they said turn that into some kind of text and then do something with that but if you guys missed the last video I mean this is the code for it is pretty straightforward but it also goes through just how to set up and get all these modules installed so definitely watch that if you haven't already all right so we're gonna get started right away here and we're gonna create a function to get some audio from the user so I'm just gonna call this function actually get underscore audio and what it's gonna do is just return the text version of whatever we say into the microphone so first of all I just want to make sure that if you guys don't have a microphone obviously this is not going to work you need a microphone and you're probably gonna get an error if you try to do what I'm gonna do without a microphone but to change your microphone settings if you just type in audio in this bottom bar and go to manage audio devices you can go here to recording and then pick what microphone you want to use you can see I have a few different ones to select here from but again you know pick what one and then that way you can actually get some kind of output or I guess give input to the computer alright so we're gonna start by doing is creating what we call a recognizer object from speech recognition module and we're gonna say s r dot recognizer so R equals s r dot recognizer now what we're gonna do is use the microphone to get some kind of inputs we're gonna say with SR dot microphone as source what we're gonna do is use this to listen so we're gonna say audio equals r dot listen source that all of course just means you know use our speech recognizer to listen to the source which is going to be the microphone and you could use something else as a source like you could use the text file or something like that and now what we're gonna do is try to figure out what they actually said so I'm gonna create a variable called said which is equal to a blank string and what I'm gonna do is create a try except Clause here and try to figure out what it is that they said so I'm going to say said equals are recognized underscored sound or not sound sorry recognize underscored Google and audio so what this is gonna do essentially is use the Google API to recognize what it is that we said and that's what you know speech recognition does that's what the module uses so we're gonna say our to recognize underscore Google and then what we're gonna do is print said to the screen so essentially we will be able to see what it is we said in a text form and now what I'm gonna do is put an accept here I'm gonna say accept exception as e because sometimes when you do this you get an error so let's say that microphone does get any input for a while you're gonna get an error or it doesn't understand what you said you're gonna get an error so we need to have an exception here where essentially we can tell a user you know we didn't understand what it is that you said or you know this error occurred so that we can just continue moving with our program we don't crash if something goes wrong so what I'm gonna do is just print a exception plus string II and what that's gonna do is just tell us what the exception was and actually I'll do it pull in here in a space we can see what it is and have a look at it so what I'm gonna do now at the end of this is return set so after we you know create this we're just going to return set and obviously if we get an error here then said it will just be equal to a blank string and we shouldn't have any issues so what I'm gonna do is start by speaking hello everybody and welcome back and then I'm else okay actually I don't want to say this whole thing again let's just say hello and then what I'm gonna do is just call this get audio function and we're gonna look and see if it can actually get what it is that we said so let's try this hello just testing here and there you go you can see that a prince here just testing here and this is really nice because the module is also actually able to detect when you're speaking and when you're not speaking just by using a basic volume level so as soon as you have a long enough pause in your sentence then what it does is it will stop recording and a execute but if you keep speaking then it will keep recording what you say so you can say longer strains or longer messages as well awesome so that is how that works so now that we've done that we're just gonna do a few small things to essentially you know speak back to the user if they say certain things now we're gonna get a little bit more complicated here in the future when we're doing things but I just want to show you what we can actually do so I'm gonna say now here text equals get audio and now we're gonna parse this text since you know if it contains hello if it as this if it has that then we're gonna say a certain thing back to the user so I'm just gonna say here if text should I do this actually I'm just gonna say if hello in text then what we're gonna do is say hello back to the user so let's try this we don't you speak hello how are you question right okay so let's just see if this actually works and let's go control B hello there hello how are you so there you go you can see how this is actually gonna work we're gonna say something we're gonna figure out if what we're saying you know like if we have a certain word in the message or if it is a certain thing and then we're gonna respond back to the user appropriately now we can also do another one like if what is your name in text then we're gonna print out or not print I guess we'll speak my name is Tim I mean we haven't named this guy yet but let just say that and let's try this one what is your name robot my name is Tim and there you go so you can see you can make as many commands as you want and I think this is kind of cool in my opinion how we can just really easily check if certain strings are in this text and then if they are we can say things back to the user so anyways I'm gonna leave the video here and I was a bit shorter but this is kind of all we need to accomplish today in terms of just getting some audio from the user as well as you know printing certain things out I welcome you guys to continue to mess around with this make this as complicated as you want and in the next few days we're going to be adding some actually useful features that aren't just you know text responding back to the user as always if you guys enjoyed make sure you leave a like and subscribe to the channel and leave any comments for future things you'd like to see\n"