rhino_demo_file.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 
16 import pvrhino
17 
18 
19 def read_file(file_name, sample_rate):
20  wav_file = wave.open(file_name, mode="rb")
21  channels = wav_file.getnchannels()
22  num_frames = wav_file.getnframes()
23 
24  if wav_file.getframerate() != sample_rate:
25  raise ValueError("Audio file should have a sample rate of %d. got %d" % (sample_rate, wav_file.getframerate()))
26 
27  samples = wav_file.readframes(num_frames)
28  wav_file.close()
29 
30  frames = struct.unpack('h' * num_frames * channels, samples)
31 
32  if channels == 2:
33  print("Picovoice processes single-channel audio but stereo file is provided. Processing left channel only.")
34 
35  return frames[::channels]
36 
37 
38 def main():
39  parser = argparse.ArgumentParser()
40 
41  parser.add_argument('--input_audio_path', help='Absolute path to input audio file.',
42  required=True)
43 
44  parser.add_argument('--access_key',
45  help='AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)',
46  required=True)
47 
48  parser.add_argument('--context_path', help="Absolute path to context file.", required=True)
49 
50  parser.add_argument('--library_path', help='Absolute path to dynamic library.', default=pvrhino.LIBRARY_PATH)
51 
52  parser.add_argument(
53  '--model_path',
54  help='Absolute path to the file containing model parameters.',
55  default=pvrhino.MODEL_PATH)
56 
57  parser.add_argument(
58  '--sensitivity',
59  help="Inference sensitivity. It should be a number within [0, 1]. A higher sensitivity value results in " +
60  "fewer misses at the cost of (potentially) increasing the erroneous inference rate.",
61  type=float,
62  default=0.5)
63 
64  parser.add_argument(
65  '--require_endpoint',
66  help="If set to `False`, Rhino does not require an endpoint (chunk of silence) before finishing inference.",
67  default='True',
68  choices=['True', 'False'])
69 
70  args = parser.parse_args()
71 
72  if args.require_endpoint.lower() == 'false':
73  require_endpoint = False
74  else:
75  require_endpoint = True
76 
77  try:
78  rhino = pvrhino.create(
79  access_key=args.access_key,
80  library_path=args.library_path,
81  model_path=args.model_path,
82  context_path=args.context_path,
83  sensitivity=args.sensitivity,
84  require_endpoint=require_endpoint)
85  except pvrhino.RhinoInvalidArgumentError as e:
86  print(f"One or more arguments provided to Rhino is invalid: {args}")
87  print(f"If all other arguments seem valid, ensure that '{args.access_key}' is a valid AccessKey")
88  raise e
89  except pvrhino.RhinoActivationError as e:
90  print("AccessKey activation error")
91  raise e
92  except pvrhino.RhinoActivationLimitError as e:
93  print(f"AccessKey '{args.access_key}' has reached it's temporary device limit")
94  raise e
95  except pvrhino.RhinoActivationRefusedError as e:
96  print(f"AccessKey '{args.access_key}' refused")
97  raise e
98  except pvrhino.RhinoActivationThrottledError as e:
99  print(f"AccessKey '{args.access_key}' has been throttled")
100  raise e
101  except pvrhino.RhinoError as e:
102  print(f"Failed to initialize Rhino")
103  raise e
104 
105  audio = read_file(args.input_audio_path, rhino.sample_rate)
106 
107  num_frames = len(audio) // rhino.frame_length
108  for i in range(num_frames):
109  frame = audio[i * rhino.frame_length:(i + 1) * rhino.frame_length]
110  is_finalized = rhino.process(frame)
111  if is_finalized:
112  inference = rhino.get_inference()
113  if inference.is_understood:
114  print('{')
115  print(" intent : '%s'" % inference.intent)
116  print(' slots : {')
117  for slot, value in inference.slots.items():
118  print(" %s : '%s'" % (slot, value))
119  print(' }')
120  print('}')
121  else:
122  print("Didn't understand the command.")
123  break
124 
125  rhino.delete()
126 
127 
128 if __name__ == '__main__':
129  main()
rhino_demo_file.read_file
def read_file(file_name, sample_rate)
Definition: rhino_demo_file.py:19
rhino_demo_file.main
def main()
Definition: rhino_demo_file.py:38
main
Definition: main.py:1


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