DEV Community

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

Posted on

1

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay