Shubhabrata08 / AudioClassificationTFLite

This project aims to classify UrbanSound8K audio and deploy the model to a microcontroller
0 stars 0 forks source link

Audio recording and MFCC generation on an ESP32 board or Arduino board #5

Open Shubhabrata08 opened 8 months ago

Shubhabrata08 commented 8 months ago

Considering the workflow for audio recording and feature extraction is clear as per #1 , we can proceed towards the POC for the same task but on an ESP32 or Arduino board. The previous POC can be employed in dev testing when issue #2 and #3 is closed.

Shubhabrata08 commented 8 months ago

Please investigate this task and report your deductions

sa-paul commented 7 months ago

MFCC generation code running properly in Raspberry pi 3:

Code:


import sounddevice as sd
import soundfile as sf
import numpy as np
import librosa.display
import matplotlib.pyplot as plt

def record_audio(sample_rate, duration):
    audio_data = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1, dtype='float')
    sd.wait()
    return audio_data.flatten()

def extract_mfcc(audio_data, sample_rate, n_mfcc=13, hop_length=512):
    mfccs = librosa.feature.mfcc(y=audio_data, sr=sample_rate, n_mfcc=n_mfcc, hop_length=hop_length)
    return mfccs

# Record a short audio clip (adjust sample_rate and duration as needed)
sample_rate = 44100
duration = 5
audio_data = record_audio(sample_rate, duration)

# Extract MFCC features
mfccs = extract_mfcc(audio_data, sample_rate)

# Display MFCC features
plt.figure(figsize=(10, 4))
librosa.display.specshow(mfccs, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.show()

Output:

WhatsApp Image 2024-02-11 at 6 34 19 PM

Extra dependencies: Reference

  1. Install the ALSA developer library, required by portaudio sudo apt-get install libasound2-dev
  2. Install portaudio sudo apt-get install portaudio19-dev
  3. Install ATLAS (numpy dependency) sudo apt-get install libatlas-base-dev
  4. Otherwise, shown error portAudio not found