-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartControl+.py
More file actions
326 lines (246 loc) · 9.12 KB
/
SmartControl+.py
File metadata and controls
326 lines (246 loc) · 9.12 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
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
try:
import os
import json
import customtkinter
import time
import webbrowser
import logging
from num2word import word
import pyaudio
from vosk import Model, KaldiRecognizer, SetLogLevel
import torch
import sounddevice as sd
import keyboard
import numpy
import silero
import threading
from transliterate import translit
import re
from num2words import num2words
from pathlib import Path
import speech_recognition as sr
import codecs
import cv2
except ImportError:
print('Не все библиотеки установлены.')
os.system(
'pip install num2word pyaudio vosk torch sounddevice keyboard silero numpy customtkinter gigachat '
'transliterate num2words pathlib SpeechRecognition opencv-python'
)
# Загрузка сохранённых данных
with codecs.open(Path('files/config.json').resolve(), 'r', 'utf-8') as data:
config = json.load(data)
data.close()
# Активационная фраза
if config['wakeword'] == '' or config['wakeword'] == ' ':
wakeword = "смарт, smart"
else:
wakeword = tuple(config['wakeword'].lower().replace(',', '').split())
# Голос синтеза речи
speaker = config['voice']
# Время приёма команд без активационной фразы
time_wait = config['time']
# Модель синтеза речи
model_id = config['sintez']
# Модель распознавания речи
if config['rasp'] == '0.22':
model = Model('vosk-model-small-ru-0.22')
elif config['rasp'] == '0.4':
model = Model('vosk-model-small-ru-0.4')
# Вариант распознавания
recognition = config['rasp_type']
# Массивы с ключевыми словами, которые нужно удалить из команды или изменить
to_replace = ['найди ', 'поищи', 'включи ', 'включить ', 'включил ', 'музыка ', 'музыку ', 'песня ', 'песню', 'видео ']
to_replace_write = ['напиши', 'введи']
to_replace_special = [['точка с запятой', ';'], ['запятая', ','], ['точка', '.'], ['дефис ', '-'], ['двоеточие', ':'],
['знак вопроса', '?'], ['восклицательный знак', '!']]
# Неизменяемые данные
sample_rate = 48000
language = 'ru'
if torch.cuda.is_available():
device = "cuda:0"
else:
device = "cpu"
device = torch.device(device)
put_accent = True
put_yo = True
rec = KaldiRecognizer(model, 16000)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
stream.start_stream()
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
mic = sr.Microphone()
r = sr.Recognizer()
time_ = 0
# Распознавание речи
def listen():
while True:
data = stream.read(4000, exception_on_overflow=False)
if (rec.AcceptWaveform(data)) and (len(data) > 0):
com_rec = json.loads(rec.Result())
if com_rec['text']:
yield com_rec['text']
# Синтез речи
def speak(text):
logging.info('Асссистент: ' + text)
text = translit(text.replace('c', 'к'), 'ru').lower().replace('w', 'в').replace('x', 'кс')
num = re.findall(r'-?\d+\+?', text)
if num != []:
for i in num:
numt = num2words(int(i), lang='ru')
text = text.replace(i, numt)
audio = model.apply_tts(text, speaker=speaker, sample_rate=sample_rate, put_accent=put_accent, put_yo=put_yo)
sd.play(audio, sample_rate)
time.sleep(len(audio) / sample_rate + 1.7)
sd.stop()
# Функции
def open_(param):
try:
os.startfile(param)
except:
webbrowser.open(param)
def search(param):
zapros = com_rec.lower()
for i in wakeword:
zapros = zapros.replace(i + ' ', '')
zapros = zapros.lower().replace('найди ', '')
webbrowser.open('https://www.google.com/search?q=' + zapros)
def search_song(param):
zapros = com_rec.lower()
for i in wakeword:
zapros = zapros.replace(i + ' ', '')
for i in to_replace:
zapros = zapros.replace(i, '')
webbrowser.open('https://music.yandex.ru/search?text=' + zapros)
def search_video(param):
zapros = com_rec.lower()
for i in wakeword:
zapros = zapros.replace(i + ' ', '')
for i in to_replace:
zapros = zapros.replace(i, '')
webbrowser.open('https://www.youtube.com/results?search_query=' + zapros)
def browser(param):
eval(f'{param}()')
def new_tab():
keyboard.send('ctrl+t')
def incognito_tab():
keyboard.send('ctrl+shift+n')
def prev_tab():
keyboard.send('ctrl+shift+tab')
def next_tab():
keyboard.send('ctrl+tab')
def down():
keyboard.send('pagedown')
def up():
keyboard.send('pageup')
def end():
keyboard.send('end')
def home():
keyboard.send('home')
def write_text(param):
text_to_write = com_rec.lower()
for i in wakeword:
text_to_write = text_to_write.replace(i + ' ', '')
for i in to_replace_write:
text_to_write = text_to_write.replace(i + ' ', '')
for i in to_replace_special:
text_to_write = text_to_write.replace(' ' + i[0], i[1])
keyboard.write(text_to_write + ' ')
def run_snake():
subprocess.run([sys.executable, "snake.py"])
def run_3d():
subprocess.run([sys.executable, "33в+.py"])
def repeat_recognized_text(text):
speak(text)
def run_ping():
subprocess.run([sys.executable, "ping.py"])
import subprocess
import sys
def paint():
subprocess.run([sys.executable, "paint.py"])
def smart():
subprocess.run([sys.executable, "запуск.py"])
def inter():
subprocess.run([sys.executable, "interface.pyw"])
def main_func(com):
exec = False
global time_
if com.startswith(wakeword) or time.time() - time_ < time_wait:
if com.startswith(wakeword):
time_ = time.time()
logging.info('Распознано: ' + com)
com = com.split()
# Веса категорий и параметров
with codecs.open(Path('files/we.json').resolve(), 'r', 'utf-8') as data_we:
we = json.load(data_we)
data_we.close()
# Определение весов
for i in com:
try:
ind_kw = 0
for ind_kw in range(len(kw['main'][i])):
we['main'][kw['main'][i][ind_kw]['param']] += kw['main'][i][ind_kw]['weight']
except:
pass
ca = max(we['main'], key=we['main'].get)
for i in com:
try:
ind_kw = 0
for ind_kw in range(len(kw[ca][i])):
we[ca][kw[ca][i][ind_kw]['param']] += kw[ca][i][ind_kw]['weight']
exec = True
except:
pass
# Исполнение команды
if exec:
pa = max(we[ca], key=we[ca].get)
eval(f'{ca}(r"{pa}")')
else:
pass
# Ключевые фразы
with codecs.open(Path('files/kw.json').resolve(), 'r', 'utf-8') as data_kw:
kw = json.load(data_kw)
data_kw.close()
# Загрузка модели синтеза речи
model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=language, speaker=model_id)
model.to(device)
# Основной цикл
with mic as source:
if recognition == 'Google Speech Recognition':
r.adjust_for_ambient_noise(source, duration=1)
while True:
com_rec = r.listen(source)
try:
com_rec = r.recognize_google(com_rec, language='ru-RU')
main_func(com_rec.lower())
if ('змея' in com_rec.lower()) or ('змейка' in com_rec.lower()):
run_snake()
if 'модель' in com_rec.lower():
run_3d()
if 'игра' in com_rec.lower():
run_ping()
if 'кисть' in com_rec.lower():
paint()
if 'жесты' in com_rec.lower():
smart()
if 'настройки' in com_rec.lower():
inter()
repeat_recognized_text(com_rec) # Вызов функции повторения распознанного текста
except:
pass
else:
for com_rec in listen():
main_func(com_rec.lower())
if ('змея' in com_rec.lower()) or ('змейка' in com_rec.lower()):
run_snake()
if 'модель' in com_rec.lower():
run_3d()
if 'игра' in com_rec.lower():
run_ping()
if 'кисть' in com_rec.lower():
paint()
if 'жесты' in com_rec.lower():
smart()
if 'настройки' in com_rec.lower():
inter()
repeat_recognized_text(com_rec) # Вызов функции повторения распознанного текста