尝试做助手但面临速度问题

问题描述 投票:0回答:2

我的程序运行得很好,但我遇到了速度问题。响应和给出结果需要太长时间。运行 2,3 次后,需要比以前更多的时间。此外,启动还需要很多时间。提高程序运行速度的方法有哪些?我是这个领域的新手。

# Imported speech recognition as sr, pyttsx3, pywhatkit, datetime, pyjokes, os
listener = sr.Recognizer()
engine = pyttsx3.init()


def talk(text):
        engine.say(text)
        engine.runAndWait()


talk('Hello what can i do for you')


def take_command():
        try:
            with sr.Microphone() as source:
                print("Listening...")
                voice = listener.listen(source)
                command = listener.recognize_google(voice)
                command = command.lower()
                if "jarvis" in command:
                    command = command.replace('jarvis', '')
                    return command
                else:
                    talk("I cannot continue until you say jarvis")
                    take_command()
        except:
            pass


def run_jarvis():
        command = take_command()
        if not command:
            take_command()
        else:
            print(command)
            if "play" in command:
                song = command.replace("play", "")
                talk("playing" + song)
                pywhatkit.playonyt(song)
            elif 'time' in command:
                time = datetime.datetime.now().strftime('%I:%M %p')
                print(time)
                talk(time)
            elif 'joke' in command:
                joke = pyjokes.get_joke()
                print(joke)
                talk(joke)
            elif 'downloads' in command:
                path = "C:/Users\Mayur\Downloads"
                path = os.path.realpath(path)
                os.startfile(path)
            elif "whatsapp" in command:
                msg = command.replace("whatsapp", "")
                pywhatkit.sendwhatmsg_instantly("+91xxxxxxxxxx", msg)
                print("Successfully Sent!")
                talk("successfully sent")
            elif 'who is' or 'what is' or 'where is' or 'when is' or 'information' or 'which is' or "how is" in command:
                result = pywhatkit.info(command, 3, True)
                talk(result)
                print(result)
            else:
                talk('Sorry sir can you repeat again')


while True:
        run_jarvis()
python performance speech-recognition
2个回答
0
投票

它对我来说工作得很好,我没有看到任何会使你的程序变慢的代码,并且

listener.recognize_google(voice)
应该连接到互联网以返回结果。所以你的网络连接可能有问题。启动缓慢也没关系,因为您已经将许多不同的库导入到您的程序中。因此,这使得启动缓慢。另外还必须安装
recognizer
engine
,这也会降低速度。总体而言,您的程序速度取决于您的计算机速度和互联网连接。


0
投票

我自己有一个虚拟助手程序,你必须记住,使用tts sapi5文本转语音的虚拟助手是逐字阅读逻辑,不像ai,ai不会从文档、url地址中逐字阅读精确的文本和页面,,它围绕相关性,绕行导航,一般响应进行设计,如果要求人工智能模型阅读精确和准确的单词,它不能这样做,请自己询问聊天gpt,如果它可以逐字逐字地执行,它会说不,所以tts sapi5 epeak 或任何类似的东西,响应有点慢。 ?这并不完全意味着不可能加速。但对于人工智能生成模型,需要注意的一件重要的事情是,它们最有益的是编码程序,而不是揭示绝对真相,EG。书中实际写的内容,用准确的话来说,但是:因为编码程序是基于变体的,为了在程序中实现特定的结果,比如编码一个循环,输出一个列表。这可以通过多种方式完成,为了达到相同的结果,编码中有很多方法可以实现相同的结果,这就是人工智能在创建程序方面表现出色的地方,你不是在寻找精确和正确的功能,达到某种完美,而是对功能和数据条件进行合理的数据搜索,这将补充您的程序目的,这就是人工智能所做的,有趣的是,我使用人工智能聊天gpt创建了一个带有窗口等的tts spai 5程序,并创建了一个模型,这不是基于人工智能:但基于逐字记录,如果集成在一起,两者都可以在计算机程序中占有一席之地。,

下面是一个Python虚拟助手程序,你可以尝试一下:确保你的版本至少是3.11。在 python 中并安装必要的库,使用 cmd 上的 pip install 导入。 下图:

