UMFUCHI
@UMFUCHI
НедоПайтонист

Почему когда я молчу спамит Неизвестная команда?

Есть код:
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)

Заметил что когда я некоторое время молчу, голос. ассистент начинает спамить что неизвестная команда, как это исправить?
  • Вопрос задан
  • 103 просмотра
Пригласить эксперта
Ответы на вопрос 1
ошибка в том что он возвращает пустой текст или видит шумы микрофона - надо попробовать следующее: проверить что возвращает сервис при молчании потом сделать if типо:
print(text)
if predicate(text) == "молчание":
   pass
else:
   m.say_message("Неизвестная команда")
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы