forked from bongtavas/Speech-Recognition-ANN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
130 lines (106 loc) · 4 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
from threading import Thread
from record import record_to_file
from features import mfcc
from anntrainer import *
from featureExtractor import *
from anntester_single import *
import scipy.io.wavfile as wav
from tkinter import *
class Application(Frame):
def createWidgets(self):
self.button_image = PhotoImage(file="button.gif")
self.RECORD = Button(self, image=self.button_image, width="100",
height="100", command=self.record_buttonpress)
self.RECORD.pack()
self.TEXTBOX = Text(self, height="1", width="30")
self.TEXTBOX.pack()
# for train record button
self.button_image2 = PhotoImage(file="button.gif")
self.RECORD2 = Button(self, image=self.button_image2, width="100",
height="100", command=self.record2_buttonpress)
self.RECORD2.pack()
self.TEXTBOX2 = Text(self, height="1", width="30")
self.TEXTBOX2.pack()
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.TEXTBOX.insert(INSERT, "Press to record")
self.TEXTBOX.tag_config(
"recording", foreground="red", justify="center")
self.TEXTBOX.tag_config(
"success", foreground="red", justify="center")
self.TEXTBOX.configure(state="disabled")
self.TEXTBOX2.insert(INSERT, "Press to record for train dataset")
self.TEXTBOX2.tag_config(
"recording", foreground="red", justify="center")
self.TEXTBOX2.tag_config(
"success", foreground="darkgreen", justify="center")
self.TEXTBOX2.configure(state="disabled")
def record_buttonpress(self): # TEST BUTTON
recorder_thread = Thread(
target=record_and_test, args=(self.TEXTBOX, self.RECORD))
recorder_thread.start()
def record2_buttonpress(self): # TRAIN BUTTON
recorder_thread = Thread(
target=record_and_train, args=(self.TEXTBOX2, self.RECORD2))
recorder_thread.start()
def record_and_train(textbox, button):
# button on
button.configure(state="disabled")
textbox.configure(state="normal")
textbox.delete("1.0", END)
textbox.insert(INSERT, "Recording")
textbox.tag_add("recording", "1.0", END)
textbox.configure(state="disabled")
words = ['down', 'eat', 'sleep', 'up']
for i in range(len(words)): # WORD loop
for j in range(10): # REPEAT loop
# record to train
record_to_file("training_sets/" +
words[i] + "-" + str(j+1) + ".wav")
print("repeat", words[i])
if(len(words) != i+1):
print("next word", words[i+1])
else:
print("finish")
# Button of
textbox.configure(state="normal")
textbox.delete("1.0", END)
textbox.tag_remove("recording", "1.0")
textbox.tag_add("success", "1.0", END)
textbox.configure(state="disabled")
button.configure(state="normal")
# MFCC calculation and TRAIN
mfcc_apply()
print("train start..")
TRAIN()
def record_and_test(textbox, button, filename="test_files/test.wav"):
# Buton on
button.configure(state="disabled")
textbox.configure(state="normal")
textbox.delete("1.0", END)
textbox.insert(INSERT, "Recording")
textbox.tag_add("recording", "1.0", END)
textbox.configure(state="disabled")
# Record to test
record_to_file(filename)
# Feed into ANN
testNet = testInit()
inputArray = extractFeature(filename)
print(len(inputArray))
outStr = feedToNetwork(inputArray, testNet)
# Button of
textbox.configure(state="normal")
textbox.delete("1.0", END)
textbox.tag_remove("recording", "1.0")
textbox.insert(INSERT, outStr)
textbox.tag_add("success", "1.0", END)
textbox.configure(state="disabled")
button.configure(state="normal")
if __name__ == '__main__':
# Display GUI
root = Tk()
app = Application(master=root)
app.mainloop()
# root.destroy()