-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_window.py
236 lines (204 loc) · 8.74 KB
/
main_window.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
233
234
235
236
# coding=utf-8
"""
Copyright (c) 2014 Zdeněk Janeček
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"""
import pickle
import os
import numpy as np
from gi.repository import Gtk
from file_tool import FileTool
from classifier import Classifier
from features import mfcc
from VoiceActivityDetection import simpleVAD
class MainWindow:
SR = 16000
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("app.glade")
self.builder.connect_signals(self)
self.window = self.builder.get_object("window")
self.file_store = self.builder.get_object("filestore")
self.file_view = self.builder.get_object("fileview")
self.status_label = self.builder.get_object("status")
self.run_indicator = self.builder.get_object("running")
self.audiofilter = self.builder.get_object("audiofilter")
self.classcombo = self.builder.get_object("classcombo")
self.aboutdialog = self.builder.get_object("aboutdialog")
self.file_dialog = Gtk.FileChooserDialog(title="Otevřít", transient_for=self.window, action=Gtk.FileChooserAction.OPEN)
self.file_dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
self.file_dialog.set_default_response(Gtk.ResponseType.OK)
self.file_dialog.set_select_multiple(True)
if os.name != 'nt':
self.audiofilter.set_name("Zvuk")
self.file_dialog.add_filter(self.audiofilter)
self.last_dir = None
# all_files tuple of Files
self.all_files = []
# sel_files: indexes to the all_files tuple
self.sel_files = []
self.X = []
self.y = []
self.class_names = {}
try:
with open('dataset.pickle', 'rb') as f:
(self.X, self.y, self.class_names) = pickle.load(f)
for name in self.class_names.items():
self.classcombo.append(str(name[0]), name[1])
ok = Classifier.new_training(self.X, self.y)
if ok:
self.status_label.set_text("{0} vzorků, {1} tříd".format(len(self.X), len(self.class_names)))
except IOError as e:
pass
self.window.show_all()
def open_button_clicked_cb(self, button):
"""Open button callback
:param button: signal came from this button
"""
if self.last_dir is not None:
self.file_dialog.set_current_folder_uri(self.last_dir)
response = self.file_dialog.run()
self.file_dialog.hide()
if response == Gtk.ResponseType.OK:
self.last_dir = self.file_dialog.get_current_folder_uri()
file_names = self.file_dialog.get_filenames()
else:
return
self.run_indicator.start()
self.window.set_sensitive(False)
self.counter = len(file_names)
for f in file_names: # update file_store
row = self.file_store.iter_n_children(None)
self.file_store.append([os.path.basename(f), "N/A"])
fobj = FileTool(f);
fobj.load_file(MainWindow.SR, row, self.update_classify_progress_cb)
self.all_files.append(fobj)
def del_button_clicked_cb(self, button):
"""Delete button callback
:param button: signal came from this button
"""
if not len(self.sel_files):
return
# delete first item, this list will be updated imediatelly
for i in range(len(self.sel_files)):
row = self.sel_files[0]
del_iter = self.file_store.get_iter(Gtk.TreePath.new_from_indices([row]))
self.file_store.remove(del_iter)
del self.all_files[row]
def play_button_clicked_cb(self, button):
"""Play button callback
:param button: signal came from this button
"""
if not len(self.sel_files):
return
self.all_files[self.sel_files[0]].play()
def cl_button_clicked_cb(self, button):
"""Classify button callback
:param button: signal came from this button
"""
if not len(self.sel_files):
return
self.counter = -1
for row in self.sel_files:
Classifier.classify(self.all_files[row], MainWindow.SR, row, self.update_classify_progress_cb)
def about_button_clicked_cb(self, button):
"""Classify button callback
:param button: signal came from this button
"""
self.aboutdialog.run()
self.aboutdialog.hide()
def fileview_selection_changed_cb(self, tree_selection):
"""Table selection callback
:param tree_selection: signal came from this button
"""
(model, pathlist) = tree_selection.get_selected_rows()
self.sel_files = []
for path in pathlist:
self.sel_files += path.get_indices()
# učí
def classcombo_changed_cb(self, entry):
"""Combobox list callback
:param entry: signal came from this button
"""
class_id = entry.get_active()
if class_id == -1:
return
self.fill(class_id)
self.classcombo.get_child().set_text("")
# vytvoří třídu
def classcombo_entry_activated_cb(self, entry):
"""Combobox entry update callback
:param entry: signal came from this button
"""
class_desc = entry.get_text()
# add only new classes
if class_desc in self.class_names.values():
return
class_id = len(self.class_names)
self.class_names[len(self.class_names)] = class_desc
self.classcombo.remove_all()
for name in self.class_names.items():
self.classcombo.append(str(name[0]), name[1])
self.fill(class_id)
def fill(self, class_id):
"""Fills internal structer with new training samples.
Do not call directly.
:param class_id: class identification
"""
# get training samples
for i in range(len(self.sel_files)):
row = self.sel_files[0]
samples = self.all_files[row].samples
feat = mfcc(samples, 16000, winlen=0.030, appendEnergy=False, VAD=simpleVAD)
# add two symptoms from the middle
self.X.append(feat[int(len(feat) / 2 - 1)])
self.y.append(class_id)
self.X.append(feat[int(len(feat) / 2 + 1)])
self.y.append(class_id)
# clear from the list
del_iter = self.file_store.get_iter(Gtk.TreePath.new_from_indices([row]))
self.file_store.remove(del_iter)
del self.all_files[row]
# print results
if Classifier.new_training(self.X, self.y):
self.status_label.set_text("{0} vzorků, {1} tříd".format(len(self.X), len(self.class_names)))
else:
self.status_label.set_text("Klasifikátor potřebuje víc tříd")
def update_classify_progress_cb(self, row, state, message):
"""Callback function for classifier, file loader or any other caller.
Must be done by UI thread
:param row: actual updated row
:param state: indicated state of caller
:param message: message from caller
"""
new_iter = self.file_store.get_iter(Gtk.TreePath.new_from_indices([row]))
if state == Classifier.LOADING:
self.file_store[new_iter][1] = "Načítám"
elif state == Classifier.CLASSIFIED:
self.file_store[new_iter][1] = self.class_names[message]
elif state == Classifier.ERROR:
self.status_label.set_text("ERROR: " + message)
self.run_indicator.stop()
self.window.set_sensitive(True)
elif state is None:
self.file_store[new_iter][1] = message
self.counter -= 1
if self.counter == 0:
self.run_indicator.stop()
self.window.set_sensitive(True)
def destroy(self, window):
"""GTK callback when is window closed. Time to save application state.
:param window: actual closed window
"""
with open('dataset.pickle', 'wb') as f:
pickle.dump((self.X, self.y, self.class_names), f, pickle.HIGHEST_PROTOCOL)
Gtk.main_quit()
def main(self):
Gtk.main()