Detecting hot words with Python

Python Methods and Functions

Requirements :

  1. Linux PC with working microphones (I tested on Arch Linux).
  2. 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 
  3. Get snowman packages from here . This file works with all Linux based OS. 
    Snowboy also supports all Raspberry Pi versions (1, 2, 3 and Zero). Supported OS — Raspbian 8.0.
  4. Now that you have unpacked the folder named usr.Now, navigate to usr / lib / python3.7 / site-packages.
  5. Copy both folders (snowboy and snowboy -1.2.0b1-py3.7.egg-info) in /usr/lib/python3.7/site-packages either with a file manager or with cp -r .
     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 the snowman is working or not. Fire up your terminal and fire up Python to get the Python wrapper.

from snowboy import snowboydecode

If that doesn't throw an import error, then you're ready to go ahead.

Now go to Snowboy site and log in. Once you are logged in, you will find the option to create a Hotword and follow the instructions. Once the whole process is complete, download the yourhotword.pmdl file yourhotword.pmdl .

Copy the hotword.pmdl file to the directory where you are going to create the program.

from snowboy import snowboydecoder

def detected_callback ( ):

print ( "hotword detected" )

  

# do your task here or call another program.

detector = snowboydecoder.HotwordDetector ( "hotword.pmdl" ,

sensitivity = 0.5 , audio_gain = 1 )

  
detector.start (detected_callback)

Exit:

  Voice Input:  hey thanos  Output:  INFO: snowboy: Keyword 1 detected at time: 2019-05-09 21:55:16 hotword detected 

This the code will run until you either interrupt it by pressing Ctrl + C or close the terminal. Since this code works offline (and does not connect to the internet), it does not transmit your voice, so there is no privacy risk. The hotword.pmdl file contains only your voice model. So, the above program will only work with your voice.

You can embed this program in another program and perform voice controlled tasks.





Get Solution for free from DataCamp guru