Answer the question
In order to leave comments, you need to log in
How can you speed up the code?
Sometimes the code can blunt like this for 7 minutes and do nothing. but just listen to what I say, for example, I said Hello several times, and then after a while he shows me what I said and so those very few Hello instead of one to which he should have answered immediately. In short, the question is, how can you speed up the python itself so that after the bot has answered, you don’t have to wait, for example, a second for the speech recognizer to activate?
import speech_recognition as sr
from gtts import gTTS
import random
import time
import datetime
import playsound
#Распознаватель речи
def listen_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Скажите вашу команду: ")
audio = r.listen(source)
try:
our_speech = r.recognize_google(audio, language="ru")
print("Вы сказали: " + our_speech)
return our_speech
except sr.UnknownValueError:
return "ошибка"
except sr.RequestError:
return "ошибка"
class VoiceCommandList:
def __init__(self):
self.actions = list()
def on(self, condition):
if isinstance(condition, str):
condition = condition.lower()
predicate = lambda text: condition in text
elif callable(condition):
predicate = condition
else:
raise TypeError('Condition must be either string or function!')
def decorator(command_func):
self.actions.append((predicate, command_func))
return command_func
return decorator
def run_command(self, text):
text = text.lower()
for predicate, command in self.actions:
if predicate(text):
try:
response = command(text)
if response is None:
response = "Команда выполнена"
except Exception as err:
response = "Ошибка при выполнении команды"
print(err)
if response:
m.say_message(response)
break
else:
m.say_message("Неизвестная команда")
vcl = VoiceCommandList()
#сама команда
@vcl.on('привет')
def hello(text):
return "Здравствуйте, сэр!"
#То что говорит бот
def say_message(message):
voice = gTTS(message, lang="ru")
file_voice_name = "_audio_" + str(time.time()) + "_" + str(random.randint(0, 100000)) + ".mp3"
voice.save(file_voice_name)
playsound.playsound(file_voice_name)
print("Голосовой ассистент: " + message)
if __name__ == '__main__':
while True:
command = listen_command()
vcl.run_command(command)
Answer the question
In order to leave comments, you need to log in
Frontal response - is there background noise? Maybe the Recognizer takes it for speech, and thinks that you have been talking all these few minutes.
There is something interesting in the parameters of the listen() method , check it out.
1) firewall iptables, port forwarding
2) port forwarding via ssh
3) vpn
In iptables, register access from the outside only for the second server. From the Internet:
iptables -N mysql
iptables -A mysql --src 127.0.0.1 -j ACCEPT
iptables -A mysql --src 1.1.1.1 -j ACCEPT
iptables -A mysql -j DROP
iptables -I INPUT -m tcp -p tcp --dport 3306 -j mysql
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question