使用python和SpeechRecognition检测句子

2024-06-28 11:36:50 发布

您现在位置:Python中文网/ 问答频道 /正文

有人可以帮我,我正在寻找文字识别方面的帮助,我使用语音识别,我希望当我启动语音助手(我编写了代码)时,它不会跟我说话,它会留在后台,只有当我像Ok Google一样呼叫它时,它才会问我一个问题,然后听我问什么,最后闭嘴

from random import choice
import time
import webbrowser
import speech_recognition as sr
import subprocess
import pyttsx3


def assistant_voix(sortie):
    if sortie != None:
        voix = pyttsx3.init()
        print("A.I : " + sortie)
        voix.say(sortie)
        voix.runAndWait()


def internet():
    try:
        urlopen('https://www.google.com', timeout=1)
        print("Connecté")
        return True
    except:
        print("Déconnecté")
        return False


def reconnaissance():
    r = sr.Recognizer()
    r.energy_threshold = 4000
    pas_compris = "Veuillez répéter."
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        r.pause_threshold = 0.7
        print(".... ")
        audio = r.listen(source)
        if internet():
            try:
                vocal = r.recognize_google(audio, language = 'fr-FR')
                print(vocal)
                return vocal
            except sr.UnknownValueError:
                assistant_voix(pas_compris)
        else:
            try:
                vocal = r.recognize_sphinx(audio, language = 'fr-fr')
                print(vocal)
                return vocal
            except sr.UnknownValueError:
                assistant_voix(pas_compris)

def application(entree):
    if entree != None:
        dico_apps = {
            "note": ["notepad","note pad"],
            "sublime": ["sublime","sublime texte"],
            "obs": ["obs","obs capture","capture l'ecran"],
            "edge": ["microsoft edge","edge"],
            "premiere": ["Adobe premiere", "premiere pro", "adobe premiere pro", "premiere"],
            "word": ["Microsoft word", "word", "traitement de texte", "world"],
            "excel": ["Excel", "Tableur"],
            }
        fini = False
        while not fini:
            for x in dico_apps["note"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Note .")
                    subprocess.Popen('C:\\Windows\\notepad.exe')
                    fini = True
            for x in dico_apps["sublime"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Sublime Text .")
                    subprocess.Popen('C:\\Program Files\\Sublime Text 3\\sublime_text.exe')
                    fini = True
            for x in dico_apps["obs"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Obs .")
                    subprocess.Popen('C:\\Program Files\\obs-studio\bin\\64bit\\obs64')
                    fini = True
            for x in dico_apps["edge"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Edge .")
                    subprocess.Popen('C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe')
                    fini = True
            for x in dico_apps["premiere"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Adobe Premiere Pro .")
                    subprocess.Popen('E:\\Logiciel\\Programme\\Première Pro\\Adobe Premiere Pro 2020\\Adobe Premiere Pro.exe')
                    fini = True
            for x in dico_apps["word"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Word .")
                    subprocess.Popen('C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE')
                    fini = True
            for x in dico_apps["excel"]:
                if x in entree.lower():
                    assistant_voix("Ouverture de Excel .")
                    subprocess.Popen('C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE')
                    fini = True
            fini = True

def main():
    assistant_voix("Bonjour , je suis Bob votre assistant de bureau. Dîtes-moi ce que je peux faire pour vous .")
    fermer = ["arrête-toi","tais-toi"]
    ouvrir = ["ouvre","ouvrir", "lance", "lancer", "démarrer", "démarre"]
    actif = True
    while actif:
        if (entree := reconnaissance()) is not None:
            for x in range(len(fermer)):
                if fermer[x] in entree.lower():
                    assistant_voix("A bientôt monsieur .")
                    actif = False
            for x in range(len(ouvrir)):
                if ouvrir[x] in entree.lower():
                    application(entree)
                    break

if __name__ == '__main__':
    main() ```

Tags: appsintrueforifdelowerdico