Python Voice Assistant Tutorial #1 - Playing Sound with gTTS (Google Text to Speech)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to a brand new tutorial series on a Python voice assistant now what we're gonna be doing is essentially creating a virtual slash voice assistant using Python the Google text-to-speech module and some other modules in Python like speech recognition and play sound now what we're going to be doing is aiming to you know ease our life a little bit by having this virtual assistant that we can speak to through our computer's microphone and then have some kind of output either on the computer screen or from the speaker's itself so anyways that is kind of how this is gonna work what I'm gonna be doing for this tutorial series is breaking everything into parts where each part is where I'm gonna implement a new feature of the virtual assistant I have a few ideas for things I'd like to do but if there's anything you want to see as a part of this virtual assistant like a feature or something that it can do leave a comment down below right now and let me know and whatever ones are liked the most or just that I like I will try to implement into the series as we can do a ton of stuff within this series and different things I'm thinking right now that we can have some kind of web scraping so it can you know you can ask you to question it can Google something find the answer I'm thinking we can get it to do some kind of automation for us like maybe we want to track expenses or something and we can tell it you know like oh I bought this today I logged in an Excel spreadsheet I don't know is it a few small ideas I have but let me know so today's what we're gonna be doing today is just getting some audio coming out of the computer so pretty much figuring out how to use Google text-to-speech as well as just installing some packages that we're gonna need so the first thing we're gonna do is open up a command prompt window you can do that by typing CMD in your window search bar here or I mean if you're on Linux and I assume you know to do this then what we're gonna do is use pimp which is package installer for Python to install a few different packages now I assume most of you are familiar with pip but if you're not just go ahead in your command prompt try to type in and hit enter and see if this works if you get some output that looks like this if you don't then it probably means you have an installed pip correctly and I have a video I'm going to leave a card suit in the top right hand corner on how to fix that it says how to install PI game but it'll tell you how to fix that so anyways we're gonna start installing some packages so the first one we're gonna use is gonna be speech recognition I always have a trouble spelling this word I believe that's correct I'm installing that you can see I already have it installed but that's the first module so speech recognition the next one we are going to install is going to be Google text-to-speech which is G TTS and this capital on the T T and s and that is just Google text-to-speech will install that one and we need two more so the next one we're gonna have is play sound now what play sound is gonna do is literally just play sound from Python for us pretty straightforward and the last one we need is PI audio so pip install PI audio now I don't have any issue installing this but I know some people have said they've had issues installing this before so make sure you're a really quick fix on if you can't install PI audio this way how you can actually do it so what you're gonna need to do is go to this website here which will be at the link in the description and download the appropriate Weill file for pi audio so you can see here it shows all the different versions of this wheel file which is just essentially what you're installing when you install something with pip it just downloads this file for you you see this one is like Python 2 7 because it says CP 2 7 and then windows 32-bit so what you're gonna look for is your Python version and whatever bit version you're using so in this case I'm using Python 3 7 with a AMD 64 so like 64-bit so this would be the file I download this one right here I already have it downloaded but that's someone you would click on and if you want to check your Python version to do that just type Python or you might have to type Python 3 depending on how many pythons you have installed on your machine and you will see that it shows your Python version here and then whatever bit version you're using in these little square brackets so anyways that's how you download that file but now that you've downloaded that file you actually need to install it so to install it what you're gonna do is find where you downloaded it so you're probably gonna have it in downloads so you can see that I have one here actually I have two versions but there's one right here what I'm gonna do is open up a command prompt window here just by typing in this top excuse me so I'm just gonna open up a command prompt window here just by typing CMD in this top navigation bar and then what I can actually do is use pip and install this file so to do that I'm gonna do Hiep install and I'm gonna type PI and assuming there's something else that starts with py in my folder I can just hit tab and it's actually gonna bring up that file itself so pi audio like this and then what I can do is hit enter and that should install this for me now you can see I already have it installed but that's a way to get around this and it should say like successfully installed pi audio if for some reason your pip version of pi audio is not working which mine wasn't before anyways that is how we get everything installed and now it's actually time to start writing some code so I'm gonna close out all of this and right now you can see that I am in sub line text this is what I'm gonna be using for the majority of this tutorial series now if you want to download sub line text just look it up it's pretty straightforward but you can use anything you want so long as you know how to run the file and test things out okay so I've actually saved everything here into a folder I'm not sure if you guys can see up top here but I'm inside of a voice assistant folder I'd recommend you save your files inside of a folder because we're gonna have a few different things in there but anyways up to you let's go ahead and start coding so I'm just gonna import these modules that I need first of all so I'm going to import OS on an import time I'm gonna import play sound I'm gonna import speech underscore recognition as SR so standing for a speech recognition Oh what is it doing that for I want SR okay and now I'm going to import G TTS and I actually believe I need to import something from an ass or I'm gonna do from G TT s imports G TTS and that's a capital on the T TNS okay so now that we have that we have all our imports done and we can actually start writing some code so the first thing I'm going to do is just make it so that we can actually give some text to the computer and have it to say it to us now this seems pretty straightforward but sometimes you ran into some issues so anyways just try to follow along here I'm going to create a function that says speak and I'm gonna have a parameter called text and what this is gonna do is just take this text and speak it like say it out of our computer speakers so to do this we're gonna say TTS is equal to G TTS dots are not dots are just brackets I'm gonna say text equals text Lang equals en and what this is gonna do is just pretty much transform this text into an audio file with the language of English that's what Yen stands for now what we're gonna do is actually save this audio file and then play that audio file it's kind of weird how you have to save the file first but that's just the way that I found works best that's what I mean do I don't say file name is equal to in this case I'll just say voice dot mp3 it's gonna save as mp3 file I'm gonna say TTS dot save file name and what this is gonna do is save the file voiced on mp3 which is gonna contain the Google Voice saying whatever text it is we passed here to an mp3 file that's in the same folder as wherever this Python file is then what I'm gonna do is do play sound dot play sound and simply load in this file name and it will play this mp3 file so we can actually hear what it says um now that is pretty much it for actually making the speakers you know like say something from Python now all we don't need to do if we want to actually play this is just do speak and then put whatever we want in fear so you like hello Tim as the text and if I run this now we should see that we're gonna hear this come out so I mean we'll check this is the first try and see if we get it hello Tim and there we go so hopefully you guys were able to hear that but it says hello Tim there is a little bit of latency on it because what it needs to do is generate this file which takes a second save the file and then load it back in and play the sound um but anyways that is how we get some sound out to our computer you guys feel free to mess around with this knowing this alone you can create some cool things with Python but anyways in the next video we're gonna be doing is actually getting sound from the users of them speaking into the microphone recognizing what they're saying transform you into text and then doing something with that anyways if you guys enjoyed make sure you leave a like and make sure to leave some comment ideas down below on what you want to see from this series
Info
Channel: Tech With Tim
Views: 188,741
Rating: undefined out of 5
Keywords: tech with tim, text to speech, text to speech python, voice assistant python, voice assistant project, python voice assistant tutorial, python voice assistant, python voice assistant code, python virtual assistant, python virtual assistant tutorial
Id: -AzGZ_CHzJk
Channel Id: undefined
Length: 8min 46sec (526 seconds)
Published: Sat Aug 31 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.