导入系统 导入剪贴板 将语音识别导入为 sr 从 googlesearch 导入搜索 导入请求 从 bs4 导入 BeautifulSoup 导入 pyttsx3 将 numpy 导入为 np 导入pyaudio 从 PyQt5.QtWidgets 导入 QMainWindow、QWidget、QVBoxLayout、QTextEdit、QLineEdit、QPushButton、QAction 从 PyQt5.QtCore 导入 Qt、pyqtSignal、QThread 从 PyQt5.QtWidgets 导入 QMenuBar、QMenu、QAction、QLineEdit 从 PyQt5 导入 QtWidgets、QtGui、QtCore 从 PyQt5.QtWidgets 导入 QVBoxLayout、QGraphicsView、QLabel、QApplication、QMainWindow、QWidget、QOpenGLWidget、QTextEdit 从 PyQt5.QtCore 导入 QTimer、pyqtSlot、pyqtSignal、QObject 从 OpenGL 导入 GL 从 OpenGL 导入 GLU 导入线程 从 PyQt5.QtCore 导入 Qt

类 AudioVisualizer(QtWidgets.QWidget): def init(自身): 超级()。init()

    self.resize(300, 50)  # Size for the visualizer widget  #400 / 100
    # Set background color to black and add a white border
    self.setStyleSheet("background-color: black; border: 8px solid white; border-radius: 10px;")  # White border and radius

    layout = QVBoxLayout()
    self.label = QLabel(" Input Waveform:")
    self.label.setStyleSheet("color: white;")  # Set label color to white for contrast against the black background
    layout.addWidget(self.label)

    # Create a graphics view with a fixed height and width
    self.graphics_view = QGraphicsView()
    self.graphics_view.setFixedHeight(80)  # Fixed height for the waveform display
    self.graphics_view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)  # Disable horizontal scroll bar
    self.graphics_view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)  # Disable vertical scroll bar
    layout.addWidget(self.graphics_view)

    self.setLayout(layout)

  

    # Initialize PyAudio
    self.audio = pyaudio.PyAudio()
    self.stream = self.audio.open(format=pyaudio.paInt16,
                                   channels=1,
                                   rate=44100,
                                   input=True,
                                   frames_per_buffer=1024)

    self.timer = QtCore.QTimer()
    self.timer.timeout.connect(self.update_waveform)
    self.timer.start(50)  # Update every 50 ms

def update_waveform(self):
    try:
        # Read data from the microphone
        data = self.stream.read(1024, exception_on_overflow=False)
        wav_data = np.frombuffer(data, dtype=np.int16)

        # Create a scene for the waveform
        scene = QtWidgets.QGraphicsScene()
        pen = QtGui.QPen(QtGui.QColor(0, 255, 0))  # Green pen for the waveform

        # Plot the waveform
        for i in range(len(wav_data) - 1):
            scene.addLine(i, wav_data[i] / 100, i + 1, wav_data[i + 1] / 100, pen)  # Scale for visibility

        self.graphics_view.setScene(scene)

    except OSError as e:
        # Handle the case where the stream is closed or there's an error
        print(f"Error reading from the audio stream: {e}")

def closeEvent(self, event):
    # Stop the audio stream when closing the application
    if self.stream.is_active():
        self.stream.stop_stream()
        self.stream.close()
    self.audio.terminate()
    event.accept()

定义 OpenGL 小部件

类 OpenGLWidget(QOpenGLWidget): def init(自身): 超级()。init() self.rotation_angle = 0 # 初始化旋转角度 self.timer = QTimer() # 创建一个计时器 self.timer.timeout.connect(self.update_rotation) # 将超时信号连接到update方法 self.timer.start(16) # 设置计时器大约每 16 毫秒触发一次(约 60 FPS)

def initializeGL(self):
    GL.glClearColor(0.0, 0.0, 0.0, 1.0)  # Black background
    GL.glEnable(GL.GL_DEPTH_TEST)  # Enable depth testing
    GL.glEnable(GL.GL_BLEND)  # Enable blending for transparency
    GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)  # Set blend function
    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glLoadIdentity()
    GLU.gluPerspective(45, 200 / 150, 0.1, 50.0)  # Updated aspect ratio for 200x150
    GL.glTranslatef(0.0, 0.0, -5)

def paintGL(self):
    GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)  # Clear the buffers
    GL.glPushMatrix()  # Save the current state
    GL.glRotatef(self.rotation_angle, 0, 1, 0)  # Rotate by the current angle
    self.draw_cube()  # Draw the cube
    GL.glPopMatrix()  # Restore the previous state
    self.update()  # Request an update

def update_rotation(self):
    self.rotation_angle += 1  # Increment the rotation angle
    self.rotation_angle %= 360  # Keep the angle within 0-359

