Building a Machine Learning Model for Answering Machine Detection

Published March 20, 2019 by Tony Hung

Did you ever need a way to detect when an answering machine was on a voice call? No? Thats ok. I did!

Prerequisites

This post assumes you have basic Python experience, as well as having a very basic understanding of machine learning. We’ll go over a few basic concepts on machine learning, and we have linked to more resources throughout this post.


A few weeks ago, I received a request from one of our sales engineers about an answering machine detection service for a client. They wanted a way to send a message to a answering machine when the call went to voicemail.

I’ve done some research on this, and it does seem possible, but I couldn’t find anything on HOW this was done. So I decided to figure it out…

The first thought was to build a machine learning model that detects when the beep sound in an answering machine is heard. In this post, we’ll go over how the model was trained and deployed into a application.

Training Data

Before we can start building a machine learning model, we need to have some data. For this problem, we need to have a bunch of audio files with the answering machine beep sounds, like this:


or this:

We also need to include samples that don’t include the beep sound:

Since this kind of data doesn’t seem to exist on the internet, we needed to gather as many samples as possible of beeps and other sounds from calls, in order to train our model. To do this, I built a webpage that allows anyone to record their voicemail greeting message.

When you call the Nexmo number, the application will create an outbound call to the same number. When the call is received, you just need to send the call directly to voicemail. From there, we record the call using the record action and save the file into a Google Cloud Storage bucket. After gathering a lot of examples, we can start looking at the data.


In any machine learning project, one of the first things to do is to look at the data and make sure it’s something we can work with.

Since it’s audio, we can’t look at it directly, but we can visualize the audio files using a mel-spectrogram, which looks like this:

A mel-spectrogram shows a range of frequencies (lowest at the bottom of the display, highest at the top) and shows how loud events are at different frequencies. In general, loud events will appear bright and quiet events will appear dark.

We’ll need to load a few files of both types of sounds, plot them, and see how they look. To show the mel-spectrogram, we’ll use a Python package called Librosa to load the audio recording, then plot the mel-spectrogram using matplotlib, another Python package to plot charts and graphs.

Let’s see what each audio file looks like.

You can clearly tell which audio file is a beep and which is just speech.


Before we train our model, we will take all the recordings that we have for both beeps and non-beeps, which are labeled as speech, and convert each recording into a vector of numbers, since our model will only accept numbers, not images.

To compute the data, we’ll use the mel-frequency cepstral coefficients (MFCCs) of each sample. Then, we’ll save this value into a csv so that we do not have to re-compute the MFCC’s over again.

For each audio sample, the csv will contain the path to the audio sample, the label of audio sample(beep, or speech), the MFCC, and the duration of the audio sample (using the get_duration function in librosa). We also tried a few other audio characteristics including chroma, contrast and tonnetz). However, these features were not used in the latest version of the model.

Let’s now take a look at the first 5 rows of the csv, just to see what the data looks like.

Each row contains a 1 dimension vector of each of the audio features. This is what we’ll use to train our model.

Training

Now we’ll take this data and train a model with it. We’ll be using the Scikit-learn package to do our training. Scikit-learn is a great package that allows you to build simple machine learning models without having to be a machine learning expert.

For each model, we took our dataframe, which contained the label of each audio file, (beep, speech), with the MFCC for each sample, split it into a train and test dataset, and ran each model through the data.

The function train takes a list of features that we want to use, which is just MFCC of the audio sample, as well as the model we want to train on. Then we print our score, which is how well the model performed. We also print the cross validation score. This makes sure that our model was trained correctly. The plot_confusion_matrix function plots a confusion matrix that shows exactly what the model got correct and incorrect.

We then tried the following models and included their accuracy (0-100% score on how well the model did).

All these models performed very well, except Support Vector Machines. The best was Gaussian Naive Bayes, so we will use that model. In our Confusion Matrix from above, out of the 67 examples, 40 samples that were predicted as a beep were actually beeps, and 22 samples that were predicted to be speech were, in fact, speech examples. However, 1 example that was predicted to be a beep was actually speech.

After we have our model, we need to save it to a file, then import this model into our VAPI application.

Building the Application

The last part is to now integrate our model into a VAPI application.
Source

When building an application, you have to first create a Nexmo application and purchase a Nexmo number.

We’ll build an application that lets a user dial a Nexmo number. We’ll then ask the user to enter a phone number to call. Once that number is entered, we’ll connect that call into the current conversation and connect to our websocket. Using Nexmo websockets, we are able to stream the audio call into our application.

First, we need to load our model into our application.

When the user first dials the Nexmo number, we return a NCCO with the following:

We first send a Text-To-Speech action into the call asking the user to enter a phone number. When the phone number is entered, we get those digits from the https://3c66cdfa.ngrok.io/ivr url.

After the phone number is entered, we will receive a callback from the https://3c66cdfa.ngrok.io/ivr url. Here we take the phone number the user entered from data["dtmf"] and perform a connect action to that phone number, then perform another connect action into our websocket. Now our websocket is able to listen in on the call.

As the call is streamed into the websocket, we need to capture chunks of speech using Voice Activity Detection, save into a wave file, and make our predictions on that wav file using our trained model.

Once we have a wav file, we use librosa.load to load in the file, and then use the librosa.feature.mfcc function to generate the MFCC of the sample. We then call loaded_model.predict([mfccs]) to make our prediction. If the output of this function is 0, a beep was detected. If it outputs 1, then it’s speech. We then generate a JSON payload of whether a beep was detected, and the uuids of the conversation. This way, our client application can send a TTS into the call, using the uuids.

Websocket Client

The final step is to build a client that connects to the websocket, observes when a beep is detected, and sends a TTS into the call, when the voicemail is detected.

Source

First, we need to connect to the websocket.

Next, we just listen for any incoming message from our websocket.

We’ll parse the incoming message as JSON, then check the beep_detected property is True. If it is, then a beep was detected. We will then send a TTS into the call saying ‘Answering Machine Detected’, then perform a hangup action into the call.


Conclusion

We’ve shown how we built a answering machine detection model with 96% accuracy, using a few audio samples of beeps and speech in order to train our model. Hopefully, we’ve shown how you can use machine learning in your projects. Enjoy!

Leave a Reply

Your email address will not be published.