-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
232 lines (183 loc) · 8.36 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from python_speech_features import mfcc
import scipy.io.wavfile as wav
import numpy as np
import os
import pickle
import random
import operator
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import seaborn as sns
import streamlit as st
import os
from collections import defaultdict
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import os
client_id = os.environ.get('CLIENT_ID')
client_secret = os.environ.get('CLIENT_SECRET')
redirect_uri = os.environ.get('REDIRECT_URI')
# Streamlit UI
st.title("Audio Detection & Classification")
# Option 2: Select or Drop Audio
uploaded_file = st.file_uploader("Select or Drop Audio", type=["wav"])
if uploaded_file:
st.audio(uploaded_file, format="audio/wav", start_time=0)
# Option to predict genre for the new audio file
if st.button("Predict Genre"):
st.write("Engine is starting...")
# Function to plot confusion matrix
def plot_confusion_matrix(y_true, y_pred, classes):
cm = confusion_matrix(y_true, y_pred)
fig, ax = plt.subplots(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=classes, yticklabels=classes)
ax.set_title("Confusion Matrix")
ax.set_xlabel("Predicted")
ax.set_ylabel("True")
st.pyplot(fig)
# function to get the distance between feature vecotrs and find neighbors
def getNeighbors(trainingSet, instance, k):
distances = []
for x in range(len(trainingSet)):
dist = distance(trainingSet[x], instance, k) + distance(instance, trainingSet[x], k)
distances.append((trainingSet[x][2], dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
# identify the class of the instance
def nearestClass(neighbors):
classVote = {}
for x in range(len(neighbors)):
response = neighbors[x]
if response in classVote:
classVote[response] += 1
else:
classVote[response] = 1
sorter = sorted(classVote.items(), key = operator.itemgetter(1), reverse=True)
return sorter[0][0]
# function to evaluate the model
def getAccuracy(testSet, predictions):
correct = 0
for x in range(len(testSet)):
if testSet[x][-1] == predictions[x]:
correct += 1
return (1.0 * correct) / len(testSet)
# directory that holds the wav files
directory = "Data/genres_original/"
results = defaultdict(int)
i = 1
for folder in os.listdir(directory):
results[i] = folder
i += 1
# st.write(results)
# binary file where we will collect all the features extracted using mfcc (Mel Frequency Cepstral Coefficients)
f = open("mydataset.dat", 'wb')
i = 0
for folder in os.listdir(directory):
i += 1
if i == 11:
break
for file in os.listdir(directory+folder):
try:
(rate, sig) = wav.read(directory+folder+"/"+file)
mfcc_feat = mfcc(sig, rate, winlen=0.020, appendEnergy=False)
covariance = np.cov(np.matrix.transpose(mfcc_feat))
mean_matrix = mfcc_feat.mean(0)
feature = (mean_matrix, covariance, i)
pickle.dump(feature, f)
except Exception as e:
print('Got an exception: ', e, ' in folder: ', folder, ' filename: ', file)
f.close()
# Split the dataset into training and testing sets respectively
dataset = []
# Function to load dataset
def load_dataset(filename, split, tr_set, te_set):
with open(filename, 'rb') as f:
while True:
try:
dataset.append(pickle.load(f))
except EOFError:
f.close()
break
for x in range(len(dataset)):
if random.random() < split:
tr_set.append(dataset[x])
else:
te_set.append(dataset[x])
trainingSet = []
testSet = []
load_dataset('my.dat', 0.66, trainingSet, testSet)
def distance(instance1 , instance2 , k ):
distance =0
mm1 = instance1[0]
cm1 = instance1[1]
mm2 = instance2[0]
cm2 = instance2[1]
distance = np.trace(np.dot(np.linalg.inv(cm2), cm1))
distance+=(np.dot(np.dot((mm2-mm1).transpose() , np.linalg.inv(cm2)) , mm2-mm1 ))
distance+= np.log(np.linalg.det(cm2)) - np.log(np.linalg.det(cm1))
distance-= k
return distance
# making predictions using KNN
leng = len(testSet)
predictions = []
for x in range(leng):
predictions.append(nearestClass(getNeighbors(trainingSet, testSet[x], 5)))
accuracy1 = getAccuracy(testSet, predictions)
st.write(accuracy1)
# Function to evaluate the model and show results
def evaluate_model(test_set, predictions, classes, results):
accuracy = getAccuracy(test_set, predictions)
st.write(f"Accuracy: {accuracy}")
y_true = [instance[-1] for instance in test_set]
plot_confusion_matrix(y_true, predictions, classes)
classification_rep = classification_report(y_true, predictions, target_names=classes)
# Display each line of the classification report separately
st.text("Classification Report:")
for line in classification_rep.split('\n'):
st.text(line)
evaluate_model(testSet, predictions, classes=list(results.values()), results=results)
# Function to plot audio waves
def plot_audio_wave(signal, rate, title="Audio Wave"):
fig, ax= plt.subplots(figsize=(10, 4))
time = np.arange(0, len(signal)) / rate
ax.plot(time, signal)
ax.set_title(title)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Amplitude")
st.pyplot(fig)
plot_audio_wave(sig, rate, title="Test Audio Wave")
st.write("Genre for the uploaded file")
(rate, sig) = wav.read(uploaded_file)
mfcc_feat = mfcc(sig, rate, winlen=0.020, appendEnergy=False)
covariance = np.cov(np.matrix.transpose(mfcc_feat))
mean_matrix = mfcc_feat.mean(0)
feature = (mean_matrix, covariance, i)
pred = nearestClass(getNeighbors(dataset, feature, 5))
st.write(results[pred])
predicted_genre = results[pred]
# Authenticate with Spotify API
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))
# Recommend playlists based on the specified genre
recommended_playlists = []
playlists = sp.category_playlists(category_id=predicted_genre, country='US', limit=3)
recommended_playlists.extend(playlists['playlists']['items'])
# Display logo and text in the same row
col1, col2 = st.columns([1, 3])
# Column 1: Logo
spotify_logo_path = "logo.png"
col1.image(spotify_logo_path, width=80)
# Column 2: Text
col2.subheader(f"Recommended Playlists for Genre : {predicted_genre}")
for playlist in recommended_playlists:
col1, col2 = st.columns(2)
with col1:
st.header(playlist['name'])
with col2:
st.subheader("Playlist Details :")
st.write(f"Playlist ID : {playlist['id']}")
playlist_url = f"https://open.spotify.com/playlist/{playlist['id']}"
st.markdown(f"[Open Playlist on Spotify]({playlist_url})")
st.markdown("---")