Как сделать распознавание речи + графический интерфейс cv2 Python
Я создаю голосового помощника и хочу, чтобы он работал параллельно с открытым графическим окном cv2. мой помощник работает только после закрытия окна. когда я подключаю потоки, чтобы графическое окно и голосовой помощник работали одновременно, я получаю сообщение об ошибке, что основной файл не видит файл, в котором находится мой data_set (именно там я тренирую своего голосового помощника). когда я удаляю потоки в файле функции, код работает. но одновременно он выполняет только потоки в основном файле вот основной код файла:
import cv2
import threading
import words
from skills import *
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
q = queue.Queue()
model = vosk.Model('vosk-model-small-ru-0.22')
device = sd.default.device
samplerate = int(sd.query_devices(device[0], 'input')['default_samplerate'])
def callback(indata, frames, time, status):
q.put(bytes(indata))
def recognize(data, vectorizer, clf):
trg = words.TRIGGERS.intersection(data.split())
if not trg:
return
data.replace(list(trg)[0], '')
text_vector = vectorizer.transform([data]).toarray()[0]
answer = clf.predict([text_vector])[0]
func_name = answer.split()[0]
speaker(answer.replace(func_name, ''))
exec(func_name + '()')
def cv2_voice():
image = cv2.imread('1.png')
cv2.imshow('image window', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
vectorizer = CountVectorizer()
vectors = vectorizer.fit_transform(list(words.data_set.keys()))
clf = LogisticRegression()
clf.fit(vectors, list(words.data_set.values()))
del words.data_set
with sd.RawInputStream(samplerate=samplerate, blocksize=16000, device=device[0],
dtype="int16", channels=1, callback=callback):
rec = KaldiRecognizer(model, samplerate) #
while True:
data = q.get()
if rec.AcceptWaveform(data):
data = json.loads(rec.Result())['text']
recognize(data, vectorizer, clf)
voice_thread = threading.Thread(target=main)
cv2_thread = threading.Thread(target=cv2_voice)
voice_thread.start()
cv2_thread.start()
if __name__ == '__main__':
main()
скажите мне, что я делаю не так. может быть, есть другой способ или улучшить этот?
Я также предоставляю файл со своими функциями для помощника:
import os, sys, requests, subprocess
import pyttsx3
import time
import cv2
import random
from joblib import Parallel, delayed
import time
def rezult1():
vo1 = threading.Thread(target=passive)
c1 = threading.Thread(target=emotion_1)
vo1.start()
c1.start()
def rezult2():
vo2 = threading.Thread(target=weather)
c2 = threading.Thread(target=emotion_2)
vo2.start()
c2.start()
def rezult3():
vo3 = threading.Thread(target=offBot)
c3 = threading.Thread(target=emotion_2)
vo3.start()
c3.start()
def rezult4():
vo4 = threading.Thread(target=passive)
c4 = threading.Thread(target=emotion_3)
vo4.start()
c4.start()
def rezult5():
vo5 = threading.Thread(target=passive)
c5 = threading.Thread(target=emotion_4)
vo5.start()
c5.start()
def rezult6():
vo6 = threading.Thread(target=smex)
c6 = threading.Thread(target=emotion_2)
vo6.start()
c6.start()
def rezult7():
vo7 = threading.Thread(target=passive)
c7 = threading.Thread(target=emotion_2)
vo7.start()
c7.start()
def rezult8():
vo8 = threading.Thread(target = TimerVoice)
c8 = threading.Thread(target = emotion_2)
vo8.start()
c8.start()
def rezult9():
vo9 = threading.Thread(target=vremia)
c9 = threading.Thread(target=emotion_2)
vo9.start()
c9.start()
def rezult10():
vo10 = threading.Thread(target=calc)
c10 = threading.Thread(target=emotion_3)
vo10.start()
c10.start()
def emotion_2():
images = ['1.png', '7.png', '2.png']
for image_path in images:
img = cv2.imread(image_path)
cv2.imshow('Image', img)
cv2.waitKey(1000)
cv2.waitKey(0)
def emotion_1():
images = ['1.png', '2.png', '4.png']
for image_path in images:
img = cv2.imread(image_path)
cv2.imshow('Image', img)
cv2.waitKey(1000)
cv2.waitKey(0)
def emotion_3():
images = ['1.png', '6.png']
for image_path in images:
img = cv2.imread(image_path)
cv2.imshow('Image', img)
cv2.waitKey(1000)
cv2.waitKey(0)
def emotion_4():
images = ['1.png', '8.png', '9.png', '10.png']
for image_path in images:
img = cv2.imread(image_path)
cv2.imshow('Image', img)
cv2.waitKey(1000)
cv2.waitKey(0)
def emotion_5():
images = ['1.png', '8.png', '3.png']
for image_path in images:
img = cv2.imread(image_path)
cv2.imshow('Image', img)
cv2.waitKey(1000)
cv2.waitKey(0)
def emotion_6():
images = ['1.png', '5.png']
for image_path in images:
img = cv2.imread(image_path)
cv2.imshow('Image', img)
cv2.waitKey(1000)
cv2.waitKey(0)
engine = pyttsx3.init()
engine.setProperty('rate', 180)
def speaker(text):
engine.say(text)
engine.runAndWait()
def weather():
try: #цикл try
params = {'q': 'London', 'units': 'metric', 'lang': 'ru', 'appid': ''}
response = requests.get(f'https://api.openweathermap.org/data/2.5/weather', params=params)
if not response:
raise
w = response.json()
speaker(f" {w['weather'][0]['description']} {round(w['main']['temp'])} ")
except:
speaker('error')
def passive():
pass
def offBot():
sys.exit()
def smex():
pass
class TimerVoice():
def parse_command(text):
words = text.split()
if len(words) >= 5 and words[0] in ["Put it down", "get it started"] and words[1] == "timer" and words[
2] == "on":
try:
minutes = int(words[3])
word_minutes = words[4]
if word_minutes in ["minute", "minutes"]:
return minutes, word_minutes
except ValueError:
pass
return None, None
def set_timer(minutes, word_minutes):
seconds = minutes * 60
time.sleep(seconds)
speaker(f"{minutes} {word_minutes}.")
def vremia():
curr_time = time.strftime("%H:%M", time.localtime())
speaker(f" {curr_time}")