Skip to content

Commit

Permalink
Place dependency imports inside class implementors
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jul 10, 2023
1 parent 4114c56 commit 349a097
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions programs/speechRecognition/speechRecognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
import yarp

from abc import ABC, abstractmethod
from pocketsphinx import Endpointer, Decoder
from vosk import Model, KaldiRecognizer

class ResponderFactory(ABC):
@abstractmethod
Expand Down Expand Up @@ -114,6 +112,7 @@ def __init__(self, stream, device, dictionary, language, rf):
raise Exception('Unable to load dictionary')

def _setDictionaryInternal(self, dictionary, language):
from pocketsphinx import Endpointer, Decoder
print('Setting dictionary to %s (language: %s)' % (dictionary, language))

lm = self.rf.findFileByName('dictionary/%s-%s.lm' % (dictionary, language))
Expand Down Expand Up @@ -160,14 +159,12 @@ class VoskSpeechRecognitionResponder(SpeechRecognitionResponder):
def __init__(self, stream, device, model):
super().__init__(stream, device)

if model is None:
self.model = Model(lang='en-us')
else:
self.model = Model(model_name='vosk-model-' + model)

self.rec = KaldiRecognizer(self.model, self.sample_rate)
if not self._setDictionaryInternal(model, None):
raise Exception('Unable to load dictionary')

def _setDictionaryInternal(self, dictionary, language):
from vosk import Model, KaldiRecognizer

try:
if dictionary is not None and str(dictionary):
print('Setting dictionary to %s' % dictionary)
Expand Down Expand Up @@ -263,7 +260,7 @@ def int_or_str(text):
try:
q = queue.Queue()

with sd.RawInputStream(blocksize=8000, #int(2880 / 2), #FIXME: hardcoded for pocketpshinx, vosk used to have 8000 here
with sd.RawInputStream(blocksize=int(2880 / 2), # FIXME: hardcoded for pocketpshinx, vosk used to have 8000 here
device=args.device,
dtype='int16',
channels=1,
Expand Down

0 comments on commit 349a097

Please sign in to comment.