Wake Word Detection on the Raspberry Pi with Porcupine?

Wake word detection, also known as keyword spotting, allows devices like smart speakers to “wake up” when they hear a specific word or phrase. This enables hands-free voice control and is an essential feature for voice assistants like Alexa and Google Assistant.

Wake Word Detection on the Raspberry Pi with Porcupine?

In this guide, we will walk through how to set up wake word detection on a Raspberry Pi using Porcupine, an open-source keyword spotting engine. Porcupine is optimized to use very little resources, making it perfect for single-board computers like the Pi.

Prerequisites

To follow this guide, you will need:

  • A Raspberry Pi with Raspbian OS installed. Any model will work, but the Pi 3B+ or Pi 4 are recommended.
  • A USB microphone or headset with a microphone. The mic must be compatible with the Pi.
  • Speakers or headphones (optional but recommended).
  • Internet connection for initial setup.

Porcupine is designed to run offline once installed, so internet is only needed for the setup steps.

Installing the Prerequisites

Before installing Porcupine, we need to ensure some prerequisites are installed on the Raspberry Pi.

Update the System Packages

Run the following commands to update the package manager and install any available updates:

sudo apt-get update

sudo apt-get upgrade -y

This keeps the system up-to-date and avoids any potential issues with missing dependencies.

Install Python 3 and Pip

Porcupine requires Python 3.5 or higher. Run these commands to install Python 3 and pip:

sudo apt-get install python3 python3-pip -y

Pip allows us to easily install Python packages.

Install Port Audio

Porcupine uses Port Audio for audio I/O. We need to install it with:

sudo apt-get install portaudio19-dev -y

Install Virtualenv (Optional)

It’s recommended to install Porcupine in a Python virtual environment to avoid conflicting with system packages. Run this to install virtualenv:

sudo pip3 install virtualenv

We’ll create the virtual environment in the next section.

Installing Porcupine

Now we’re ready to install Porcupine.

Create Virtual Environment

If you installed virtualenv above, create a virtual environment for Porcupine:

python3 -m venv porcupine

source porcupine/bin/activate

This activates the virtual environment. You will need to re-activate it every time you reopen the terminal.

Install Porcupine Package

Use pip to install Porcupine in the virtual environment (or system-wide without virtualenv):

pip install pvporcupine

This will download and install Porcupine along with its requirements like NumPy.

Download Model File

We need to download Porcupine’s pretrained model to use it for wake word detection. Pick one of the model files for your desired wake word from Picovoice’s GitHub:

  • Porcupine.ppn – “Porcupine” wake word
  • Alexa.ppn – “Alexa” wake word
  • Computer.ppn – “Computer” wake word

Run wget to download the file, replacing the URL with your desired model:

wget https://github.com/Picovoice/porcupine/raw/master/resources/keyword_files/Computer/Computer.ppn

This will download the model to your working directory.

Porcupine is now installed and ready to use!

Creating the Wake Word Detection Script

With Porcupine set up, we can now write a Python script to listen for our wake word.

Importing the Porcupine Library

Start by importing Porcupine’s library:

python

from pvporcupine import Porcupine

Initializing Porcupine

Next, create a Porcupine object, passing the path to the model file:

python

porcupine = Porcupine(keyword_file_path=‘Computer.ppn’)

Replace ‘Computer.ppn’ with your downloaded model file.

Setting up the Microphone

Use PyAudio to open a stream from the default microphone:

python

import pyaudio

pa = pyaudio.PyAudio()

audio_stream = pa.open(

        rate=porcupine.sample_rate,

        channels=1,

        format=pyaudio.paInt16,

        input=True,

        frames_per_buffer=porcupine.frame_length)

This sets up an audio stream at the sample rate expected by Porcupine (16000 Hz by default).

Listening for Wake Word

With the microphone stream initialized, we can start listening for the wake word in a loop:

python

while True:

    pcm = audio_stream.read(porcupine.frame_length)

    pcm = struct.unpack_from(“h” * porcupine.frame_length, pcm)

    keyword_index = porcupine.process(pcm)

    if keyword_index >= 0:

         Wake word detected!

        print(“Wake word detected!”)

porcupine.process() analyzes each audio frame and returns the index of the detected wake word, or -1 if nothing is detected.

When it returns a non-negative index, our wake word was spoken! We can trigger our voice assistant or other logic here.

Shutting Down

Finally we close the audio stream and PyAudio when finished:

python

audio_stream.close()

pa.terminate()

The full script is available below. Save it as wakeword_detection.py or another name.

python

from pvporcupine import Porcupine

import pyaudio

import struct

 Porcupine setup

porcupine = Porcupine(keyword_file_path=‘Computer.ppn’)

 Microphone stream config 

pa = pyaudio.PyAudio()

audio_stream = pa.open(

        rate=porcupine.sample_rate,

        channels=1,

        format=pyaudio.paInt16,

        input=True,

        frames_per_buffer=porcupine.frame_length)

while True:

    pcm = audio_stream.read(porcupine.frame_length)

    pcm = struct.unpack_from(“h” * porcupine.frame_length, pcm)

    keyword_index = porcupine.process(pcm)

    if keyword_index >= 0:

        print(“Wake word detected!”)

audio_stream.close()

pa.terminate()

Running the Wake Word Detection

With the script complete, we can test out wake word detection.

Activate Your Virtual Environment

If using virtualenv, don’t forget to activate the porcupine environment:

source porcupine/bin/activate

Run the Script

Run the script with:

python wakeword_detection.py

Speak your wake word into the microphone when prompted. You should see “Wake word detected!” printed when it hears the keyword.

Try adjusting the microphone level and position to improve detection accuracy. You can also fine-tune the sensitivity in Porcupine’s settings.

Integrating with Other Programs

