recognize_vosk.py
Go to the documentation of this file.
1 # file to override recognize_vosk ( https://github.com/Uberi/speech_recognition/blob/3.9.0/speech_recognition/__init__.py#L1711 )
2 # we need this to use vosk model anywhere
3 
4 from speech_recognition import AudioData
6 try:
7  from vosk import Model, KaldiRecognizer
8 except ImportError as e:
9  bold_red = "\x1b[31;1m"
10  reset = "\x1b[0m"
11  print(bold_red + str(e)+'\nvsok is not installed try "pip install vosk"' + reset)
12 import json
13 import os.path as osp
14 import rospkg
15 import rospy
16 
17 def recognize_vosk(self, audio_data, model_path=None, language='en-US'):
18 
19  assert isinstance(audio_data, AudioData), "Data must be audio data"
20 
21  if not hasattr(self, 'vosk_model'):
22  if model_path is None:
23  PKG = 'ros_speech_recognition'
24  rp = rospkg.RosPack()
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')
30  else:
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))
32  exit (1)
33  rospy.loginfo("Loading model from {}".format(model_path))
34  self.vosk_model = Model(model_path)
35  rec = KaldiRecognizer(self.vosk_model, 16000);
36 
37  rec.AcceptWaveform(audio_data.get_raw_data(convert_rate=16000, convert_width=2));
38  finalRecognition = rec.FinalResult()
39  text = json.loads(finalRecognition)['text']
40  return text
41 
42 RecognizerEx.recognize_vosk = recognize_vosk
def recognize_vosk(self, audio_data, model_path=None, language='en-US')


ros_speech_recognition
Author(s): Yuki Furuta
autogenerated on Sat Jun 24 2023 02:40:38