-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_test.py
More file actions
120 lines (111 loc) · 4.88 KB
/
model_test.py
File metadata and controls
120 lines (111 loc) · 4.88 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import librosa
import librosa.display
import numpy as np
import wave
import contextlib
import scipy.io.wavfile as wav
import os
import tensorflow as tf
from sms import send_email, send_msg
from flask import jsonify
modileNo = 3136525478
receiver_email = 'apoorv.mehrotra@sjsu.edu'
#get current directory path
path = os.getcwd()
#get the path to save the uploaded audio file
#model_path = path + "/model5_unshuf_aug_accu96.model"
#model_path1 = path + "/model4_shuf_aug_accu_97.model"
model_path2= path + "/SoundPredict_91acu_35epochs.model"
model_path3 = path + "/SoundPredict_aug_95.model"
model1=tf.keras.models.load_model(model_path2)
model2=tf.keras.models.load_model(model_path3)
def PredictVersion2(filename,path):
file_path = path + '/' + filename
categories = ['air_conditioner','car_horn','children_playing','dog_bark','drilling','engine_idling','gun_shot','jackhammer','siren','street_music']
try:
#Verify the video duration, should be greater than equal
# to 2.97 sec for good audio recognition
(source_rate, source_sig) = wav.read(file_path)
duration_seconds = len(source_sig) / float(source_rate)
print(duration_seconds)
if duration_seconds >= 2.97:
#If required file size met, reshape the audio file to process through the model
y, sr = librosa.load(path + '/' + filename, duration=2.97)
ps = librosa.feature.melspectrogram(y=y, sr=sr)
re_shape=np.array([ps.reshape( (128, 128, 1))])
print(re_shape)
else:
response = jsonify({"status": "small_size", "message": "File size is small, should be greater than 3 seconds"})
#print("Internal Server error: {}".format(err))
response.status_code=200
return response
model_list = []
model_list.append(model1)
model_list.append(model2)
index = 0
percentage = 0
for model in model_list:
prediction = model.predict([re_shape])
#prediction.shape
index = 0
percentage = 0
for i in range(10):
print(prediction[0][i])
if(prediction[0][i]*100 > percentage):
index = i
percentage = prediction[0][i]*100
if index == 6:
percent = np.float64(percentage).item()
percent=round(percent,2)
type(percentage)
type(percent)
send_email(receiver_email,percent)
send_msg(modileNo,percent)
response = jsonify({"status":200, "sound_source":categories[index].capitalize(),
"surety":percent, "notification":"Sent"})
return response
return jsonify({"status":200, "sound_source":categories[index].capitalize(), "surety":percentage})
except ValueError as err:
response = jsonify({"status": 500, "message": str(err)})
#print("Internal Server error: {}".format(err))
response.status_code=500
return response
def Predict(filename,path):
file_path = path + '/' + filename
categories = ['air_conditioner','car_horn','children_playing','dog_bark','drilling','engine_idling','gun_shot','jackhammer','siren','street_music']
try:
#Verify the video duration, should be greater than equal
# to 2.97 sec for good audio recognition
(source_rate, source_sig) = wav.read(file_path)
duration_seconds = len(source_sig) / float(source_rate)
print(duration_seconds)
if duration_seconds >= 2.97:
#If required file size met, reshape the audio file to process through the model
y, sr = librosa.load(path + '/' + filename, duration=2.97)
ps = librosa.feature.melspectrogram(y=y, sr=sr)
re_shape=np.array([ps.reshape( (128, 128, 1))])
print(re_shape)
model = tf.keras.models.load_model(model1)
#Now, we can make a prediction:
prediction = model.predict([re_shape])
prediction.shape
print(prediction)
index = 0
percentage = 0
for i in range(10):
print(prediction[0][i])
if(prediction[0][i]*100 > percentage):
index = i
percentage = prediction[0][i]*100
#if index == 6:
#send_email(receiver_email)
#send_msg(modileNo)
#if(percentage > 75):
return {"status":200, "sound_source":categories[index], "surety":percentage}
#else:
# return {"status":"200","message":"Sound can't be predicted, Too much disturbance"}
else:
return {"status":"401","message":"File is to short"}, 401
except ValueError as err:
#print("Internal Server error: {}".format(err))
return {"status": 500, "message": str(err)}, 500