Python Sample

Two samples are provided in the Python version of the Cochl.Sense SDK: sense-file and sense-stream.

  • sense-file performs a prediction on an audio file (wav or mp3).

  • sense-stream performs a prediction on an audio buffer.

1. Check the Requirements


Follow our Getting started section to set up the environment.

Python versions supported

  • Cochl.Sense Python SDK currently supports
    • Python3.6
    • Python3.7
    • Python3.8
  • Please contact support@cochl.ai if you wish to use a different version.

2. Python Requirements


Install the packages below to use our Cochl.Sense Python SDK.

sudo apt update
sudo apt install curl
sudo apt install ffmpeg sox virtualenv portaudio19-dev python3-pyaudio libffi-dev

Install more if you have installed Python using the package tool instead of building Python from source.

# for python3.6
sudo apt install python3.6-dev

# for python3.7
sudo apt install python3.7-dev

# for python3.8
sudo apt install python3.8-dev

3. Python Virtual Environment


Create a new virtual environment by choosing a Python interpreter and making a ./venv directory to hold it.

virtualenv -p python3.6 --no-site-packages ./venv

# NOTE
# - If there's an error with `--no-site-packages`, then remove it and try again with 
virtualenv -p python3.6 ./venv
virtualenv -p python3.7 --no-site-packages ./venv

# NOTE
# - If there's an error with `--no-site-packages`, then remove it and try again with 
virtualenv -p python3.7 ./venv
virtualenv -p python3.8 --no-site-packages ./venv

# NOTE
# - If there's an error with `--no-site-packages`, then remove it and try again with 
virtualenv -p python3.8 ./venv

Activate the virtual environment using a shell-specific command.

source ./venv/bin/activate

When the virtualenv is active, your shell prompt is prefixed with (venv). Installing packages within a virtual environment will not affect the host system setup.


Let’s start by upgrading pip:

# (venv)
curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o get-pip.py
python get-pip.py
pip3 install --upgrade pip
# (venv)
curl https://bootstrap.pypa.io/pip/get-pip.py -o get-pip.py
python get-pip.py
pip3 install --upgrade pip
# (venv)
curl https://bootstrap.pypa.io/pip/get-pip.py -o get-pip.py
python get-pip.py
pip3 install --upgrade pip

4. Install license manager for SDK


To use above v1.4.0 Cochl.Sense SDK, you should prepare the license manager before installing the SDK. This license manager is used to manage Cochl.Sense SDK license when devices are on the network or not.

# e.g., folder tree in Python 3.8 SDK 1.4.0 for x86_64 Ubuntu Linux 
sense
  ├── sense-1.4-py38-none-linux_x86_64.whl
  └── license
        ├── bin
        ├── dinst
        ├── dunst
        ├── haspvlib_25011.so
        ├── haspvlib_arm64_25011.so
        ├── haspvlib_armhf_25011.so
        ├── haspvlib_x86_64_25011.so
        └── pkg

To install the license manager, run the following commands.

cd sense/license
sudo ./dinst

Uninstall license manager for SDK

(Warning) Uninstallation may affect other applications using the license manager service. If you want to remove the license manager, please check whether the system uses this service. To remove the license manager, run the following commands.

# check the service
ps -aux | grep hasplmd

# remove the license manager
cd sense/license
sudo ./dunst

5. Launch Examples


Install some Python packages

# (venv)
pip3 install numpy==1.21.4 || pip3 install numpy==1.19.4
pip3 install pyaudio sox pydub

Install the Cochl.Sense SDK

We can install the Cochl.Sense wheel file that we’ve downloaded here.

# (venv)
pip3 install path/to/sdk/sense-1.x-xxx-xxx.whl

Retrieve the samples

git clone https://github.com/cochlearai/sense-sdk-python-tutorials

Parameters

You can refer to the c++ sample page for more information about those parameters.

Open audio_file_example.py in the sample directory, and set parameters.

# audio_file_example.py

...
# if <= 0. will use all the threads available on the machine
sense_params.num_threads = -1

# Metrics
sense_params.metrics.retention_period = 0   # range, 1 to 31 days
sense_params.metrics.free_disk_space = 100  # range, 0 to 1,000,000 MB
sense_params.metrics.push_period = 30       # range, 1 to 3,600 seconds
sense_params.log_level = 0

sense_params.device_name = "Testing device"

sense_params.hop_size_control.enable = True
sense_params.sensitivity_control.enable = True
sense_params.result_abbreviation.enable = True
sense_params.label_hiding.enable = False  # stream mode only
...

Put your Project Key into the sample code. Note that it’s important to invoke the SenseTerminate() function at the end if SenseInit() was successful. Otherwise, an undefined behavior may occur while cleaning up the memory allocated by the sense during SenseInit().

# audio_file_example.py

...
if SenseInit("Your project key",
             sense_params) < 0:
...
SenseTerminate()
...

Open audio_stream_example.py in the sample directory, and set parameters.

# audio_stream_example.py

...
# if <= 0. will use all the threads available on the machine
sense_params.num_threads = -1

# Metrics
sense_params.metrics.retention_period = 0   # range, 1 to 31 days
sense_params.metrics.free_disk_space = 100  # range, 0 to 1,000,000 MB
sense_params.metrics.push_period = 30       # range, 1 to 3,600 seconds
sense_params.log_level = 0

sense_params.device_name = "Testing device"

sense_params.hop_size_control.enable = True
sense_params.sensitivity_control.enable = True
sense_params.result_abbreviation.enable = True
sense_params.label_hiding.enable = True
...

Put your Project Key into the sample code. Note that it’s important to invoke the SenseTerminate() function at the end if SenseInit() was successful. Otherwise, an undefined behavior may occur while cleaning up the memory allocated by the sense during SenseInit().

# audio_stream_example.py

...
if SenseInit("Your project key",
             sense_params) < 0:
...
SenseTerminate()
...

Run

# (venv)
python3 audio_file_example.py <PATH_TO_AUDIO_FILE>

The repository contains audio files that can be used as well.

# (venv)
python3 audio_file_example.py audio_files/babycry.wav
# (venv)
python3 audio_stream_example.py

Troubleshooting

If the process ends with an error message Illegal instruction (core dumped) , please try again with a different numpy version.

# (venv)
pip3 uninstall numpy
pip3 install numpy==1.19.4