def draw_cube(self):
    # Define the vertices of the cube
    vertices = [
        (1, 1, -1),   # Vertex 0
        (1, -1, -1),  # Vertex 1
        (-1, -1, -1), # Vertex 2
        (-1, 1, -1),  # Vertex 3
        (1, 1, 1),    # Vertex 4
        (1, -1, 1),   # Vertex 5
        (-1, -1, 1),  # Vertex 6
        (-1, 1, 1)    # Vertex 7
    ]

    edges = [
        (0, 1), (1, 2), (2, 3), (3, 0),  # Back face
        (4, 5), (5, 6), (6, 7), (7, 4),  # Front face
        (0, 4), (1, 5), (2, 6), (3, 7)   # Connecting edges
    ]

    # Draw the cube edges
    GL.glBegin(GL.GL_LINES)  # Begin drawing lines
    GL.glColor4f(1.0, 1.0, 1.0, 0.5)  # Set color with transparency (white with 50% alpha)
    for edge in edges:
        for vertex in edge:
            GL.glVertex3fv(vertices[vertex])  # Specify each vertex for the edges
    GL.glEnd()  # End drawing

自定义 QObject 以安全地处理线程

类 TextAppender(QObject): 文本信号 = pyqtSignal(str)

创建主应用程序窗口

类主窗口(QMainWindow): def init(自身): 超级()。init() self.setWindowTitle("带有嵌入式 OpenGL 和音频可视化工具的主窗口") self.setGeometry(100, 100, 800, 600) # 主窗口大小(宽度,高度)

    # Create a central widget to hold the layout
    central_widget = QWidget(self)  # Ensure central widget is linked to MainWindow
    self.setCentralWidget(central_widget)

    # Create a vertical layout
    layout = QVBoxLayout(central_widget)

    # Set style for the central widget
    central_widget.setStyleSheet("background-color: black; border: 8px solid gray; border-radius: 12px;")  # Main window border

    # Create an audio visualizer widget (replace AudioVisualizer() with your actual class)
    self.audio_visualizer = AudioVisualizer()  # Replace this with your actual audio visualizer class
    self.audio_visualizer.setFixedSize(600, 100)  # Set size of audio visualizer widget
    self.audio_visualizer.setStyleSheet("background-color: black; border: 10px solid light gray; border-radius: 12px;")  # Visualizer style

    layout.addWidget(self.audio_visualizer, alignment=Qt.AlignHCenter)  # Add audio visualizer at the top center

    # Create an OpenGL widget
    self.opengl_widget = OpenGLWidget()
    self.opengl_widget.setFixedSize(500, 300)  # Set size of OpenGL widget
    self.opengl_widget.setStyleSheet("background-color: black; border: 8px white; border-radius: 12px;")  # OpenGL widget border

    layout.addWidget(self.opengl_widget)  # Add OpenGL widget to layout

    # Create a QTextEdit to display command output
    self.output_area = QTextEdit()
    self.output_area.setReadOnly(True)  # Set as read-only
    self.output_area.setStyleSheet("background-color: black; color: white; border: 8px solid red; border-radius: 12px; font-size: 18px;")  # Text area style with white text
    self.output_area.setFixedSize(800, 450)  # Set the output area size
    layout.addWidget(self.output_area)  # Add text output area to layout

    # Thread-safe text appender
    self.text_appender = TextAppender()
    self.text_appender.text_signal.connect(self.append_text)

    # Start the  assistant without interference
    self.start_assistant()

def append_text(self, text):
    """Appends text to the QTextEdit output area."""
    self.output_area.append(text)  # Append the received text to the QTextEdit

def start_assistant(self):
    """Logic to start the AI assistant automatically (this shouldn't be modified)."""
    pass


@pyqtSlot(str)
def append_text(self, text):
    # Safely append text to the QTextEdit
    self.output_area.append(text)  # Add the text to the output area

def speak(self, text, speed=150, amplitude=200):
    # Initialize the pyttsx3 engine
    engine = pyttsx3.init()
    engine.setProperty('rate', speed)  # Set speech rate
    engine.setProperty('volume', amplitude / 200)  # Set speech volume
    engine.say(text)
    engine.runAndWait()

def google_search(self, query):
    try:
        search_results = list(search(query))
        return search_results
    except Exception as e:
        self.text_appender.text_signal.emit(f"An error occurred during search: {e}")
        return []

