Hotword detection with Python

25 Oct, 2021
Most of us have heard about Alexa, Ok google or hey Siri and may have thought of creating your own Virtual Personal Assistant with your favorite name like, Hey Thanos!. So here’s the easiest way to do it without getting your hands dirty.Requirements:  
Linux pc with working microphones (I have tested on Arch Linux).Install python3, pyaudio, sox(and also swig for Arch Linux) with your package manager.:  
Linux pc with working microphones (I have tested on Arch Linux).
Install python3, pyaudio, sox(and also swig for Arch Linux) with your package manager.:  
sudo apt-get update
sudo apt-get install python3 python3-pip
sudo apt-get install python-pyaudio python3-pyaudio sox
Get snowboy packages from here. This file works with all Linux based OS. Snowboy also supports all versions of Raspberry Pi (1, 2, 3 and Zero). Supported OS is Raspbian 8.0.Now when you extracted with a folder named usr.Now navigate to usr/lib/python3.7/site-packages.Copy both folder(snowboy and snowboy-1.2.0b1-py3.7.egg-info) to /usr/lib/python3.7/site-packages either by using file manager or cp -r command.  
Get snowboy packages from here. This file works with all Linux based OS. Snowboy also supports all versions of Raspberry Pi (1, 2, 3 and Zero). Supported OS is Raspbian 8.0.
Now when you extracted with a folder named usr.Now navigate to usr/lib/python3.7/site-packages.
Copy both folder(snowboy and snowboy-1.2.0b1-py3.7.egg-info) to /usr/lib/python3.7/site-packages either by using file manager or cp -r command.  
cd Downloads
tar -xf python-snowboy-1.3.0-1-x86_64.pkg.tar.xz
cd usr/lib/python3.7/site-packages
sudo cp -r snowboy /usr/lib/python3.7/site-packages
sudo cp -r snowboy-1.2.0b1-py3.7.egg-info
Now you can check if snowboy works or or not. Fire up your terminal and type Python to get the python shell.  
Python3
from snowboy import snowboydecode
If this does not throw import error then you are ready to go further.Now go to snowboy website and do login. Once you login in, you will find create Hotword option and do further things as per instructions.once whole process is completed download the yourhotword.pmdl file which is generated.Copy the hotword.pmdl file to directory where you are going to create program.  
Python3
from snowboy import snowboydecoderdef detected_callback():    print ("hotword detected")     # do your task here or call other program.detector = snowboydecoder.HotwordDetector("hotword.pmdl",                       sensitivity = 0.5, audio_gain = 1) detector.start(detected_callback)
Output:  
Voice Input :  hey thanos
Output:
INFO:snowboy:Keyword 1 detected at time: 2019-05-09 21:55:16
hotword detected
This code will keep on running until you interrupt it with Ctrl+C or close terminal. As this code works offline (and does not connect to internet) so it does not stream your voice, so there is no risk of privacy. The hotword.pmdl file contains the model of your voice only. So, the above program will work with your voice only.You can implement this program to another program to and carry out tasks controlled by voice. 
prachisoda1234
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
Introduction To PYTHON
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list