示例#1
0
文件: stt.py 项目: pyVoice/pyVoice
def recognize_keyword() -> None:
    """
    Listens for the keyword, to activate the assistant.

    Steps:
        1. Listens for audio from the microphone
        2. Recognizes the audio using `gTTS`
        3. Checks if the keyword (as in `settings.KEYWORD`) is in the audio data (if True, break loop)
    """

    global keyword_detected
    global new_process

    audio = listen()
    new_process = True
    log.debug("Recognizing keyword...")

    try:
        rec_input = recognizer.recognize_google(audio, language=settings.LANGUAGE)

        if settings.KEYWORD in rec_input.lower():
            log.debug("Keyword detected!")
            # stop listening
            keyword_detected = True
        else:
            log.debug("Keyword not detected in '{0}'".format(rec_input))
    except sr.UnknownValueError:
        log.debug("Speech engine could not resolve audio")
    except sr.RequestError:
        log.error("An error ocurred with the Google services, try again")
    except:
        traceback.print_exc()
        log.error("A unknown error ocurred...")
示例#2
0
    def run(self) -> None:
        self.greet()

        while True:
            if self.stop:
                break

            try:
                if stt.listen_for_keyword():
                    log.debug("Back in main loop...")

                    audio = stt.listen()
                    audio_input = stt.recognize(audio)

                    if not audio_input:
                        log.info("Couldn't resolve audio...")
                        continue
                    else:
                        log.info("Catched input: '{0}'".format(audio_input))

                    cmd = matching.get_match(audio_input)

                    matching.execute_match(cmd)
            except KeyboardInterrupt:
                log.info("Detected keyboard interruption...")
                self.quit()
                break
            except:
                log.error("Unexpected error...")
                traceback.print_exc()

                # sends the traceback to Sentry
                capture_exception(traceback.print_exc())
                break
示例#3
0
文件: stt.py 项目: pyVoice/pyVoice
def recognize(audio: sr.AudioData) -> str:
    """
    Transcribes human voice data from a `AudioData` object (from `listen`)

    Args:
        audio (sr.AudioData): The raw audio data from the user

    Returns:
        str: A sentence/phrase with the user intent
    """

    output = None

    log.debug("Recognizing audio...")

    if settings.STT_ENGINE == "google":
        try:
            output = recognizer.recognize_google(audio, language=settings.LANGUAGE)
        except sr.UnknownValueError:
            log.debug("Speech engine could not resolve audio")
        except sr.RequestError:
            log.error("An error ocurred with the Google services, try again")
        except:
            traceback.print_exc()
            log.error("A unknown error ocurred...")
        finally:
            return output
示例#4
0
def register_install() -> json:
    api_url = settings.API_URL

    # system info/telemetry
    os = utils.get_operating_system()
    id = utils.get_device_id()
    version = settings.VERSION
    location = utils.get_location()

    req_data = {
        "device_id": id,
        "operating_system": os,
        "version": version,
        "location": location,
    }

    try:
        req = requests.post(api_url, data=req_data)
    except Exception:
        log.error("An error ocurred with the API request.")

    # debug only
    # print(req_data)
    # print(req.text)

    return req.json()
示例#5
0
def play_mp3(file: str) -> None:
    """
    Plays a MP3 file. Used when engine is set to gTTS.

    Args:
        file (str): the file to play
    """

    if file.endswith(".mp3"):
        playsound(file, True)
    else:
        log.error("The file provided is not a MP3 file.")
示例#6
0
def setup() -> None:
    """
    Initialize the TTS engine

    Steps:
        1. Checks the platform (operating system)
        2. Sets the engine to one supported (SAPI or gTTS)
    """

    global engine

    # detect OS
    if platform.system() == "Windows" and settings.TTS_AUTODETECT:
        try:
            import win32com.client as win32com

            engine = win32com.Dispatch("SAPI.SpVoice")
            log.debug("Using SAPI")
        except ModuleNotFoundError:
            log.error("Couldn't find module named 'win32com.client'")
            log.error(
                "Check installation or install via 'pip install pypiwin32'")
    else:
        try:
            from gtts import gTTS

            engine = gTTS
            log.debug("Using gTTS")
        except ModuleNotFoundError:
            log.error("Couldn't find module named 'gTTS'")
            log.error("Check installation or install via 'pip install gTTS'")

        log.info("(!) Using slow TTS engine on your OS")
示例#7
0
def execute_match(cmd) -> None:
    log.debug("Executing...")
    #
    try:
        # check if command exists as python script
        if commands.__contains__(cmd["name"]):
            log.debug("Replying...")
            commands.get(cmd["name"]).ex(cmd)
        elif cmd["name"] == "google":
            log.debug("Already executed...")
        else:
            log.error("Couldn't match command script")
    # type error: no command found
    except TypeError:
        tts.speak(
            replying.get_reply(["matching", "fail"], system=True, module=True))
示例#8
0
def ex(cmd):
    country_name = settings.COUNTRY.lower()

    api_url = "https://api.covid19api.com/total/country/{0}/status/confirmed".format(
        country_name
    )

    try:
        res = requests.get(api_url).json()[-1]
        total_cases = res["Cases"]

        tts.speak(
            replying.get_reply("covid").format(country_name.capitalize(), total_cases)
        )
    except requests.ConnectionError:
        log.error("An error ocurred while loading the data, try again later...")
    except:
        log.error("An error ocurred, try again later...")
示例#9
0
def ex(cmd):
    url = "http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&lang={2}&units=metric".format(
        settings.LOCATION, settings.WEATHER_API_KEY, settings.LANGUAGE_SHORT)

    data = requests.get(url)
    content = json.loads(data.text)

    if "message" in content:
        log.error(content["message"])
        tts.speak("An error ocurred: " + content["message"])
    else:
        temp = content["main"]["temp"]
        temp_max = content["main"]["temp_max"]
        description = content["weather"][0]["description"]

        tts.speak(
            replying.get_reply("weather").format(settings.LOCATION, temp,
                                                 description, temp_max))