Spaces:
Runtime error
Runtime error
# import library | |
import gradio as gr | |
import librosa | |
import pandas as pd | |
import numpy as np | |
import tensorflow as tf | |
from tensorflow.keras.layers.experimental import preprocessing | |
from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
from tensorflow.keras.models import Sequential | |
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization, Input | |
def get_waveform_label(file): | |
lab = tf.strings.split(file, os.path.sep)[-2] | |
audio_binary = tf.io.read_file(file) | |
audio, _ = tf.audio.decode_wav(audio_binary) | |
waveform=tf.squeeze(audio, axis=-1) | |
return waveform | |
def get_spectrogram_label(audio): | |
padding = tf.zeros([300000]-tf.shape(audio), dtype=tf.float32) | |
wave = tf.cast(audio, tf.float32) | |
eq_length = tf.concat([wave, padding], 0) | |
spectrogram = tf.signal.stft(eq_length, frame_length=210, frame_step=110) | |
spectrogram = tf.abs(spectrogram) | |
spectrogram = tf.expand_dims(spectrogram, -1) | |
return spectrogram | |
# %load saved model | |
model = tf.keras.models.load_model('https://github.com/sarufi-io/AUDIO-ANALYSIS-WITH-LIBROSA/tree/main/My_model/cat_dog_audio_classifier') | |
def get_audio(audio): | |
audio_waveform = get_waveform_label(audio) | |
audio_spect = get_spectrogram_label(audio_waveform) | |
final_feat = np.array([audio_spect]) | |
res = np.argmax(model.predict(final_feat),axis=1) | |
if res == 1: | |
res ="Dog Audio"; | |
else: | |
res = "Cat Audio" | |
return res | |
iface = gr.Interface(fn=get_audio,inputs=["audio", "audio"],title="CAT_DOG AUDIO CLASSIFICATION",outputs="html").launch() |