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 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)
pip3 install --upgrade pip

4. Launch Examples


Install some Python packages

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

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 Metrics parameters.

# audio_file_example.py

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

# Metrics
params.metrics.retention_period = 0   # days
params.metrics.free_disk_space = 100  # MB
params.metrics.push_period = 30       # seconds
params.log_level = 0

params.device_name = "Testing device"
...

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

# audio_file_example.py

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

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

# audio_stream_example.py

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

# Metrics
params.metrics.retention_period = 0   # days
params.metrics.free_disk_space = 100  # MB
params.metrics.push_period = 30       # seconds
params.log_level = 0

params.device_name = "Testing device"
...

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

# audio_stream_example.py

...
if SenseInit("Your project key",
             params) < 0:
...
sense.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