17 from rhino
import Rhino
26 return '%s_%s' % (s, language)
30 wav_file = wave.open(file_name, mode=
"rb")
31 channels = wav_file.getnchannels()
32 num_frames = wav_file.getnframes()
34 if wav_file.getframerate() != sample_rate:
36 "Audio file should have a sample rate of %d, got %d" % (sample_rate, wav_file.getframerate()))
38 samples = wav_file.readframes(num_frames)
41 frames = struct.unpack(
'h' * num_frames * channels, samples)
44 print(
"Picovoice processes single-channel audio but stereo file is provided. Processing left channel only.")
46 return frames[::channels]
50 system = platform.system()
54 if system ==
'Darwin':
55 return os.path.join(os.path.dirname(__file__), contexts_root,
'mac',
'%s_mac.rhn' % context)
56 elif system ==
'Linux':
57 if platform.machine() ==
'x86_64':
58 return os.path.join(os.path.dirname(__file__), contexts_root,
'linux',
'%s_linux.rhn' % context)
62 cpu_info = subprocess.check_output([
'cat',
'/proc/cpuinfo']).decode()
63 cpu_part_list = [x
for x
in cpu_info.split(
'\n')
if 'CPU part' in x]
64 cpu_part = cpu_part_list[0].split(
' ')[-1].lower()
65 except Exception
as error:
66 raise RuntimeError(
"Failed to identify the CPU with '%s'\nCPU info: %s" % (error, cpu_info))
68 if '0xb76' == cpu_part
or '0xc07' == cpu_part
or '0xd03' == cpu_part
or '0xd08' == cpu_part:
69 return os.path.join(os.path.dirname(__file__),
70 contexts_root,
'raspberry-pi',
'%s_raspberry-pi.rhn' % context)
71 elif '0xd07' == cpu_part:
72 return os.path.join(os.path.dirname(__file__),
73 contexts_root,
'jetson',
'%s_jetson.rhn' % context)
74 elif '0xc08' == cpu_part:
75 return os.path.join(os.path.dirname(__file__),
76 contexts_root,
'beaglebone',
'%s_beaglebone.rhn' % context)
78 raise NotImplementedError(
"Unsupported CPU: '%s'." % cpu_part)
79 elif system ==
'Windows':
80 return os.path.join(os.path.dirname(__file__), contexts_root,
'windows',
'%s_windows.rhn' % context)
82 raise ValueError(
"Unsupported system '%s'." % system)
87 model_path_subdir =
'%s.pv' % model_path_subdir
88 return os.path.join(os.path.dirname(__file__), relative, model_path_subdir)
94 _language_to_contexts = {
95 'en': [
'coffee_maker'],
96 'es': [
'iluminación_inteligente'],
101 for language
in _language_to_contexts:
102 cls.
rhinos[language] = dict()
103 for context
in _language_to_contexts[language]:
105 access_key=sys.argv[1],
113 if cls.
rhinos is not None:
114 for language
in cls.
rhinos:
115 for context
in cls.
rhinos[language]:
116 cls.
rhinos[language][context].delete()
118 def run_rhino(self, language, audio_file_name, context, is_whithin_context, intent=None, slots=None):
119 rhino = self.
rhinos[language][context]
123 os.path.join(os.path.dirname(__file__),
'../../resources/audio_samples/', audio_file_name),
127 for i
in range(len(audio) // rhino.frame_length):
128 frame = audio[i * rhino.frame_length:(i + 1) * rhino.frame_length]
129 is_finalized = rhino.process(frame)
133 self.assertTrue(is_finalized,
"Failed to finalize.")
135 inference = rhino.get_inference()
137 if is_whithin_context:
138 self.assertTrue(inference.is_understood,
"Couldn't understand.")
140 self.assertEqual(intent, inference.intent,
"Incorrect intent.")
142 self.assertEqual(slots, inference.slots,
"Incorrect slots.")
144 self.assertFalse(inference.is_understood,
"Shouldn't be able to understand.")
149 audio_file_name=
'test_within_context.wav',
150 context=
'coffee_maker',
151 is_whithin_context=
True,
152 intent=
'orderBeverage',
153 slots=dict(beverage=
'americano', numberOfShots=
'double shot', size=
'medium'))
158 audio_file_name=
'test_out_of_context.wav',
159 context=
'coffee_maker',
160 is_whithin_context=
False)
165 audio_file_name=
'test_within_context_es.wav',
166 context=
'iluminación_inteligente',
167 is_whithin_context=
True,
168 intent=
'changeColor',
169 slots=dict(location=
'habitación', color=
'rosado'))
174 audio_file_name=
'test_out_of_context_es.wav',
175 context=
'iluminación_inteligente',
176 is_whithin_context=
False)
181 audio_file_name=
'test_within_context_de.wav',
182 context=
'beleuchtung',
183 is_whithin_context=
True,
184 intent=
'changeState',
185 slots=dict(state=
'aus'))
190 audio_file_name=
'test_out_of_context_de.wav',
191 context=
'beleuchtung',
192 is_whithin_context=
False
196 if __name__ ==
'__main__':
197 if len(sys.argv) != 2:
198 print(
"usage: test_rhino.py ${ACCESS_KEY}")
201 unittest.main(argv=sys.argv[:1])