def recognize_speech(self):
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        self.text_appender.text_signal.emit("Detecting...")  # Redirect to output area
        recognizer.energy_threshold = 252
        recognizer.adjust_for_ambient_noise(source, duration=1)
        audio = recognizer.listen(source)
    try:
        self.text_appender.text_signal.emit("Recognizing...")  # Redirect to output area
        query = recognizer.recognize_google(audio)
        self.text_appender.text_signal.emit("Output of query: " + query)  # Redirect recognized speech to output area
        return query.lower()
    except sr.UnknownValueError:
        self.text_appender.text_signal.emit("Error: Unable to recognize speech")  # Redirect error to output area
        return ""
    except sr.RequestError as e:
        self.text_appender.text_signal.emit(f"Could not request results: {e}")  # Redirect error to output area
        return ""

def read_from_url(self, url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            html_content = response.text
            soup = BeautifulSoup(html_content, 'html.parser')
            paragraphs = [p.text for p in soup.find_all('p')]
            if not paragraphs:
                self.text_appender.text_signal.emit("No content found in the URL.")
                return

            for paragraph in paragraphs:
                self.speak(paragraph)  # Read the paragraph aloud
                self.text_appender.text_signal.emit(paragraph)  # Append the paragraph to the output area
                self.speak("Would you like me to read? If not, say 'stop reading', or say 'continue'.")
                decision = self.recognize_speech()  # Wait for the user's response

                if decision and "stop reading" in decision.lower():
                    self.text_appender.text_signal.emit("Stopped reading.")
                    break  # Exit the loop if the user chooses to stop

            # After stopping reading, ask if the user wants to continue searching or exit
            self.speak("Do you want to continue with a different data scan? If yes, say 'yes'. If not, say 'exit program'.")
            post_decision = self.recognize_speech()  # Capture user's decision

            if post_decision and "exit program" in post_decision.lower():
                self.speak("Exiting program.")
                self.text_appender.text_signal.emit("Exiting program.")
                sys.exit()  # Exit the program immediately

            elif post_decision and "yes" in post_decision.lower():
                self.speak("Continuing with another search.")
                self.text_appender.text_signal.emit("Continuing with another search.")
                self.ai_assistant_main()  # Restart the search process

        else:
            self.text_appender.text_signal.emit("Failed to retrieve content from the URL.")
    except Exception as e:
        self.text_appender.text_signal.emit(f"An error occurred while reading from the URL: {e}")

def extract_number(self, choice):
    num_str = ''.join(filter(str.isdigit, choice))
    if num_str:
        return int(num_str)
    else:
        number_words = {
            "one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
            "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10
        }
        choice_lower = choice.lower()
        for word, number in number_words.items():
            if word in choice_lower:
                return number
        return None

def start_assistant(self):
    # Start the AI assistant in a separate thread
    threading.Thread(target=self.assistant_main, daemon=True).start()

def assistant_main(self):
    while True:
        self.speak("Please say your search query.")
        self.text_appender.text_signal.emit("Prompting user for input...")
        query = self.recognize_speech()

        if query == "":
            self.text_appender.text_signal.emit("No query received, retrying...")
            continue

        if "exit program" in query:
            self.speak("Exiting program.")
            self.text_appender.text_signal.emit("Exiting program.")
            sys.exit()

        self.speak(f"Performing a search for {query}.")
        search_results = self.google_search(query)

        if not search_results:
            self.speak("No results found.")
            self.text_appender.text_signal.emit("No results found.")
            continue

        self.text_appender.text_signal.emit("Results:")  # Output results
        for i, result in enumerate(search_results[:5], 1):  # Show first five results
            self.text_appender.text_signal.emit(f"{i}. {result}")

        self.speak("Please choose one of the search results by saying the number.")

        # Get the user's choice
        choice = self.recognize_speech()
        number = self.extract_number(choice)

        if number is not None and 1 <= number <= len(search_results[:5]):
            url = search_results[number - 1]
            self.speak(f"Reading from result number {number}.")
            self.text_appender.text_signal.emit(f"Reading from: {url}")
            clipboard.copy(url)  # Copy URL to clipboard
            self.read_from_url(url)  # Read the content from the selected URL
        else:
            self.speak("Invalid choice. Please try again.")
            self.text_appender.text_signal.emit("Invalid choice. Retrying...")
            continue

if name == "main":

app = QApplication(sys.argv)

# Create the main window for the AI assistant
window = MainWindow()
window.show()



sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.