Skip to content

add function to Recognizer to set alternative model path for VOSK model #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ zh-CN.zip
it-IT.zip
pocketsphinx-python/
examples/TEST.py
.idea
**/.DS_Store
**/.__pycache__
tmp/
tmp/*

15 changes: 12 additions & 3 deletions speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,15 @@ def __init__(self):
self.phrase_threshold = 0.3 # minimum seconds of speaking audio before we consider the speaking audio a phrase - values below this are ignored (for filtering out clicks and pops)
self.non_speaking_duration = 0.5 # seconds of non-speaking audio to keep on both sides of the recording

self.model_path = "model" # path to VOSK model

def add_model_path(self, model_path):
""" Set a path to load a speech recognition model. """

setattr(self, "model_path", model_path)

pass

def record(self, source, duration=None, offset=None):
"""
Records up to ``duration`` seconds of audio from ``source`` (an ``AudioSource`` instance) starting at ``offset`` (or at the beginning if not specified) into an ``AudioData`` instance, which it returns.
Expand Down Expand Up @@ -1393,12 +1402,12 @@ def recognize_vosk(self, audio_data, language='en'):
from vosk import Model, KaldiRecognizer

assert isinstance(audio_data, AudioData), "Data must be audio data"

if not hasattr(self, 'vosk_model'):
if not os.path.exists("model"):
if not os.path.exists(self.model_path):
return "Please download the model from https://github.com/alphacep/vosk-api/blob/master/doc/models.md and unpack as 'model' in the current folder."
exit (1)
self.vosk_model = Model("model")
self.vosk_model = Model(self.model_path)

rec = KaldiRecognizer(self.vosk_model, 16000);

Expand Down