Once working, you can have your script trigger other logic like launching a voice assistant when the wake word is detected.

For example, use Popen to launch a bash script:

python

from subprocess import Popen

if keyword_index >= 0:

    print(“Wake word detected!”)

    Popen(“/path/to/script.sh”)

The possibilities are endless!

Optimizing Accuracy

Porcupine provides a few ways to optimize wake word detection accuracy for your specific environment.

Sensitivity Setting

The detection sensitivity can be set when creating the Porcupine object, from 0 to 1. Higher values increase sensitivity but also increase false positives.

python

porcupine = Porcupine(keyword_file_path=‘Computer.ppn’, sensitivity=0.5)

Tune this value for your use case. The default is 0.5.

Keyword Threshold

You can require a certain number of positive detections before triggering wake word logic by using a keyword threshold:

python

keyword_threshold = 3

detections = 0

 In main loop

if keyword_index >= 0:

    detections += 1

    if detections >= keyword_threshold:

         Trigger logic

        detections = 0

This prevents sporadic false positives from accidentally waking your system.

Audio Processing

You can integrate Porcupine with more advanced audio preprocessing like noise suppression to improve accuracy. Tools like PyAudioProcessing or SpeechRecognition can help reduce environmental noise.

Conclusion

Implementing wake word detection on the Raspberry Pi with Porcupine is straightforward and enables many exciting hands-free voice projects. Some possibilities are:

  • Voice assistant – Launch Alexa, Siri, Google Assistant, or a custom assistant.
  • Home automation – Control lights, thermostats, appliances.
  • Computer control – “Wake up” your PC.
  • Robotics – Enable voice commands for Raspberry Pi robots.
  • IoT voice control – For custom DIY IoT devices.

Porcupine provides an efficient keyword spotting engine optimized for single-board computers. With just a microphone and speaker, you can add offline voice control to almost any Pi project!

Key Takeaways:

  • Porcupine enables accurate wake word detection on the Raspberry Pi using minimal resources.
  • Models for “Hey Computer”, “Alexa”, and “Porcupine” wake words are included.
  • Detection sensitivity and keyword threshold can be tuned to optimize for your environment.
  • Audio preprocessing like noise cancellation can further improve accuracy.
  • Wake word detection enables hands-free voice control for Pi projects.

Frequently Asked Questions

Q: What models or wake words does Porcupine support?
A: Porcupine has pretrained models for “Porcupine”, “Alexa”, “Computer”, and other common wake words. You can also train custom models.

Q: How accurate is Porcupine’s wake word detection?
A: In optimal conditions it can reach over 90% accuracy. Accuracy depends on environment noise, microphone quality, sensitivity tuning, and audio preprocessing.

Q: Can I train Porcupine to detect custom wake words?
A: Yes, Porcupine provides tools to create custom wake word models. It requires training data of people speaking the phrase.

Q: Does Porcupine require an internet connection?
A: Porcupine can run fully offline once installed and the model file is downloaded. No internet is required after setup.

Q: What microphone should I use with Porcupine on Raspberry Pi?
A: Any USB microphone compatible with Raspberry Pi will work. Higher quality mics produce better accuracy. Mini lapel mics work well.

Q: How do I improve accuracy if Porcupine isn’t detecting my wake word reliably?
A: Try adjusting the sensitivity, increasing keyword threshold, improving microphone positioning, and using audio preprocessing to filter noise.

Q: Can I use Porcupine to detect multiple different wake words?
A: Yes, you can run multiple Porcupine instances in parallel listening for different keywords.

Q: Does Porcupine work with all Raspberry Pi models?
A: It can work on any Pi model, but Pi 3B+ or Pi 4 is recommended for best performance. Compute Module also works well.

Q: Can I use Porcupine on other single-board computers like Orange Pi?
A: Porcupine works on many Linux-based boards. Its low resource usage makes it ideal for these devices.

Q: How does Porcupine’s accuracy compare to commercial solutions like Alexa or Siri?
A: Commercial solutions use more sophisticated deep learning models and leverage cloud processing, so tend to be more accurate.

Q: Is Porcupine open-source?
A: Porcupine is open-source but the optimzed library uses a proprietary neural network model file for inference.

Q: Does Porcupine work on Windows/Mac or only Linux?
A: Officially Porcupine only supports Linux currently. Unofficial Windows/Mac support may work but isn’t guaranteed.

Q: Can I run Porcupine directly on an Arduino or other microcontroller?
A: Porcupine requires Python and more processing power so cannot run directly on a low-power microcontroller.

Q: Does Porcupine support wake word detection in languages other than English?
A: Porcupine has some support for Spanish, Italian, and German wake words. Limited support for other languages currently.

Q: Can I use Porcupine for speech recognition or conversation?
A: Porcupine is only designed for detecting a single specified wake word. For full speech recognition use tools like DeepSpeech.

Q: Does Porcupine integrate withHome Assistant? Node-RED? Other platforms?
A: Porcupine doesn’t directly integrate with other platforms, but can trigger any logic via Python integration scripts.

Q: Can I use Porcupine at the same time as voice assistants like Alexa?
A: Yes, Porcupine will run locally ignoring the assistant’s wake word. Enable manual activation if using the same wake word.

Q: How do I reduce false activations of the wake word?
A: Try adjusting sensitivity lower, increasing keyword threshold, improving microphone positioning, and noise cancellation.

Q: Is it possible to run Porcupine directly on a Pi Zero or other low-power Pis?
A: It may work on Pi Zero but accuracy will be lower due to slower processing. Pi 3B+ or Pi 4 recommended for best performance.

Q: Can Porcupine be used for always-listening voice control, or onlywake word?
A: Porcupine is solely a wake word engine. For always-listening voice control combine it with tools like DeepSpeech.

 

Leave a Comment