DEV Community

Dilek Karasoy for Picovoice

Posted on

End-to-End Speech Recognition with Python

Let's start with why you should use Picovoice Python SDK when there are alternative libraries and in-depth tutorials on speech recognition with Python.

  1. Private - processes voice data on the device
  2. Cross-platform β€” Linux, macOS, Windows, Raspberry Pi, …
  3. Real-time - zero latency

-I do not need to say accurate I guess. I haven't seen any vendor claiming mediocre accuracy πŸ™ƒ

Now, let's get started!

1 β€” Install Picovoice

pip3 install picovoice
Enter fullscreen mode Exit fullscreen mode

2 β€” Create a Picovoice Instant
Picovoice SDK consists of Porcupine Wake Word, enabling custom hotwords and Rhino Speech-to-Intent, enabling custom voice commands. Jointly they enable hands-free experiences.
Porcupine, set an alarm for 1 hours and 13 seconds.
Porcupine detects the hotword "Porcupine", then Rhino captures the user’s intent and provides intent and intent details as seen below:

    is_understood: true,
    intent: setAlarm,
    slots: {
        hours: 1,
        seconds: 13
Enter fullscreen mode Exit fullscreen mode

To create a Picovoice instance we need Porcupine and Rhino models, paths to the models and callbacks for hotword detection and inference completion. For the simplicity, we'll use pre-trained Porcupine and Rhino models, however, you can train custom ones on the Picovoice Console: While exploring the Picovoice Console, grab your AccessKey, too! Signing up for Picovoice Console is free, no credit card required.

from picovoice import Picovoice
keyword_path = ...  # path to Porcupine wake word file (.PPN)
def wake_word_callback():
context_path = ...  # path to Rhino context file (.RHN)
def inference_callback(inference):
    if inference.is_understood:
        for k, v in inference.slots.items():
            print(f"{k} : {v}")

pv = Picovoice(
Enter fullscreen mode Exit fullscreen mode

Do not forget to replace model path and AccessKey placeholders.

3 β€” Process Audio with Picovoice
Pass frames of audio to the engine:

Enter fullscreen mode Exit fullscreen mode

4 β€” Read audio from the Microphone
Install [pvrecorder]( and read the audio:

from pvrecoder import PvRecoder
# `-1` is the default input audio device.
recorder = PvRecoder(device_index=-1)
Enter fullscreen mode Exit fullscreen mode

Read audio frames from the recorder and pass it to .process method:

pcm =
Enter fullscreen mode Exit fullscreen mode

5β€” Create a GUI with Tkinter
Tkinter is the standard GUI framework shipped with Python. Create a frame, add a label showing the remaining time to it, then launch:

window = tk.Tk()
time_label = tk.Label(window, text='00 : 00 : 00')

window.protocol('WM_DELETE_WINDOW', on_close)

Enter fullscreen mode Exit fullscreen mode

Some resources:
Source code for the tutorial
Original Medium Article
Picovoice SDK
Picovoice Console

Top comments (0)