Skip to content

Raspberry Pi Audio Machine Learning with Python

Veronika Stellwag edited this page May 10, 2021 · 8 revisions

In this tutorial you learn how to do use our I2S microphones and Python to fulfill a basic classification task using Machine Learning. For audio recording we use the python-sounddevice library, for processing we use SciPy and feature extraction is done using librosa. Pandas is used to prepare the audio dataset. The Machine Learning model itself comes from Python's scikit-learn library.

Requirements

The first step is to go through this guide and make sure that I2S is running on your Raspberry Pi and the I2S microphone is connected.

Apt Packages

For the required Pip packages ATLAS, PortAudio and the ALSA development package are required:

sudo apt-get install libatlas-base-dev libportaudio2 libasound-dev llvm libsndfile-dev

Pip Packages

For communication between our microphone and Python we use the python-sounddevice library. It can be installed using the following command:

python3 -m pip install --user sounddevice

For pre-processing the audio data (e.g. high-pass filtering) we need to install SciPy:

python3 -m pip install --user scipy

Raw audio data can be an input for a Machine Learning model, but for this example we make it simple and generate certain features of the audio samples as model input - instead of taking the raw data directly. These features can be extracted using librosa.

LLVM_CONFIG=llvm-config python3 -m pip install --user surfboard
python3 -m pip install --user librosa

Pandas (Python Data Analysis Library) is required to prepare the recorded dataset for Machine Learning with scikit-learn.

python3 -m pip install --user pandas

And of course we also need to install scikit-learn, a powerful Machine Learning framework for Python.

python3 -m pip install --user sklearn

Running the example

You can find the example in this GitHub repo at application_examples/raspberrypi/machine_learning/basic_classification.py. To run the example, please clone the repository locally on your Raspberry Pi.

Before running the examples please open it and read the comments to understand what it does. You can run the example by typing

python3 basic_classification.py

on your Raspberry Pi.

The example will look for a file samples.p. If the file exists it reads out the audio samples from this file. If not, it asks you to generate your own audio samples. Further details can be found in the comments of the source code.

Clone this wiki locally