-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudio_Classifier.py
More file actions
199 lines (161 loc) · 7.79 KB
/
Audio_Classifier.py
File metadata and controls
199 lines (161 loc) · 7.79 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
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
# Authors: Stephanie Murray and Breanna Powell
# CSS 584: Assignment 4, Option 1 - Classifying audio as music (yes) or speech (no)
###########
# To Run #
###########
# Option 1
# Create a virtual environment
# Activate the virtual environment
# Run "python install_packages.py" to install the necessary packages
# Option 2 (If you didn't use option 1)
# Open Anaconda Navigator
# Open Visual Studio Code through Anaconda
# Navigate to and open the folder called "audio_processing"
# Open the terminal window
# Install the following packages:
# $pip install pygame
# $pip install librosa
# $pip install sklearn
# Other packages should be installed already
# Use the following command to run:
# $python Audio_Classifier.py
import os
import tkinter as tk
from tkinter import filedialog, Listbox, messagebox
import pygame
import pandas as pd
from feature_extraction.Feature_Extractor import generate_csv
from classifier.classifier import train_model
import joblib
class AudioClassifierGUI:
def __init__(self, root):
self.results = []
self.root = root
root.title("Audio Classifier by S. Murray and B. Powell")
window_width = 580
window_height = 650
# Get screen dimensions
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Calculate x and y coordinates for the Tk root window
x = (screen_width/2) - (window_width/2)
y = (screen_height/2) - (window_height/2)
# Set the dimensions of the screen place it in middle
root.geometry('%dx%d+%d+%d' % (window_width, window_height, x, y))
# Initialize pygame for audio playback
pygame.mixer.init()
# Create frames
top_frame = tk.Frame(root)
bottom_frame = tk.Frame(root)
top_frame.grid(row=0, column=0, padx=10, pady=10, sticky="ew")
bottom_frame.grid(row=1, column=0, padx=10, pady=10, sticky="ew")
# Top Frame Components
self.train_label = tk.Label(top_frame, text="Training Files")
self.train_label.pack(side=tk.LEFT, padx=10)
self.train_listbox = Listbox(top_frame, bg='light gray', width=50, height=20)
self.train_listbox.pack(side=tk.LEFT, fill=tk.Y)
self.train_listbox.bind("<Button-1>", lambda event: "break")
top_button_frame = tk.Frame(top_frame)
top_button_frame.pack(side=tk.LEFT, fill=tk.Y, padx=10)
self.load_button = tk.Button(top_button_frame, text="Load Folder of .wav files", command=self.load_folder)
self.load_button.pack()
# Bottom Frame Components
self.test_label = tk.Label(bottom_frame, text="Testing Files")
self.test_label.pack(side=tk.LEFT, padx=10)
self.test_listbox = Listbox(bottom_frame, width=50, height=15)
self.test_listbox.pack(side=tk.LEFT, fill=tk.Y)
bottom_button_frame = tk.Frame(bottom_frame)
bottom_button_frame.pack(side=tk.LEFT, fill=tk.Y, padx=10)
self.test_button = tk.Button(bottom_button_frame, text="Test Selected File", command=self.test_file)
self.test_button.pack(pady=5)
self.play_button = tk.Button(bottom_button_frame, text="Play Selected File", command=self.play_audio)
self.play_button.pack(pady=5)
# Attributes
self.model = None
self.file_paths = {}
self.train_files = []
self.test_files = []
def load_folder(self):
folder_path = filedialog.askdirectory()
messagebox.showinfo("Please Wait", "Loading files. Please wait a few seconds...")
if folder_path:
# Populate file_paths dictionary
for root, dirs, files in os.walk(folder_path):
for file in files:
full_path = os.path.join(root, file)
self.file_paths[file] = full_path
self.extract_features_and_train(folder_path)
#self.root.after(2000, lambda: messagebox.showinfo("Loading Complete", "Files loaded successfully!"))
def extract_features_and_train(self, folder_path):
generate_csv(folder_path)
x_train, x_test, y_train, y_test, self.model, self.train_files, self.test_files, self.test_labels = train_model()
# Save the model for later use
joblib.dump(self.model, 'audio_classifier_model.pkl')
self.update_file_lists()
def update_file_lists(self):
# Clear existing lists
self.train_listbox.delete(0, tk.END)
self.test_listbox.delete(0, tk.END)
# Populate training files list (non-clickable)
for file in self.train_files:
self.train_listbox.insert(tk.END, file)
# Populate testing files list (clickable)
for file in self.test_files:
self.test_listbox.insert(tk.END, file)
def test_file(self):
selected_file = self.test_listbox.get(tk.ACTIVE)
if selected_file:
# Load the model for testing
self.model = joblib.load('audio_classifier_model.pkl')
scaler = joblib.load('scaler.pkl')
if selected_file in self.test_labels:
# Get the ground truth label from the dictionary
ground_truth = "Music" if self.test_labels[selected_file] == "yes" else "Speech"
# Load the features from the CSV file
features_data = pd.read_csv('feature_extraction/features.csv')
# Find the row corresponding to the selected file
selected_row = features_data[features_data['fileName'] == selected_file]
print("Selected Row:")
print(selected_row)
if not selected_row.empty:
# Extract the features for the selected file
features = selected_row.drop(["Label", "fileName"], axis=1)
normalized_features = scaler.transform(features)
# Debugging message to check the loaded features
#print("Loaded Features:")
#print(features)
# print("Columns:")
#print(features_data.columns)
#print("Normalized Features:")
#print(normalized_features)
# Predict whether the audio file contains speech or music
prediction = self.model.predict(normalized_features)
# Debugging message to check the prediction
#print("Prediction:")
#print(prediction)
self.results.append((selected_file, prediction[0], ground_truth))
# Convert the prediction to a readable label
predicted_label = "Music" if prediction[0] == 1 else "Speech"
# Display the results
messagebox.showinfo("Result", f"File: {selected_file}\nGround Truth: {ground_truth}\nPrediction: {predicted_label}")
else:
messagebox.showerror("Error", "Selected file not found in the feature data")
else:
messagebox.showerror("Error", "Ground truth label not found for selected file")
else:
messagebox.showerror("Error", "No file selected")
def play_audio(self):
selected_file = self.test_listbox.get(tk.ACTIVE)
if selected_file and selected_file in self.file_paths:
file_path = self.file_paths[selected_file]
# Play the audio file using pygame
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def print_summary(self):
print("File, Model Output, Ground Truth Label")
for file_name, model_output, ground_truth in self.results:
print(f"{file_name}, Model output: {model_output}, Ground truth label: {ground_truth}")
root = tk.Tk()
gui = AudioClassifierGUI(root)
root.mainloop()
gui.print_summary()