-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
234 lines (166 loc) · 8.68 KB
/
gui.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
import sys
from datetime import datetime
import tkinter.font
from tkinter import*
from datetime import datetime
from PIL import Image, ImageTk
import threading
from random import *
import openpyxl
import matplotlib.pyplot as plt
import time
import speech_recognition as sr
import pyaudio
import wave
from pydub.playback import play
from pydub import AudioSegment
import numpy as np
import struct
import time
sr.__version__
import tkinter.ttk
FORMAT = pyaudio.paInt16
LEN = 10**100
PASS = 5
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 3
MIN_STRING_LIST_LENGTH = 9
WAVE_OUTPUT_FILENAME = "./data/wav/file.wav"
class Gui() :
def __init__(self):
self.window = tkinter.Tk()
self.font = tkinter.font.Font(family = "나눔스퀘어 Bold", size = 24)
self.font4 = tkinter.font.Font(family = "나눔스퀘어 Regular", size = 18)
self.font5 = tkinter.font.Font(family = "나눔스퀘어 Regular", size = 15)
self.font6 = tkinter.font.Font(family = "나눔스퀘어 Light", size = 12)
self.window.title("Kwangwoon Univ. Chambit Design Project (GWAENGSU(괭수) - STC(Speech to Code) Program)")
self.window.geometry("1090x800")
self.window.resizable(False, False)
icon = PhotoImage(file='icon/dialogue.png')
self.window.iconphoto(False, icon)
self.show_outer_line()
self.show_voice_listbox()
self.show_label()
self.show_button()
self.show_combobox()
self.show_checkbox()
t = threading.Thread(target=self.voice_to_text)
t.start()
self.window.mainloop()
def show_label(self):
self.label_voice=tkinter.Label(self.window, text="TEXT",font = self.font, fg = 'black')
self.label_voice.place(x = 510, y = 110, width = 90, height = 30)
self.label_code=tkinter.Label(self.window, text="CODE",font = self.font, fg = 'black')
self.label_code.place(x = 860, y = 110, width = 90, height = 30)
label_convert=tkinter.Label(self.window, text="convert",font = self.font4, fg = 'grey')
label_convert.place(x = 420, y = 45, width = 90, height = 30)
label_record=tkinter.Label(self.window, text="record",font = self.font4, fg = 'grey')
label_record.place(x = 60, y = 45, width = 90, height = 30)
label_langugae=tkinter.Label(self.window, text="language",font = self.font4, fg = 'grey')
label_langugae.place(x = 60, y = 375, width = 100, height = 30)
label_software=tkinter.Label(self.window, text="software",font = self.font4, fg = 'grey')
label_software.place(x = 60, y = 515, width = 100, height = 30)
arrow = PhotoImage(file="icon/next.png")
self.arrow = Label(image=arrow, height=40)
self.arrow.image = arrow
self.arrow.place(x = 710, y = 400, width = 45, height = 50)
def show_voice_listbox(self) :
#scrollbar=tkinter.Scrollbar(self.window,relief='solid',bd = 4)
#scrollbar.place(x = 160, y = 100, width = 30, height = 80)
self.voice_listbox=tkinter.Listbox(self.window, relief='groove', bd=2, font=self.font5)
self.voice_listbox.place(x = 405, y = 150, width = 300, height = 610)
self.code_listbox=tkinter.Listbox(self.window, relief='groove', bd=2, font=self.font5)
self.code_listbox.place(x = 755, y = 150, width = 300, height = 610)
def show_outer_line(self):
outer_line1=tkinter.Label(self.window, relief="groove",bd = 2)
outer_line1.place(x = 390, y = 60, width = 680, height = 720)
outer_line2=tkinter.Label(self.window, relief="groove",bd = 2)
outer_line2.place(x = 30, y = 60, width = 330, height = 300)
outer_line3=tkinter.Label(self.window, relief="groove",bd = 2)
outer_line3.place(x = 30, y = 385, width = 330, height = 120)
outer_line4=tkinter.Label(self.window, relief="groove",bd = 2)
outer_line4.place(x = 30, y = 530, width = 330, height = 150)
def show_button(self) :
self.button_start = tkinter.Button(self.window, relief="raised" ,repeatdelay=1000, repeatinterval=1000, \
bg = 'white',bd = 3,text = "start",font = self.font, highlightcolor = 'grey')
self.button_start.place(x= 65,y = 100,width = 255,height = 50)
self.button_stop = tkinter.Button(self.window, relief="raised" ,repeatdelay=1000, repeatinterval=1000, \
bg = 'white',bd = 3,text = "stop",font = self.font, highlightcolor = 'grey')
self.button_stop.place(x= 65,y = 180,width = 255,height = 50)
self.button_play = tkinter.Button(self.window, relief="raised" ,repeatdelay=1000, repeatinterval=1000, \
bg = 'white',bd = 3,text = "play",font = self.font)
self.button_play.place(x= 65,y = 260,width = 255,height = 50)
self.button_send = tkinter.Button(self.window, relief="groove" ,repeatdelay=1000, repeatinterval=1000, \
bd = 3,text = "send",font = self.font4)
self.button_send.place(x= 170,y = 675,width = 150,height = 30)
self.delete=tkinter.Button(self.window, text="새로고침", font=self.font6, command=self.deleteText)
self.delete.place(x=30, y=720, width=70, height=40)
def getText(self):
self.result1 = self.voice_listbox.get(0, "end")
self.result2 = self.code_listbox.get(0, "end")
return self.result1+self.result2
def deleteText(self):
result = self.getText()
for i in result:
self.voice_listbox.delete(0, "end")
self.code_listbox.delete(0, "end")
def show_combobox(self) :
values1 = [" Python"," C", " Java"]
combobox1=tkinter.ttk.Combobox(self.window, height=10, values=values1, font = self.font3)
combobox1.place(x= 65,y = 440,width = 255,height = 50)
combobox1.set(" Python")
values2 = [" Jupyter Notebook"," Pycharm"]
combobox2=tkinter.ttk.Combobox(self.window, height=10, values=values2, font = self.font4)
combobox2.place(x= 65,y = 610,width = 255,height = 50)
combobox2.set(" Jupyter Notebook")
def show_checkbox(self) :
checkVar1=tkinter.IntVar()
ckeck_box=tkinter.Checkbutton(self.window,text=" auto",variable=checkVar1,font = self.font4)
ckeck_box.place(x = 60, y = 670)
def voice_to_text(self):
while(1):
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=pyaudio.paInt16,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=1,
frames_per_buffer=CHUNK)
frames, string_list = [], []
for i in range(LEN):
data = stream.read(CHUNK)
frames.append(data)
string = np.frombuffer(data, np.int16)[0]
string_list.append(string)
# stop Recording
if string == 0 and i > PASS:
break
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
if len(string_list) > MIN_STRING_LIST_LENGTH:
r = sr.Recognizer()
korean_audio = sr.AudioFile("./data/wav/file.wav")
with korean_audio as source:
mandarin = r.record(source)
try :
sentence = r.recognize_google(audio_data=mandarin, language="ko-KR")
self.voice_listbox.insert(END,sentence)
print(sentence)
if sentence in '종료':
break
except:
print('*** 다시 말해주세요 ***')
def voice_play(self):
audio_file = AudioSegment.from_file(file="./data/wav/file.wav")
play(audio_file)
Gui()