DEV Community

Cover image for Day 18: Hotword detection with Python
Dilek Karasoy for Picovoice

Posted on

Day 18: Hotword detection with Python

Today we'll work with Porcupine Python SDK It runs on

  • Linux (x86_64),
  • macOS (x86_64 / arm64),
  • Windows (amd64),
  • Raspberry Pi (Zero, 2, 3, 4),
  • NVIDIA Jetson Nano,
  • BeagleBone

Install the SDK

pip3 install pvporcupine
Enter fullscreen mode Exit fullscreen mode

Grab your AccessKey
If you haven't already, sign up for the Picovoice Console to get your AccessKey for free.

Usage
Check the list of built-in hotword models such as Alexa, Hey Google, OK Google, Hey Siri, and Jarvis.

import pvporcupine
for keyword in pvporcupine.KEYWORDS:
    print(keyword)
Enter fullscreen mode Exit fullscreen mode

Initialization with Built-in Hotwords

porcupine = pvporcupine.create(
        access_key=access_key,
        keywords=[keyword_one, keyword_two])
Enter fullscreen mode Exit fullscreen mode

Initialization with Custom Hotwords

porcupine = pvporcupine.create(
        access_key=access_key,
        keyword_paths=[keyword_paths_one, keyword_path_two])
Enter fullscreen mode Exit fullscreen mode

Processing

keyword_index = porcupine.process(audio_frame)
if keyword_index >= 0:
    # Logic to handle keyword detection events
Enter fullscreen mode Exit fullscreen mode

Cleanup

porcupine.delete()
Enter fullscreen mode Exit fullscreen mode

A Working Example
Install PvRecorder Python SDK using PIP:

pip3 install pvrecorder
Enter fullscreen mode Exit fullscreen mode

Let's record audio from the default microphone on the device and process recorded audio with Porcupine:

import pvporcupine
from pvrecorder import PvRecorder
porcupine = pvporcupine.create(access_key=access_key, keywords=keywords)
recoder = PvRecorder(device_index=-1, frame_length=porcupine.frame_length)
try:
    recoder.start()
    while True:
        keyword_index = porcupine.process(recoder.read())
        if keyword_index >= 0:
            print(f"Detected {keywords[keyword_index]}")
except KeyboardInterrupt:
    recoder.stop()
finally:
    porcupine.delete()
    recoder.delete()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)