![]() Because we have the fuzziness parameter set to 2, search_es('open spofity') will also return the same thing even though there is a misspelling.Įven though this is a small functionality, the key here is that this principle can be applied to a lot of different functionalities - Q&A, other commands, etc… Tie it all Together in a Voice Assistant Now try it out! search_es('open spotify') should return the command we will pass to os.system which is open /Applications/Spotify.app. import os d = '/Applications' records = apps = os.listdir(d) for app in apps: record = ) return res To do this, we can quickly create a list of dictionaries. So let’s first get a list of all our applications.īasically we’re gonna upload all our voice commands (something like “open spotify”) to elasticsearch and map them to the system commands (the actual programming command) we want to run. Obviously it’s not really that cool at all, but it works well as an example, and the general structure I’ll use can be applied to a much wider range of functionality. For an offhand easy idea my first instinct was to use it to open applications. ![]() Now we can decide how we wanna use the input. So now you can transcribe voice to text, make your computer speak and the world is your oyster, sick. Using the subprocess package, we can easily make our computer speak: import subprocess def say(text): subprocess.call() say("yo whatup dog") Opening Applications on your Computer There’s multiple ways to do this in Python, but the easiest is just to use the one that comes with macOS. For more on these classes and how that stuff works, refer to aforementioned post. This code should run until it hears something, then print it out what you said (or what it thinks you said). In less than 10 lines of code, this package should let us connect to our built in computer mic and transcribe our voice! import speech_recognition as sr r = sr.Recognizer() mic = sr.Microphone() with mic as source: r.adjust_for_ambient_noise(source) audio = r.listen(source) transcript = r.recognize_google(audio) print(transcript) Super convenient if you don’t feel like setting up an account for any of the other providers. ![]() One of these - the Google Web Speech API - supports a default API key that is hard-coded into the SpeechRecognition library.” The speech_recognition package is great because as stated in this aforementioned post - “The SpeechRecognition library acts as a wrapper for several popular speech APIs and is thus extremely flexible. pip install SpeechRecognition pip install pyaudio pip install elasticsearch Speech Recognition in Python We’ll also use the elasticsearch Python client to make things easier for us, I’m working in Python 3. The speech_recognition package we’ll use to translate speech to text, and pyaudio is necessary for connecting to your computer’s microphone. I’ll be using pip for python package management. Then in a terminal run: brew install portaudio brew install elasticsearch Homebrew is a great package manager for macOS and can be downloaded here. On Mac, install portaudio and elasticsearch with homebrew. The second is the same as my past post, and uses elasticsearch to do query-query matching and return a response. The first one is to get access to the microphone - credit to this post for a great intro to speech recognition in python. I’ll show two different voice activated capabilities in this blog post: There’s some other posts out there with pretty much the same general concept - but didn’t see anything much like this on TDS and figured what the hell □□ I can write better than that and mine will be more fun. This blog post should, however, give you a great place to start if you’ve ever wondered how to create your own voice assistant in python and love to screw around. Honestly, it took me longer to find/make the gifs for this post than it did to code it out. Like my last blog post on creating a chatbot using your own text messages, this isn’t going to be a fully developed idea and won’t get you too far in a production. Ironic Cortana Reference Since We’re on Mac - Source
0 Comments
Leave a Reply. |