-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
47 lines (43 loc) · 1.62 KB
/
main.py
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
import tensorflow as tf
import librosa
import numpy as np
from fastapi import FastAPI, HTTPException
app = FastAPI()
dict = {0:'airconditioner',
1:'carhorn',
2:'children_playing',
3:'dog_bark',
4:'drilling',
5:'engine_idling',
6:'gun_shot',
7:'jackhammer',
8:'siren',
9:'street_music',
10:'screaming',
11:'fire_alarm',
12:'crying',
13:'traffic_noise'
}
@app.get("/sound")
def get_predict_sound():
new_model = tf.keras.models.load_model('audio_classification_final.hdf5')
filename="dog-barking.wav"
audio, sample_rate = librosa.load(filename, sr=16000, res_type='kaiser_fast')
mels = np.mean(librosa.feature.melspectrogram(y=audio, sr=sample_rate).T,axis=0)
mels = mels.transpose()
mels = mels.reshape(1, 16, 8, 1)
print(mels.shape)
predicted_label = np.argmax(new_model.predict(mels), axis=-1)
print(dict[predicted_label[0]])
return {"Predicted sound": dict[predicted_label[0]]}
@app.post("/sound")
def predict_sound(filename: str):
new_model = tf.keras.models.load_model('audio_classification_final.hdf5')
audio, sample_rate = librosa.load(filename, sr=16000, res_type='kaiser_fast')
mels = np.mean(librosa.feature.melspectrogram(y=audio, sr=sample_rate).T,axis=0)
mels = mels.transpose()
mels = mels.reshape(1, 16, 8, 1)
print(mels.shape)
predicted_label = np.argmax(new_model.predict(mels), axis=-1)
print(dict[predicted_label[0]])
return {"Predicted sound": dict[predicted_label[0]]}