-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
112 lines (100 loc) · 5.44 KB
/
model.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
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
import os
import pickle
import librosa
import numpy as np
import pandas as pd
import seaborn as sns
import soundfile as sf
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
def extract_features(x, path):
X, sample_rate = sf.read(path, dtype='float32')
# Mcc
Mcc = librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=47)
Mcc = np.mean(Mcc.T, axis=0)
# chroma_stft
chroma_stft = librosa.feature.chroma_stft(
y=X, sr=sample_rate, n_chroma=12, n_fft=4096)
chroma_stft = np.mean(chroma_stft.T, axis=0)
# chroma_cqt
chroma_cqt = librosa.feature.spectral_bandwidth(y=X, sr=sample_rate)
chroma_cqt = np.mean(chroma_cqt.T, axis=0)
# tonnetz
#tonnetz = librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate, chroma=chroma_cqt)
# melspectrogram
melspectrogram = librosa.feature.melspectrogram(y=X, sr=sample_rate)
melspectrogram = np.mean(melspectrogram.T, axis=0)
# spectral_centroid
spectral_centroid = librosa.feature.spectral_centroid(y=X, sr=sample_rate)
spectral_centroid = np.mean(spectral_centroid.T, axis=0)
# spectral_contrast
spectral_contrast = librosa.feature.spectral_contrast(y=X, sr=sample_rate)
spectral_contrast = np.mean(spectral_contrast.T, axis=0)
feature = np.hstack((Mcc, chroma_stft, chroma_cqt,
melspectrogram, spectral_centroid, spectral_contrast))
x.append(feature)
def LabelEncoder(arr, le):
le.fit(arr)
Y = le.fit_transform(arr)
return Y
Y = np.array(['Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry' 'Angry'
'Angry' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy' 'Happy'
'Happy' 'Happy' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Neutral'
'Neutral' 'Neutral' 'Neutral' 'Neutral' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'
'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad' 'Sad'])
le = preprocessing.LabelEncoder()
Y = LabelEncoder(Y, le)
print(type(Y))
model_SVC = pickle.load(open('SVC.sav', 'rb'))
X = []
path = "/home/gaurav/Desktop/music-genre-classification-using-machine-learning-main/Angry.wav"
extract_features(X, path)
y_pre = model_SVC.predict(X)
le.inverse_transform(y_pre)
print(model_SVC.predict_proba(X))
a = model_SVC.predict_proba(X)
z = ['Angry', 'Happy', 'Neutral', 'Sad']
print(z[np.argmax(a)])