Python Sample

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

sense-flie makes prediction out of the input file, and sense-stream makes prediction out of the input stream such as a microphone.

This guide walks through how to run the samples.

1. Check Requirements


Pre-setup

Python supports

  • Cochl.Sense Python SDK currently supports
    • Python3.6
    • Python3.7
    • Python3.8
  • Please contact support@cochl.ai for other version supports.

2. Prerequisites and Dependencies


Install packages to use Cochl.Sense SDK Python.

  $ 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.

# if python3.6
  $ sudo apt install python3-dev

# if 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 virtualenv -p python3 ./venv

Activate the virtual environment using a shell-specific command

  $ source ./venv/bin/activate

When virtualenv is active, your shell prompt is prefixed with (venv). Install packages within a virtual environment without affecting the host system setup. Start by upgrading pip:

  (venv) $ pip3 install --upgrade pip

4. Launch Examples


Install Python packages

  (venv) $ pip3 install numpy pyaudio sox pydub

Install Cochl.Sense SDK

Install Cochl.Sense wheel file that you’ve downloaded here.

  (venv) $ pip3 install path/to/sdk/sense-1.0-xxx-xxx.whl

Prepare sample

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

Set up parameters

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

# audio_file_example.py

# Default, Emergency, Human_Interaction, Human_Status, Home_Context params.service = sense.Human_Interaction # AF_UINT8 AF_INT8 AF_INT16 AF_INT32 AF_DOUBLE AF_FLOAT32 params.audio_format = sense.AF_FLOAT32 # Metrics params.metrics.retention_period = 0 # days params.metrics.free_disk_space = 100 # MB params.metrics.push_period = 30 # seconds

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

# 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

# Default, Emergency, Human_Interaction, Human_Status, Home_Context params.service = sense.Human_Interaction # AF_UINT8 AF_INT8 AF_INT16 AF_INT32 AF_DOUBLE AF_FLOAT32 params.audio_format = sense.AF_FLOAT32 # Metrics params.metrics.retention_period = 0 # days params.metrics.free_disk_space = 100 # MB params.metrics.push_period = 30 # seconds

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

# 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) , try again with different version numpy.

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