File size: 1,944 Bytes
3518c99
d5683e9
3518c99
 
 
abd8c0e
09044bc
d5683e9
3518c99
 
 
 
 
 
 
447969b
6518e15
3bd0279
 
3518c99
 
 
 
 
 
 
 
 
 
 
 
 
d6536c1
abd8c0e
d6536c1
d5683e9
3518c99
 
 
 
 
 
 
 
 
 
db59b72
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# import library
import gradio as gr
import librosa
import pandas as pd
import numpy as np
import pickle
import os

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]
    print(file)
    print(file.name)
    audio_binary = tf.io.read_file(file.name)
    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 = pickle.load(open('audio_classifier_model.pkl', 'rb'))
    
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
  

# %gradio interface

inputs = gr.inputs.Audio(label="Input Audio", type="file")
outputs = "text"
title = "Cat/Dog Audio Classification"
description = "Gradio demo App for Cat and Dog Audio Classification with Tensorflow. To use it, simply upload your audio .wav format, or use sample audio by click the button below Example"
examples = [
    ['dog_barking_102.wav']
]
gr.Interface(get_audio, inputs, outputs, title=title, description=description, examples=examples).launch()