rhino_demo_mic.py
Go to the documentation of this file.
1 #
2 # Copyright 2018-2021 Picovoice Inc.
3 #
4 # You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5 # file accompanying this source.
6 #
7 # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 # specific language governing permissions and limitations under the License.
10 #
11 
12 import argparse
13 import struct
14 import wave
15 from threading import Thread
16 
17 import pvrhino
18 from pvrecorder import PvRecorder
19 
20 
21 class RhinoDemo(Thread):
22  """
23  Microphone Demo for Rhino Speech-to-Intent engine. It creates an input audio stream from a microphone, monitors
24  it, and extracts the intent from the speech command. It optionally saves the recorded audio into a file for further
25  debugging.
26  """
27 
28  def __init__(self, access_key, library_path, model_path, context_path, require_endpoint, audio_device_index=None,
29  output_path=None):
30  """
31  Constructor.
32 
33  :param access_key: AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).
34  :param library_path: Absolute path to Rhino's dynamic library.
35  :param model_path: Absolute path to file containing model parameters.
36  :param context_path: Absolute path to file containing context model (file with `.rhn` extension). A context
37  represents the set of expressions (spoken commands), intents, and intent arguments (slots) within a domain of
38  interest.
39  :param require_endpoint If set to `False`, Rhino does not require an endpoint (chunk of silence) before
40  finishing inference.
41  :param audio_device_index: Optional argument. If provided, audio is recorded from this input device. Otherwise,
42  the default audio input device is used.
43  :param output_path: If provided recorded audio will be stored in this location at the end of the run.
44  """
45 
46  super(RhinoDemo, self).__init__()
47 
48  self._access_key = access_key
49  self._library_path = library_path
50  self._model_path = model_path
51  self._context_path = context_path
52  self._require_endpoint = require_endpoint
53  self._audio_device_index = audio_device_index
54 
55  self._output_path = output_path
56 
57  def run(self):
58  """
59  Creates an input audio stream, instantiates an instance of Rhino object, and infers the intent from spoken
60  commands.
61  """
62 
63  rhino = None
64  recorder = None
65  wav_file = None
66 
67  try:
68  rhino = pvrhino.create(
69  access_key=self._access_key,
70  library_path=self._library_path,
71  model_path=self._model_path,
72  context_path=self._context_path,
73  require_endpoint=self._require_endpoint)
74 
75  recorder = PvRecorder(device_index=self._audio_device_index, frame_length=rhino.frame_length)
76  recorder.start()
77 
78  if self._output_path is not None:
79  wav_file = wave.open(self._output_path, "w")
80  wav_file.setparams((1, 2, 16000, 512, "NONE", "NONE"))
81 
82  print(rhino.context_info)
83  print()
84 
85  print(f"Using device: {recorder.selected_device}")
86  print("Listening...")
87  print()
88 
89  while True:
90  pcm = recorder.read()
91 
92  if wav_file is not None:
93  wav_file.writeframes(struct.pack("h" * len(pcm), *pcm))
94 
95  is_finalized = rhino.process(pcm)
96  if is_finalized:
97  inference = rhino.get_inference()
98  if inference.is_understood:
99  print('{')
100  print(" intent : '%s'" % inference.intent)
101  print(' slots : {')
102  for slot, value in inference.slots.items():
103  print(" %s : '%s'" % (slot, value))
104  print(' }')
105  print('}\n')
106  else:
107  print("Didn't understand the command.\n")
108  except pvrhino.RhinoInvalidArgumentError as e:
109  print("One or more arguments provided to Rhino is invalid: {\n" +
110  f"\t{self._access_key=}\n" +
111  f"\t{self._library_path=}\n" +
112  f"\t{self._model_path=}\n" +
113  f"\t{self._context_path=}\n" +
114  f"\t{self._require_endpoint=}\n" +
115  "}")
116  print(f"If all other arguments seem valid, ensure that '{self._access_key}' is a valid AccessKey")
117  raise e
118  except pvrhino.RhinoActivationError as e:
119  print("AccessKey activation error")
120  raise e
121  except pvrhino.RhinoActivationLimitError as e:
122  print(f"AccessKey '{self._access_key}' has reached it's temporary device limit")
123  raise e
124  except pvrhino.RhinoActivationRefusedError as e:
125  print(f"AccessKey '{self._access_key}' refused")
126  raise e
127  except pvrhino.RhinoActivationThrottledError as e:
128  print(f"AccessKey '{self._access_key}' has been throttled")
129  raise e
130  except pvrhino.RhinoError as e:
131  print(f"Failed to initialize Rhino")
132  raise e
133  except KeyboardInterrupt:
134  print('Stopping ...')
135 
136  finally:
137  if recorder is not None:
138  recorder.delete()
139 
140  if rhino is not None:
141  rhino.delete()
142 
143  if wav_file is not None:
144  wav_file.close()
145 
146  @classmethod
148  devices = PvRecorder.get_audio_devices()
149 
150  for i in range(len(devices)):
151  print(f'index: {i}, device name: {devices[i]}')
152 
153 
154 def main():
155  parser = argparse.ArgumentParser()
156 
157  parser.add_argument('--access_key',
158  help='AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)',
159  required=True)
160 
161  parser.add_argument('--context_path', help="Absolute path to context file.", required=True)
162 
163  parser.add_argument('--library_path', help="Absolute path to dynamic library.", default=pvrhino.LIBRARY_PATH)
164 
165  parser.add_argument(
166  '--model_path',
167  help="Absolute path to the file containing model parameters.",
168  default=pvrhino.MODEL_PATH)
169 
170  parser.add_argument(
171  '--sensitivity',
172  help="Inference sensitivity. It should be a number within [0, 1]. A higher sensitivity value results in " +
173  "fewer misses at the cost of (potentially) increasing the erroneous inference rate.",
174  type=float,
175  default=0.5)
176 
177  parser.add_argument(
178  '--require_endpoint',
179  help="If set to `False`, Rhino does not require an endpoint (chunk of silence) before finishing inference.",
180  default='True',
181  choices=['True', 'False'])
182 
183  parser.add_argument('--audio_device_index', help='Index of input audio device.', type=int, default=-1)
184 
185  parser.add_argument('--output_path', help='Absolute path to recorded audio for debugging.', default=None)
186 
187  parser.add_argument('--show_audio_devices', action='store_true')
188 
189  args = parser.parse_args()
190 
191  if args.require_endpoint.lower() == 'false':
192  require_endpoint = False
193  else:
194  require_endpoint = True
195 
196  if args.show_audio_devices:
197  RhinoDemo.show_audio_devices()
198  else:
199  if not args.context_path:
200  raise ValueError('Missing path to context file')
201 
202  RhinoDemo(
203  access_key=args.access_key,
204  library_path=args.library_path,
205  model_path=args.model_path,
206  context_path=args.context_path,
207  require_endpoint=require_endpoint,
208  audio_device_index=args.audio_device_index,
209  output_path=args.output_path).run()
210 
211 
212 if __name__ == '__main__':
213  main()
rhino_demo_mic.RhinoDemo._access_key
_access_key
Definition: rhino_demo_mic.py:47
rhino_demo_mic.RhinoDemo.__init__
def __init__(self, access_key, library_path, model_path, context_path, require_endpoint, audio_device_index=None, output_path=None)
Definition: rhino_demo_mic.py:28
rhino_demo_mic.RhinoDemo._output_path
_output_path
Definition: rhino_demo_mic.py:54
rhino_demo_mic.RhinoDemo.run
def run(self)
Definition: rhino_demo_mic.py:57
rhino_demo_mic.RhinoDemo.show_audio_devices
def show_audio_devices(cls)
Definition: rhino_demo_mic.py:147
rhino_demo_mic.RhinoDemo
Definition: rhino_demo_mic.py:21
rhino_demo_mic.main
def main()
Definition: rhino_demo_mic.py:154
main
Definition: main.py:1
rhino_demo_mic.RhinoDemo._library_path
_library_path
Definition: rhino_demo_mic.py:48
rhino_demo_mic.RhinoDemo._context_path
_context_path
Definition: rhino_demo_mic.py:50
rhino_demo_mic.RhinoDemo._audio_device_index
_audio_device_index
Definition: rhino_demo_mic.py:52
rhino_demo_mic.RhinoDemo._require_endpoint
_require_endpoint
Definition: rhino_demo_mic.py:51
rhino_demo_mic.RhinoDemo._model_path
_model_path
Definition: rhino_demo_mic.py:49


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50