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