Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,32 +7,40 @@ model = tf.keras.models.load_model('capuchin_bird_audio.h5')
|
|
7 |
class_names = ['This Is Not A Capuchin bird','It is a capuchin Bird']
|
8 |
# Function to preprocess input for the model
|
9 |
def test_preprocess_1(file_path):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Function to make predictions
|
25 |
def predict_audio(wav):
|
26 |
input_data = test_preprocess_1(wav)
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
else:
|
33 |
-
|
34 |
-
|
35 |
-
return result
|
36 |
|
37 |
|
38 |
# Gradio Interface
|
@@ -45,5 +53,5 @@ iface = gr.Interface(
|
|
45 |
)
|
46 |
|
47 |
# Launch the interface on localhost
|
48 |
-
iface.launch()
|
49 |
|
|
|
7 |
class_names = ['This Is Not A Capuchin bird','It is a capuchin Bird']
|
8 |
# Function to preprocess input for the model
|
9 |
def test_preprocess_1(file_path):
|
10 |
+
_, file_extension = os.path.splitext(file_path)
|
11 |
+
|
12 |
+
if file_extension.lower() == '.wav':
|
13 |
+
file_contents = tf.io.read_file(file_path)
|
14 |
+
wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)
|
15 |
+
wav = tf.squeeze(wav, axis=-1)
|
16 |
+
sample_rate = tf.cast(sample_rate, dtype=tf.int64)
|
17 |
+
wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=16000)
|
18 |
+
wav = wav[:48000]
|
19 |
+
zero_padding = tf.zeros([48000] - tf.shape(wav), dtype=tf.float32)
|
20 |
+
wav = tf.concat([zero_padding, wav], 0)
|
21 |
+
spectrogram = tf.signal.stft(wav, frame_length=320, frame_step=32)
|
22 |
+
spectrogram = tf.abs(spectrogram)
|
23 |
+
spectrogram = tf.expand_dims(spectrogram, axis=2)
|
24 |
+
spectrogram = tf.expand_dims(spectrogram, axis=0)
|
25 |
+
return spectrogram
|
26 |
+
else:
|
27 |
+
return False
|
28 |
|
29 |
# Function to make predictions
|
30 |
def predict_audio(wav):
|
31 |
input_data = test_preprocess_1(wav)
|
32 |
+
if input_data:
|
33 |
+
prediction = model.predict(input_data)
|
34 |
+
|
35 |
+
# Threshold logic
|
36 |
+
if prediction > 0.5:
|
37 |
+
result = class_names[1]
|
38 |
+
else:
|
39 |
+
result = class_names[0]
|
40 |
+
|
41 |
+
return result
|
42 |
else:
|
43 |
+
return "please upload a wav format"
|
|
|
|
|
44 |
|
45 |
|
46 |
# Gradio Interface
|
|
|
53 |
)
|
54 |
|
55 |
# Launch the interface on localhost
|
56 |
+
iface.launch(share=True)
|
57 |
|