Speech recognition can be very helpful in your daily activities like you can switch on and off your laptops, control your T.V and A.C., and handle other home appliances. In this blog, we will learn a fun activity to play a snake game using voice control. By learning this method you can apply it on other real life applications.
To perform speech recognition, you need to train a model. And training a model requires a large amount of data which requires a lot of time. To save this time I was searching for a pre-trained model. There are some open source
To play a snake game, You first need to develop a snake game. But don’t worry you need not to develop it from scratch, you can clone this repository. And if you want to know algorithm behind this code you can follow this blog.
Now you have a snake game, the next thing is how to use speech recognition to play this game. First, clone the Porcupine repository into your system. It has some pre-trained wake words define in it. You can also use your own wake words. For this problem, I have used four wake words “go left”, “go right”, “go down” and “snake up”.
Here are the steps to play snake game using voice control:
- First go to Porcupine directory that you have cloned.
- Then go to tools -> optimizer -> System(windows or linux or mac) -> os type(64 ar 32 bit)
- Then use pv_porcupine_optimizer.exe file to create wake word files. To do this you need following command
pv_porcupine_optimizer.exe -r D:/Porcupine/resources/ -w "wake word" -o D:/Porcupine -p windows
- Here -r corresponds to resource directory which you can find inside Porcupine directory, -w corresponds to your wake word that you can choose, -o corresponds to the output directory of your wake word and -p corresponds to your platform(windows, linux or mac)
- To generate four different wake words, I have run the above command four times.
- Now you have created your wake words, the next thing is to integrate it with your snake game python code.
- Inside Porcupine folder go to binding -> python. There is a file named porcupine.py
- Open porcupine. py file and append following code at the last of it. You will also be needed to install
pyaudioand pyautoguiusing pip.
import pyautogui # to press a button to play the game
# below are the four wake word's path that you have generated earlier
key1 = 'go_down_windows.ppn'
key2 = 'snake_up_windows.ppn'
key3 = 'go_right_windows.ppn'
key4 = 'go_left_windows.ppn'
# this is the library path that you can fnd inside Porcupine -> lib -> system(windows or linux or mac) -> os type( 64 or 32 bit)
library_path = '/Porcupine/lib/windows/amd64/libpv_porcupine.dll'
# this is model file path can be find inside Porcupine -> lib -> common
model_file_path = '/Porcupine/lib/common/porcupine_params.pv'
keyword_file_paths = [key1, key2, key3, key4]
sensitivities = [0.5,0.5,0.5,0.5]
handle = Porcupine(library_path, model_file_path, keyword_file_paths=keyword_file_paths, sensitivities=sensitivities)
pa = pyaudio.PyAudio()
audio_stream = pa.open(rate=handle.sample_rate,channels=1,format=pyaudio.paInt16,input=True,frames_per_buffer=handle.frame_length,input_device_index=None)
pcm = audio_stream.read(handle.frame_length)
pcm = struct.unpack_from("h" * handle.frame_length, pcm)
pcm = get_next_audio_frame()
keyword_index = handle.process(pcm)
- Now run both porcupine.py file and your snake game code to play it with your voice control.
Now you have got an idea to use real time speech recognition. Hope you can find some real life applications to apply it.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.