使用pyinstaller将文件转换为exe时,Python语音识别无法工作

2024-06-08 20:34:40 发布

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

我使用Python中的语音识别和GTTS模块创建了一个语音助手。当从脚本运行时,代码工作得非常好。但是当我使用pyinstaller--onefile Speak.py(文件名)转换它时,它停止了工作。它正在使用我的麦克风,但没有任何反应。假设我说“嘿,谷歌”,这是叫醒关键词,它根本没有反应。我试了一会儿,但没有用。有人能帮我吗

代码:

"from gtts import gTTS
import speech_recognition as sr
import os
import playsound
from datetime import datetime
import webbrowser

keeprunning = True

WAKING = "HEY GOOGLE"
def chec(cond, tex):
    if cond in tex:
        return True
    else:
        return False

def speak(text):
    speaker = gTTS(text = text, lang = "en")
    speaker.save("Voice.mp3")
    playsound.playsound("voice.mp3")
    os.remove("Voice.mp3")
def cleanify(text, wtremove, wtremove2=""):
    try:
        text = text.replace(wtremove)
    except:
        if not wtremove2 == "":
            text = text.replace(wtremove2, "")
    return text

#Main controlling part here(Oof!)
def Control(comnd):
    if chec("SEARCH", comnd) or chec("GOOGLE", comnd):
        comnd = cleanify(comnd, "SEARCH", "GOOGLE")
        webbrowser.open(f"https://google.com/search?q={comnd.lower()}")

    elif chec("WHO ARE YOU", comnd):
        speak("I am your virtual assistant, google")
        keepcontrol()

    elif chec("TIME", comnd):
        t = datetime.now()
        t = time.strptime(t.strftime("%H:%S"), "%H:%M")
        tt = time.strftime( "%I:%M %p", t )
        speak(f"The current time is {tt}")
    elif chec("NOTE", comnd) or chec("REMEMBER", comnd) or chec("JOT", comnd):
        try:
            text = comnd.replace("NOTE")
        except:
            try:
                text = text.replace("REMEMBER")
            except:
                text = text.replace("JOT")
        try:
            with open("This_File_Name_Is_Very_Big_Because_I_dont_Want_to_overwrite_anyother_files_so_forgive_me_plz.txt", "rt") as file:
                previous = file.read()
        except:
            with open("This_File_Name_Is_Very_Big_Because_I_dont_want_to_overwrite_any_other_files_so_forgive_me_plz.txt", "wt") as file:
                ttw = f"""
                    •{text.lower()}
                """
                file.write(previous + ttw)
                previous = ""

        with open("Notes.txt", "wt") as file:
            ttw = previous + "  •" + text

            file.write(ttw)
        speak("Got it! I noted it down")

    elif chec("OPEN", comnd):
        web = cleanify(comnd, "OPEN")
        bruh = cleanify(web, ".com")
        speak(f"Opening {bruh}")
        print(comnd)
        web = cleanify(comnd, "OPEN")
        if web == "":
            web = listen()
        else:
            if "https://" in web:
                webbrowser.open(web.lower())
            else:
                web = "https://" + web
                webbrowser.open(web.lower())
        keepcontrol()
        
#Main controlling part ends

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        listen = r.listen(source)
        try:
            text = r.recognize_google(listen)
            return text.upper()
        except Exception as e:
            if not e == "":
                return ""
                keepcontrol()
            else:
                speak("Sorry we couldnt catch that, please try again")
                keepcontrol()

def keepcontrol():
    while keeprunning:
        text = listen()
        if not text == None:
            if text.count(WAKING) == 1:
                speak("I am listening..")
                text = listen()
                if not text == None and not text == "":
                    Control(text)
                else:
                    speak("Sorry i did not get that. Try again in a few seconds")
                    keepcontrol()
            elif chec("QUIT", text):
                exit()
            elif chec("", text):
                keepcontrol()
            else:
                speak("Sorry we didnt get that right. Please try again.")
                        
                
        
speak("I am booting up...")
keepcontrol()
"

Tags: textimportwebifdefasnotopen
1条回答
网友
1楼 · 发布于 2024-06-08 20:34:40

当您将其转换为exe时,我认为您忘记将用于构建助手的文件放在文件夹中。 转换exe后,只需将这些文件(您使用的文件)复制到保存exe的文件夹中即可。 希望它能起作用

相关问题 更多 >