porcupine/binding/python/util.py
Go to the documentation of this file.
1 #
2 # Copyright 2020-2022 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 logging
13 import os
14 import platform
15 import subprocess
16 
17 
18 log = logging.getLogger('PPN')
19 log.setLevel(logging.WARNING)
20 
21 
22 def _pv_linux_machine(machine):
23  if machine == 'x86_64':
24  return machine
25  elif machine == 'aarch64':
26  arch_info = '-' + machine
27  elif machine in ['armv7l', 'armv6l']:
28  arch_info = ''
29  else:
30  raise NotImplementedError("Unsupported CPU architecture: '%s'" % machine)
31 
32  cpu_info = ''
33  try:
34  cpu_info = subprocess.check_output(['cat', '/proc/cpuinfo']).decode()
35  cpu_part_list = [x for x in cpu_info.split('\n') if 'CPU part' in x]
36  cpu_part = cpu_part_list[0].split(' ')[-1].lower()
37  except Exception as error:
38  raise RuntimeError("Failed to identify the CPU with '%s'\nCPU info: %s" % (error, cpu_info))
39 
40  if '0xb76' == cpu_part:
41  return 'arm11' + arch_info
42  elif '0xc07' == cpu_part:
43  return 'cortex-a7' + arch_info
44  elif '0xd03' == cpu_part:
45  return 'cortex-a53' + arch_info
46  elif '0xd07' == cpu_part:
47  return 'cortex-a57' + arch_info
48  elif '0xd08' == cpu_part:
49  return 'cortex-a72' + arch_info
50  elif '0xc08' == cpu_part:
51  return 'beaglebone' + arch_info
52  elif machine == 'armv7l':
53  log.warning(
54  'WARNING: Please be advised that this device (CPU part = %s) is not officially supported by Picovoice. '
55  'Falling back to the armv6-based (Raspberry Pi Zero) library. This is not tested nor optimal.' % cpu_part)
56  return 'arm11'
57  else:
58  raise NotImplementedError("Unsupported CPU: '%s'." % cpu_part)
59 
60 
62  pv_system = platform.system()
63  if pv_system not in {'Darwin', 'Linux', 'Windows'}:
64  raise ValueError("Unsupported system '%s'." % pv_system)
65 
66  if pv_system == 'Linux':
67  pv_machine = _pv_linux_machine(platform.machine())
68  else:
69  pv_machine = platform.machine()
70 
71  return pv_system, pv_machine
72 
73 
74 _PV_SYSTEM, _PV_MACHINE = _pv_platform()
75 
76 _RASPBERRY_PI_MACHINES = {'arm11', 'cortex-a7', 'cortex-a53', 'cortex-a72', 'cortex-a53-aarch64', 'cortex-a72-aarch64'}
77 _JETSON_MACHINES = {'cortex-a57-aarch64'}
78 
79 
80 def pv_library_path(relative):
81  if _PV_SYSTEM == 'Darwin':
82  if _PV_MACHINE == 'x86_64':
83  return os.path.join(os.path.dirname(__file__), relative, 'lib/mac/x86_64/libpv_porcupine.dylib')
84  elif _PV_MACHINE == "arm64":
85  return os.path.join(os.path.dirname(__file__), relative, 'lib/mac/arm64/libpv_porcupine.dylib')
86  elif _PV_SYSTEM == 'Linux':
87  if _PV_MACHINE == 'x86_64':
88  return os.path.join(os.path.dirname(__file__), relative, 'lib/linux/x86_64/libpv_porcupine.so')
89  elif _PV_MACHINE in _JETSON_MACHINES:
90  return os.path.join(
91  os.path.dirname(__file__),
92  relative,
93  'lib/jetson/%s/libpv_porcupine.so' % _PV_MACHINE)
94  elif _PV_MACHINE in _RASPBERRY_PI_MACHINES:
95  return os.path.join(
96  os.path.dirname(__file__),
97  relative,
98  'lib/raspberry-pi/%s/libpv_porcupine.so' % _PV_MACHINE)
99  elif _PV_MACHINE == 'beaglebone':
100  return os.path.join(os.path.dirname(__file__), relative, 'lib/beaglebone/libpv_porcupine.so')
101  elif _PV_SYSTEM == 'Windows':
102  return os.path.join(os.path.dirname(__file__), relative, 'lib/windows/amd64/libpv_porcupine.dll')
103 
104  raise NotImplementedError('Unsupported platform.')
105 
106 
107 def pv_model_path(relative):
108  return os.path.join(os.path.dirname(__file__), relative, 'lib/common/porcupine_params.pv')
109 
110 
112  if _PV_SYSTEM == 'Darwin':
113  return 'mac'
114  elif _PV_SYSTEM == 'Linux':
115  if _PV_MACHINE == 'x86_64':
116  return 'linux'
117  elif _PV_MACHINE in _JETSON_MACHINES:
118  return 'jetson'
119  elif _PV_MACHINE in _RASPBERRY_PI_MACHINES:
120  return 'raspberry-pi'
121  elif _PV_MACHINE == 'beaglebone':
122  return 'beaglebone'
123  elif _PV_SYSTEM == 'Windows':
124  return 'windows'
125 
126  raise NotImplementedError('Unsupported platform')
127 
128 
129 def pv_keyword_paths(relative):
130  keyword_files_dir = \
131  os.path.join(os.path.dirname(__file__), relative, 'resources/keyword_files', pv_keyword_files_subdir())
132 
133  res = dict()
134  for x in os.listdir(keyword_files_dir):
135  res[x.rsplit('_')[0]] = os.path.join(keyword_files_dir, x)
136 
137  return res
python.util.pv_keyword_files_subdir
def pv_keyword_files_subdir()
Definition: porcupine/binding/python/util.py:111
python.util._pv_platform
def _pv_platform()
Definition: porcupine/binding/python/util.py:61
python.util.pv_library_path
def pv_library_path(relative)
Definition: porcupine/binding/python/util.py:80
python.util.pv_keyword_paths
def pv_keyword_paths(relative)
Definition: porcupine/binding/python/util.py:129
python.util._pv_linux_machine
def _pv_linux_machine(machine)
Definition: porcupine/binding/python/util.py:22
python.util.pv_model_path
def pv_model_path(relative)
Definition: porcupine/binding/python/util.py:107


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