Integrate YAMNet for audio classification

- Added sample audio files: cafe_crowd_talk.wav, miaow_16k.wav, and speech_whistling2.wav
- Implemented YAMNet-based audio classification in classify.py
- Updated requirements.txt to include TensorFlow and dependencies for YAMNet
This commit is contained in:
Joel Mathew Thomas
2024-12-26 00:41:23 +05:30
parent 7fbc8ab651
commit 1ddbbadfc8
5 changed files with 138 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import csv
import matplotlib.pyplot as plt
from IPython.display import Audio
from scipy.io import wavfile
from scipy import signal
model = hub.load('https://tfhub.dev/google/yamnet/1')
#Find the name of the class with the top score when mean-aggregated across frames.
def class_names_from_csv(class_map_scv_text):
"""Returns list of class names corresponding to score vector."""
class_names = []
with tf.io.gfile.GFile(class_map_scv_text) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
class_names.append(row['display_name'])
return class_names
class_map_path = model.class_map_path().numpy()
class_names = class_names_from_csv(class_map_path)
# Resample audio to 16K
def ensure_sample_rate(original_sample_rate, waveform, desired_sample_rate=16000):
"""Resample waveform if required."""
if original_sample_rate != desired_sample_rate:
desired_length = int(round(float(len(waveform)) / original_sample_rate * desired_sample_rate))
waveform = signal.resample(waveform, desired_length)
return desired_sample_rate, waveform
# wav_file_name = 'speech_whistling2.wav'
wav_file_name = 'cafe_crowd_talk.wav'
sample_rate, wav_data = wavfile.read(wav_file_name, 'rb')
sample_rate, wav_data = ensure_sample_rate(sample_rate, wav_data)
# Show some basic information about the audio.
duration = len(wav_data)/sample_rate
print(f'Sample rate: {sample_rate} Hz')
print(f'Total duration: {duration:.2f}s')
print(f'Size of the input: {len(wav_data)}')
# The wav_data needs to be normalized to values in [-1.0, 1.0]
waveform = wav_data / tf.int16.max
# Execute the Model
# Check the output.
scores, embeddings, spectogram = model(waveform)
scores_np = scores.numpy()
spectogram_np = spectogram.numpy()
infered_class = class_names[scores_np.mean(axis=0).argmax()]
print(f'The main sound is : {infered_class}')