porcupine_demo.py
Go to the documentation of this file.
1 #
2 # Copyright 2020 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 sys
15 from threading import Thread
16 
17 import pvporcupine
18 from pvrecorder import PvRecorder
19 from gpiozero import LED
20 
21 from .apa102 import APA102
22 
23 COLORS_RGB = dict(
24  blue=(0, 0, 255),
25  green=(0, 255, 0),
26  orange=(255, 128, 0),
27  pink=(255, 51, 153),
28  purple=(128, 0, 128),
29  red=(255, 0, 0),
30  white=(255, 255, 255),
31  yellow=(255, 255, 51),
32  off=(0, 0, 0),
33 )
34 
35 KEYWORDS_COLOR = {
36  'alexa': 'yellow',
37  'computer': 'white',
38  'hey google': 'red',
39  'hey siri': 'purple',
40  'jarvis': 'pink',
41  'picovoice': 'green',
42  'porcupine': 'blue',
43  'bumblebee': 'orange',
44  'terminator': 'off',
45 }
46 
47 driver = APA102(num_led=12)
48 power = LED(5)
49 power.on()
50 
51 
52 class PorcupineDemo(Thread):
53  def __init__(self, access_key, device_index, sensitivity):
54  super(PorcupineDemo, self).__init__()
55 
56  self._device_index = device_index
57  self._keywords = list(KEYWORDS_COLOR.keys())
58  self._porcupine = pvporcupine.create(access_key=access_key, keywords=self._keywords, sensitivities=[sensitivity] * len(KEYWORDS_COLOR))
59 
60  @staticmethod
61  def _set_color(color):
62  for i in range(12):
63  driver.set_pixel(i, color[0], color[1], color[2])
64  driver.show()
65 
66  def run(self):
67  recorder = None
68 
69  try:
70  recorder = PvRecorder(device_index=self._device_index, frame_length=self._porcupine.frame_length)
71  recorder.start()
72 
73  print('[Listening ...]')
74 
75  while True:
76  pcm = recorder.read()
77  keyword_index = self._porcupine.process(pcm)
78  if keyword_index >= 0:
79  print("detected '%s'" % self._keywords[keyword_index])
80  self._set_color(COLORS_RGB[KEYWORDS_COLOR[self._keywords[keyword_index]]])
81  except KeyboardInterrupt:
82  sys.stdout.write('\b' * 2)
83  print('Stopping ...')
84  finally:
85  if recorder is not None:
86  recorder.delete()
87 
88  self._porcupine.delete()
89 
90 
91 def main():
92  parser = argparse.ArgumentParser()
93 
94  parser.add_argument('--access_key',
95  help='AccessKey obtained from Picovoice Console (https://picovoice.ai/console/)',
96  required=True)
97 
98  parser.add_argument('--audio_device_index', help='Index of input audio device.', type=int, default=-1)
99 
100  args = parser.parse_args()
101 
102  o = PorcupineDemo(access_key=args.access_key,
103  device_index=args.audio_device_index,
104  sensitivity=0.6)
105  o.run()
106 
107 
108 if __name__ == '__main__':
109  main()
ppnrespeakerdemo.porcupine_demo.PorcupineDemo
Definition: porcupine_demo.py:52
ppnrespeakerdemo.porcupine_demo.PorcupineDemo._keywords
_keywords
Definition: porcupine_demo.py:57
ppnrespeakerdemo.porcupine_demo.PorcupineDemo.run
def run(self)
Definition: porcupine_demo.py:66
ppnrespeakerdemo.apa102.APA102
Definition: apa102.py:11
ppnrespeakerdemo.porcupine_demo.PorcupineDemo._device_index
_device_index
Definition: porcupine_demo.py:56
ppnrespeakerdemo.porcupine_demo.main
def main()
Definition: porcupine_demo.py:91
ppnrespeakerdemo.porcupine_demo.PorcupineDemo.__init__
def __init__(self, access_key, device_index, sensitivity)
Definition: porcupine_demo.py:53
main
Definition: main.py:1
ppnrespeakerdemo.porcupine_demo.PorcupineDemo._set_color
def _set_color(color)
Definition: porcupine_demo.py:61
ppnrespeakerdemo.porcupine_demo.PorcupineDemo._porcupine
_porcupine
Definition: porcupine_demo.py:58


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