-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
296 lines (223 loc) · 9.37 KB
/
main.py
File metadata and controls
296 lines (223 loc) · 9.37 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
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
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMenu
from PyQt5.QtGui import QIcon
import pyaudio
import sys
import multiprocessing
import os
import threading
import traceback
from modules.audio_io import AudioInputOutput
from modules.recognizer import Recognizer
from modules.Translator import Translator
from modules.Configure import Configure
from modules.Logger import Logger
from ui.Settings import Ui_SettingsWindow
from ui.ScreenOutput import Ui_Output_Ui
from ui.Tray import SystemTray
logger = Logger('log_records.csv')
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(StoppableThread, self).__init__(*args, **kwargs)
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
class Ui_Output(Ui_Output_Ui):
def closeEvent(self, event):
try:
config.update('Output', 'x', str(self.pos().x()))
config.update('Output', 'y', str(self.pos().y()))
config.update('Output', 'width', str(self.size().width()))
config.update('Output', 'height', str(self.size().height()))
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
class Ui_Settings(Ui_SettingsWindow):
def fill_data(self):
try:
pa = pyaudio.PyAudio()
info = pa.get_host_api_info_by_index(0)
num_devices = info.get('deviceCount')
self.devices = {}
for i in range(0, num_devices): # Получаю все устройства, что подключены для ввода аудио
if (pa.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
self.devices[i] = pa.get_device_info_by_host_api_device_index(0, i).get('name')
for device in self.devices.keys(): # добавляю все аудио устройства
self.deviceComboBox.addItem(self.devices[device])
for i in os.listdir('./resources/models'): # Прохожусь по названиям всех файлов в папке
if i.find('.') == -1: # Если в названии нет ".", то добавляю в список
self.modelComboBox.addItem(i)
for lang in self.langs.keys(): # Добавляю все языки
self.translateFromComboBox.addItem(lang)
self.translateToComboBox.addItem(lang)
config.reload()
if config.read('General', 'activate') == '1':
self.activeCheckBox.setChecked(True)
else:
self.activeCheckBox.setChecked(False)
if config.read('Translate', 'translate') == '1':
self.translateCheckBox.setChecked(True)
else:
self.translateCheckBox.setChecked(False)
self.deviceComboBox.setCurrentText(self.devices[int(config.read('General', 'device_id'))])
self.modelComboBox.setCurrentText(config.read('General', 'recognition_model'))
self.translateFromComboBox.setCurrentText(config.read('Translate', 'translate_from'))
self.translateToComboBox.setCurrentText(config.read('Translate', 'translate_to'))
self.fontComboBox.setCurrentText(config.read('Output', 'font_family'))
self.fontSpinBox.setValue(int(config.read('Output', 'font_size')))
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
def save_data(self):
try:
global general
global translate
config.reload()
if self.activeCheckBox.isChecked():
config.update('General', 'activate', '1')
general.value['activate'] = '1'
else:
config.update('General', 'activate', '0')
general.value['activate'] = '0'
if self.translateCheckBox.isChecked():
config.update('Translate', 'translate', '1')
translate.value['translate'] = '1'
else:
config.update('Translate', 'translate', '0')
translate.value['translate'] = '0'
device_id = list(self.devices.keys())[list(self.devices.values()).index(self.deviceComboBox.currentText())]
config.update('General', 'device_id', str(device_id))
recognition_model = self.modelComboBox.currentText()
config.update('General', 'recognition_model', recognition_model)
translate_from = self.translateFromComboBox.currentText()
config.update('Translate', 'translate_from', translate_from)
translate_to = self.translateToComboBox.currentText()
config.update('Translate', 'translate_to', translate_to)
config.update('Output', 'font_family', self.fontComboBox.currentText())
config.update('Output', 'font_size', str(self.fontSpinBox.value()))
general.value = {
'activate': config.read('General', 'activate'),
'device_id': str(device_id),
'recognition_model': recognition_model
}
translate.value = {
'translate': config.read('Translate', 'translate'),
'translate_from': Ui_Settings.langs[translate_from],
'translate_to': Ui_Settings.langs[translate_to]
}
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
def customizePshButton_pressed(self):
try:
global output
output = Ui_Output(False, './resources/icon.png')
output.setText('text', int(config.read('Output', 'font_size')), config.read('Output', 'font_family'))
output.resize(int(config.read('Output', 'width')), int(config.read('Output', 'height')))
output.move(int(config.read('Output', 'x')), int(config.read('Output', 'y')))
output.show()
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
class Tray(SystemTray):
def set_menu(self):
try:
menu = QMenu()
exitAction = menu.addAction('Settings')
exitAction.triggered.connect(open_settings)
exitAction = menu.addAction('Exit')
exitAction.triggered.connect(self.exit)
self.setContextMenu(menu)
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
def open_settings():
try:
global settings
settings = Ui_Settings('./resources/icon.png')
settings.show()
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
def tray_main():
try:
global config
global app
app = QtWidgets.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
tray = Tray(QIcon('./resources/icon.png'), app)
tray.show()
sys.exit(app.exec_())
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
def output_main(text, x, y, width, height, font_size, font_family):
global output
app = QtWidgets.QApplication(sys.argv)
output = Ui_Output(True, './resources/icon.png')
output.setText(text, font_size, font_family)
output.resize(width, height)
output.move(x, y)
output.show()
sys.exit(app.exec_())
def recognition_main(general, translate):
logger = Logger('log_records.csv')
try:
old_general = {'device_id': -123, 'recognition_model': 101}
global config
config = Configure('./config.ini')
while True:
if old_general == general.value and general.value['activate'] == '1':
try:
data = appIO.get_data() # получаю массив байт с микрофона
except OSError:
pass
text = appRecognizer.speech2text(data) # превращяю этот массив в текст
if text != None:
if translate.value['translate'] == '1':
text = Translator.translate(text, translate.value['translate_to'], translate.value['translate_from'], 'Google')
try:
output_thread.stop()
except UnboundLocalError:
pass
config.reload()
output_thread = StoppableThread(target=output_main, daemon=True, args=(
text,
int(config.read('Output', 'x')),
int(config.read('Output', 'y')),
int(config.read('Output', 'width')),
int(config.read('Output', 'height')),
int(config.read('Output', 'font_size')),
config.read('Output', 'font_family')
)) # Передаю данные для окна
output_thread.start()
else:
if general.value['activate'] == '1':
if old_general['device_id'] != general.value['device_id']:
appIO = AudioInputOutput(input_device_index=int(general.value['device_id'])) # инициализирую нужные классы
appIO.start_stream() # Начинаю работу с микрофона
if old_general['recognition_model'] != general.value['recognition_model']:
appRecognizer = Recognizer(f"./resources/models/{general.value['recognition_model']}") # Переинициализирую модель для распознования
appIO.play_sound('./resources/notification.wav')
old_general = general.value
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))
if __name__ == '__main__':
try:
multiprocessing.freeze_support()
global config
config = Configure('./config.ini')
manager = multiprocessing.Manager()
global general
general = manager.Value('dic', {
'activate': config.read('General', 'activate'),
'device_id': config.read('General', 'device_id'),
'recognition_model': config.read('General', 'recognition_model')
})
global translate
translate = manager.Value('dic', {
'translate': config.read('Translate', 'translate'),
'translate_from': Ui_Settings.langs[config.read('Translate', 'translate_from')],
'translate_to': Ui_Settings.langs[config.read('Translate', 'translate_to')]
})
tray_process = multiprocessing.Process(target=recognition_main, args=(general, translate), daemon=True).start()
tray_main()
except Exception:
logger.logging.error(traceback.format_exc().replace('"', '\''))