4 from speech_recognition
import AudioData
7 from vosk
import Model, KaldiRecognizer
8 except ImportError
as e:
9 bold_red =
"\x1b[31;1m" 11 print(bold_red + str(e)+
'\nvsok is not installed try "pip install vosk"' + reset)
19 assert isinstance(audio_data, AudioData),
"Data must be audio data" 21 if not hasattr(self,
'vosk_model'):
22 if model_path
is None:
23 PKG =
'ros_speech_recognition' 25 data_path = osp.join(rp.get_path(PKG),
'trained_data')
26 if language ==
'en-US':
27 model_path = osp.join(data_path,
'vosk-model-small-en-us-0.15')
28 elif language ==
'ja':
29 model_path = osp.join(data_path,
'vosk-model-small-ja-0.22')
31 rospy.logerr(
"Unsupported language: {0}.\n Please download the model from https://alphacephei.com/vosk/models and specify its path as 'vosk_model_path'.".format(language))
33 rospy.loginfo(
"Loading model from {}".format(model_path))
34 self.vosk_model = Model(model_path)
35 rec = KaldiRecognizer(self.vosk_model, 16000);
37 rec.AcceptWaveform(audio_data.get_raw_data(convert_rate=16000, convert_width=2));
38 finalRecognition = rec.FinalResult()
39 text = json.loads(finalRecognition)[
'text']
42 RecognizerEx.recognize_vosk = recognize_vosk
def recognize_vosk(self, audio_data, model_path=None, language='en-US')