/
main.py
330 lines (301 loc) · 9.61 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import tkinter as tk
from keyword_spotting_service import Keyword_Spotting_Service
import pdfkit
import sys
import os
import datetime
from os import path
from tkinter import filedialog
from tkinter import messagebox
from pydub import AudioSegment
from pydub.utils import make_chunks
class MainWindow:
""" GUI Wrapper """
# configure root directory path relative to this file
THIS_FOLDER_G = ""
if getattr(sys, "frozen", False):
# frozen
THIS_FOLDER_G = os.path.dirname(sys.executable)
else:
# unfrozen
THIS_FOLDER_G = os.path.dirname(os.path.realpath(__file__))
def __init__(self, root):
self.root = root
self._pdf = None
self._file_url = tk.StringVar()
self._Dist_url = tk.StringVar()
self._status = tk.StringVar()
self._status.set("---")
root.title("Voice2Text")
root.configure(bg="#eeeeee")
try:
icon_img = tk.Image(
"photo",
file=self.THIS_FOLDER_G + "/assets/icon.png"
)
root.call(
"wm",
"iconphoto",
root._w,
icon_img
)
except Exception:
pass
self.menu_bar = tk.Menu(
root,
bg="#eeeeee",
relief=tk.FLAT
)
self.menu_bar.add_command(
label="How To",
command=self.show_help_callback
)
self.menu_bar.add_command(
label="Quit!",
command=root.quit
)
root.configure(
menu=self.menu_bar
)
self.file_entry_label = tk.Label(
root,
text="Enter Audio File Path Or Click SELECT FILE Button",
bg="#eeeeee",
anchor=tk.W
)
self.file_entry_label.grid(
padx=12,
pady=(8, 0),
ipadx=0,
ipady=1,
row=0,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.file_entry = tk.Entry(
root,
textvariable=self._file_url,
bg="#fff",
exportselection=0,
relief=tk.FLAT
)
self.file_entry.grid(
padx=15,
pady=6,
ipadx=8,
ipady=8,
row=1,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.select_btn = tk.Button(
root,
text="SELECT FILE",
command=self.selectfile_callback,
width=42,
bg="#1089ff",
fg="#ffffff",
bd=2,
relief=tk.FLAT
)
self.select_btn.grid(
padx=15,
pady=8,
ipadx=24,
ipady=6,
row=2,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.Dist_entry_label = tk.Label(
root,
text="Enter destination directory Path Or Click SELECT Directory Button",
bg="#eeeeee",
anchor=tk.W
)
self.Dist_entry_label.grid(
padx=12,
pady=(8, 0),
ipadx=0,
ipady=1,
row=3,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.Dist_entry = tk.Entry(
root,
textvariable=self._Dist_url,
bg="#fff",
exportselection=0,
relief=tk.FLAT
)
self.Dist_entry.grid(
padx=15,
pady=6,
ipadx=8,
ipady=8,
row=4,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.Dist_btn = tk.Button(
root,
text="SELECT Directory",
command=self.selectdist_callback,
width=42,
bg="#1089ff",
fg="#ffffff",
bd=2,
relief=tk.FLAT
)
self.Dist_btn.grid(
padx=15,
pady=8,
ipadx=24,
ipady=6,
row=5,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.convert_btn = tk.Button(
root,
text="Convert",
command=self.convert_callback,
bg="#00bd56",
fg="#ffffff",
bd=2,
relief=tk.FLAT
)
self.convert_btn.grid(
padx=15,
pady=(4, 12),
ipadx=24,
ipady=6,
row=7,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.reset_btn = tk.Button(
root,
text="RESET",
command=self.reset_callback,
bg="#aaaaaa",
fg="#ffffff",
bd=2,
relief=tk.FLAT
)
self.reset_btn.grid(
padx=15,
pady=(4, 12),
ipadx=24,
ipady=6,
row=8,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
self.status_label = tk.Label(
root,
textvariable=self._status,
bg="#eeeeee",
anchor=tk.W,
justify=tk.LEFT,
relief=tk.FLAT,
wraplength=350
)
self.status_label.grid(
padx=12,
pady=(0, 12),
ipadx=0,
ipady=1,
row=9,
column=0,
columnspan=4,
sticky=tk.W + tk.E + tk.N + tk.S
)
tk.Grid.columnconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 1, weight=1)
tk.Grid.columnconfigure(root, 2, weight=1)
tk.Grid.columnconfigure(root, 3, weight=1)
def selectfile_callback(self):
try:
name = filedialog.askopenfile(title='Please select audio file',
filetypes=[('Audio Files', ['.wav'])])
self._file_url.set(name.name)
# print(name.name)
except Exception as e:
self._status.set(e)
self.status_label.update()
def selectdist_callback(self):
try:
name = filedialog.askdirectory(
title='Please select Destination Directory', )
self._Dist_url.set(name)
# print(name.name)
except Exception as e:
self._status.set(e)
self.status_label.update()
def freeze_controls(self):
self.file_entry.configure(state="disabled")
self.select_btn.configure(state="disabled")
self.Dist_btn.configure(state="disabled")
self.convert_btn.configure(state="disabled")
self.reset_btn.configure(state="disabled")
self.status_label.update()
def unfreeze_controls(self):
self.file_entry.configure(state="normal")
self.select_btn.configure(state="normal")
self.Dist_btn.configure(state="normal")
self.convert_btn.configure(state="normal")
self.reset_btn.configure(state="normal")
self.status_label.update()
def reset_callback(self):
self._pdf = None
self._file_url.set("")
self._Dist_url.set("")
self._status.set("---")
def convert_callback(self):
if not self._file_url.get():
self._status.set("please choose audio file")
elif not self._Dist_url.get():
self._status.set("please choose Destination directory")
else:
self._status.set("processing")
self.freeze_controls()
AUDIO_FILE = str(self._file_url.get())
filename = "game_log_" + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".pdf"
destination = self._Dist_url.get()
kss = Keyword_Spotting_Service()
myaudio = AudioSegment.from_file(AUDIO_FILE , "wav")
chunk_length_ms = 1000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) # Make chunks of one sec
# Export all of the individual chunks as wav files
predictedSentence = ""
for i, chunk in enumerate(chunks):
chunk_name = "sample_AI\chunk{0}.wav".format(i)
chunk.export(chunk_name, format="wav")
for i, chunk in enumerate(chunks):
predictedSentence += kss.predict("sample_AI\chunk"+str(i)+".wav")+" "
self._pdf = pdfkit.from_string("prediction is: " + predictedSentence,
path.join(destination, filename))
self._status.set("Log file generated!")
self.unfreeze_controls()
def show_help_callback(self):
messagebox.showinfo(
"How To",
"""1. Open the App and Click SELECT FILE Button and select your file e.g. "abc.wav".
2. Select where output file should be generated.
3. Click Convert Button to Start the converting. A new PDF file named "match_log_datetime.pdf" will be created in the selected directory.
5. Click RESET Button to reset the input fields and status bar."""
)
if __name__ == "__main__":
ROOT = tk.Tk()
MAIN_WINDOW = MainWindow(ROOT)
ROOT.mainloop()