Day 18: Hotword detection with Python
Dilek Karasoy
Posted on January 25, 2023
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
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)
Initialization with Built-in Hotwords
porcupine = pvporcupine.create(
access_key=access_key,
keywords=[keyword_one, keyword_two])
Initialization with Custom Hotwords
porcupine = pvporcupine.create(
access_key=access_key,
keyword_paths=[keyword_paths_one, keyword_path_two])
Processing
keyword_index = porcupine.process(audio_frame)
if keyword_index >= 0:
# Logic to handle keyword detection events
Cleanup
porcupine.delete()
A Working Example
Install PvRecorder Python SDK using PIP:
pip3 install pvrecorder
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()
💖 💪 🙅 🚩
Dilek Karasoy
Posted on January 25, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.