Skip to content
This repository was archived by the owner on May 6, 2022. It is now read-only.

Commit 718aac0

Browse files
authored
Add Spokestack Keyword Profile (#72)
This adds a profile for the KeywordRecognizer to allow users to easily use keyword models.
1 parent 75455b0 commit 718aac0

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

spokestack/profile/keyword.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Pipeline profile for pyaudio input, vad, keyword.
3+
"""
4+
from typing import Any, List
5+
6+
from spokestack.activation_timeout import ActivationTimeout
7+
from spokestack.agc.webrtc import AutomaticGainControl
8+
from spokestack.asr.keyword.tflite import KeywordRecognizer
9+
from spokestack.io.pyaudio import PyAudioInput
10+
from spokestack.nsx.webrtc import AutomaticNoiseSuppression
11+
from spokestack.pipeline import SpeechPipeline
12+
from spokestack.vad.webrtc import VoiceActivityDetector, VoiceActivityTrigger
13+
14+
15+
class SpokestackKeyword:
16+
"""Spokestack keyword profile."""
17+
18+
@staticmethod
19+
def create(
20+
classes: List[str],
21+
model_dir: str,
22+
sample_rate: int = 16000,
23+
frame_width: int = 20,
24+
**kwargs: Any
25+
) -> SpeechPipeline:
26+
"""Create a speech pipeline instance from profile.
27+
28+
Args:
29+
model_dir (str): Directory containing the tflite keyword models.
30+
classes: (List(str)): Classes for the keyword model to recognize
31+
sample_rate (int): sample rate of the audio (Hz).
32+
frame_width (int): width of the audio frame: 10, 20, or 30 (ms).
33+
34+
"""
35+
pipeline = SpeechPipeline(
36+
input_source=PyAudioInput(
37+
frame_width=frame_width, sample_rate=sample_rate, **kwargs
38+
),
39+
stages=[
40+
AutomaticGainControl(sample_rate=sample_rate, frame_width=frame_width),
41+
AutomaticNoiseSuppression(sample_rate=sample_rate),
42+
VoiceActivityDetector(
43+
sample_rate=sample_rate, frame_width=frame_width, **kwargs
44+
),
45+
VoiceActivityTrigger(),
46+
KeywordRecognizer(
47+
classes=classes,
48+
model_dir=model_dir,
49+
sample_rate=sample_rate,
50+
**kwargs,
51+
),
52+
ActivationTimeout(frame_width=frame_width, **kwargs),
53+
],
54+
)
55+
return pipeline

tests/profile/test_keyword.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
This module contains the tests for the keyword profile.
3+
"""
4+
from unittest import mock
5+
6+
from spokestack.profile.keyword import SpokestackKeyword
7+
8+
9+
@mock.patch("spokestack.profile.keyword.PyAudioInput")
10+
@mock.patch("spokestack.profile.keyword.KeywordRecognizer")
11+
@mock.patch("spokestack.profile.keyword.SpeechPipeline")
12+
def test_activate(*args):
13+
pipeline = SpokestackKeyword.create(
14+
classes=[
15+
"one",
16+
"two",
17+
"three",
18+
],
19+
model_dir="mock_model_dir",
20+
)
21+
pipeline.run()

0 commit comments

Comments
 (0)