-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_creator.py
More file actions
63 lines (48 loc) · 1.87 KB
/
model_creator.py
File metadata and controls
63 lines (48 loc) · 1.87 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
import librosa
import numpy as np
import joblib
import io
from six.moves.urllib.request import urlopen
#Trainng
import os
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import pandas as pd
path = (os.path.abspath(os.path.dirname(__file__).replace("",""))+"/dataset.csv")
dt = pd.read_csv(path)
train, test = train_test_split(dt)
#print(train.shape)
#print(test.shape)
train_X = train[['chroma_stft','spec_cent','spec_bw','rolloff','zcr','mfcc']]# features
train_y = train.prognosis #labels
test_X = test[['chroma_stft','spec_cent','spec_bw','rolloff','zcr','mfcc']]
test_y = test.prognosis
#knn
model = KNeighborsClassifier(n_neighbors=3) #KNN Model for classification
model.fit(train_X, train_y)
joblib.dump(model, 'covid_cough_model.sav') #save model using joblib
print("model saved")
url = "https://firebasestorage.googleapis.com/v0/b/server-65459.appspot.com/o/neg-cough.wav?alt=media&token=bd7a2d9f-b5f4-41d8-869d-9d61fd31fe17"# an example url where the cough audio is manually uploaded
y, sr = librosa.load(io.BytesIO(urlopen(url).read()))
#print(y.shape)
#print(sr)
chroma_stft = librosa.feature.chroma_stft(y=y, sr=sr)
spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr)
rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)
zcr = librosa.feature.zero_crossing_rate(y)
mfcc = librosa.feature.mfcc(y=y, sr=sr)
#loading up variables
chroma_stft = np.mean(chroma_stft)
spec_cent = np.mean(spec_cent)
spec_bw = np.mean(spec_bw)
rolloff = np.mean(rolloff)
zcr = np.mean(zcr)
mfcc = np.mean(mfcc)
#prediction
model = joblib.load('covid_cough_model.sav')
#print(model)
val = np.array([chroma_stft,spec_cent,spec_bw,rolloff,zcr,mfcc])
val=val.reshape(1,-1)
prediction = model.predict(val)
print(prediction[0])