File size: 1,552 Bytes
3518c99
d5683e9
3518c99
 
 
abd8c0e
09044bc
d5683e9
3518c99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6536c1
abd8c0e
d6536c1
d5683e9
3518c99
 
 
 
 
 
 
 
 
 
be9cdb0
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
# 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]
    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 = 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
  
iface = gr.Interface(fn=get_audio,inputs=["audio"],outputs=["text"],title="CAT/DOG AUDIO CLASSIFICATION").launch(inline=False)