Python;使用语音命令控制车辆的应用程序

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

这是我的代码

import sys
import speech_recognition as sr
import pygame
import time
from pygame.locals import QUIT

class CarControl:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((1000, 900))
        self.clock = pygame.time.Clock()

        self.engine_running = False
        self.direction = (0, 0)  # Initially, the car is not moving
        self.speed = 5  # Initial speed

        self.car_image = pygame.image.load("car.png")
        self.car_rect = self.car_image.get_rect()
        self.car_rect.center = (200, 150)
        
        # Visual indicators for climate and multimedia control
        self.climate_indicator = False
        self.volume_level = 50  # Initial volume

        self.handle_command()

    # Rest of the methods remain unchanged

    def start_engine(self):
        if not self.engine_running:
            self.engine_running = True
            print("Engine has started.")

    def stop_engine(self):
        if self.engine_running:
            self.engine_running = False
            self.direction = (0, 0)  # Stop the car when the engine is off
            print("Engine has stopped.")

    def turn_left(self):
        if self.engine_running:
            self.direction = (-self.speed, 0)  # Set left movement
            print("Car is turning left.")
        else:
            print("Engine must be started to turn.")

    def turn_right(self):
        if self.engine_running:
            self.direction = (self.speed, 0)  # Set right movement
            print("Car is turning right.")
        else:
            print("Engine must be started to turn.")

    def go_forward(self):
        if self.engine_running:
            self.direction = (0, -self.speed)  # Set forward movement
            print("Car is moving forward.")
        else:
            print("Engine must be started to move forward.")

    def go_backward(self):
        if self.engine_running:
            self.direction = (0, self.speed)  # Set backward movement
            print("Car is moving backward.")
        else:
            print("Engine must be started to move backward.")

    def set_speed(self, speed):
        self.speed = speed
        print(f"Speed has been set to {speed}.")

    def draw_climate_indicator(self):
        # Draw visual indicator for climate control
        pygame.draw.circle(self.screen, (0, 255, 0) if self.climate_indicator else (255, 0, 0), (50, 50), 20)

    def draw_volume_indicator(self):
        # Draw visual indicator for volume
        pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(800, 50, 20, 100))
        pygame.draw.rect(self.screen, (0, 0, 255), pygame.Rect(800, 150 - self.volume_level, 20, self.volume_level))

    def control_volume(self, direction):
        # Integrate logic for volume control here
        if direction == "increase":
            self.volume_level = min(self.volume_level + 10, 100)
            print("Volume has been increased.")
            # Add code to increase the sound system volume
        elif direction == "decrease":
            self.volume_level = max(self.volume_level - 10, 0)
            print("Volume has been decreased.")
            # Add code to decrease the sound system volume

    def control_climate(self, command):
        # Integrate logic for climate control here
        if "turn on climate" in command:
            self.climate_indicator = True
            print("Climate control has been turned on.")
            # Add code to turn on the climate control
        elif "turn off climate" in command:
            self.climate_indicator = False
            print("Climate control has been turned off.")

    def handle_command(self):
        recognizer = sr.Recognizer()
        running = True

        while running:
            for event in pygame.event.get():
                if event.type == QUIT:
                    running = False
                    
            if self.engine_running:
               self.direction = (0, -self.speed)
               self.car_rect.x += self.direction[0]
               self.car_rect.y += self.direction[1]

            with sr.Microphone() as source:
                print("Listening...")
                recognizer.adjust_for_ambient_noise(source)
                audio = recognizer.listen(source)

            try:
                text = recognizer.recognize_google(audio, language='en-US')
                if "start" in text:
                    self.start_engine()
                elif "stop" in text:
                    self.stop_engine()
                elif "left" in text:
                    self.turn_left()
                elif "right" in text:
                    self.turn_right()
                elif "forward" in text:
                    self.go_forward()
                elif "backward" in text:
                    self.go_backward()
                elif "speed" in text:
                    words = text.split()
                    for word in words:
                        if word.isdigit():
                            self.set_speed(int(word))
                elif "increase volume" in text:
                    self.control_volume("increase")
                elif "decrease volume" in text:
                    self.control_volume("decrease")
                elif "turn on climate" in text or "turn off climate" in text:
                    self.control_climate(text)
            except sr.UnknownValueError:
                print("Unknown command")
            except sr.RequestError:
                print("Failed to recognize speech.")

            self.car_rect.move_ip(self.direction)
            self.car_rect.x += self.direction[0] * self.speed
            self.car_rect.y += self.direction[1] * self.speed

            self.screen.fill((0, 0, 0))
            self.screen.blit(self.car_image, self.car_rect)
            self.draw_climate_indicator()
            self.draw_volume_indicator()
            pygame.display.flip()
          
            time.sleep(0.1)
            self.clock.tick(60)

        pygame.quit()

if __name__ == "__main__":
    car_control = CarControl()
    car_control.handle_command()

        

我希望汽车在您发出声音命令时连续移动,但每个“语音命令”仅移动一次。我刚刚开始学习使用语音命令和 Pygame (如您所见),所以我很感谢您的帮助。

我希望汽车在您发出声音命令时连续移动,但每个“语音命令”仅移动一次。我刚刚开始学习使用语音命令和 Pygame (如您所见),所以我很感谢您的帮助。

python pygame speech-recognition
1个回答
0
投票

Recognizer.recognize_google
等待新输入。该函数在接收并处理新的音频流之前不会返回。你不断地暂停游戏循环并等待新的输入。因此,汽车不会连续行驶。我建议在线程中进行语音识别。

© www.soinside.com 2019 - 2024. All rights reserved.