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-dev

  # for other versions
  $ sudo apt install python3.x-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 --no-site-packages ./venv
  $ virtualenv -p python3.7 --no-site-packages ./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 ./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 numpy 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 your Service, Audio Format, and Metrics parameters.

# audio_file_example.py

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

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 sense.SenseInit("{your-project-key}", params) < 0: sense.SenseTerminate()

Open audio_stream_example.py in the sample directory, and set your Service, Audio Format, and Metrics parameters.

# audio_stream_example.py

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

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 sense.SenseInit("{your-project-key}", params) < 0: sense.SenseTerminate()

Run

  (venv) $ python3 audio_file_example.py
  (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
  (venv) $ pip3 install numpy==1.19.4