Beispiel #1
0
 def __init__(self, desktop_geometry):
     """
     Please see help(GoogleEarth) for more info
     """
     self.keyboard_commands = KeyboardCommands()
     self.screen_position = []
     self.screen_resize = []
     self.desktop_geometry = desktop_geometry
Beispiel #2
0
    def __init__(self, desktop_geo, screen_geo):
        """
        Please see help(GoogleEarth) for more info.
        """
        self.keyboard_commands = KeyboardCommands()

        self.new_width = None
        self.new_height = None

        self.desktop_geometry = desktop_geo
        self.screen_geometry = screen_geo
Beispiel #3
0
class Output():
    def __init__(self, cv_window):
        self.commands = KeyboardCommands()
        self.cv_window = cv_window

    def send_output(self):
        try:
            while True:
                self.commands.set_command(self.cv_window.get_output())
                self.commands.send_command()
        except KeyboardInterrupt:
            pass
Beispiel #4
0
# Continously gets output from model and executes as keyboard command to the
# Google Earth window
def send_output():
    # This try catch will kill the commands being sent if ctrl + c is used
    # in the terminal. It doesn't work w/ Google Earth window open because
    # the Google Earth window is constantly being targeted. Temporary.
    try:
        while True:
            commands.set_command(cv_window.get_output())
            commands.send_command()
    except KeyboardInterrupt:
        pass

if __name__ == "__main__":
    # Instantiate classes
    google_earth = GoogleEarth()
    cv_window = HandRecognition()
    commands = KeyboardCommands()

    #Starts Google Earth and positions it at fixed location with default size
    google_earth.start_google_earth()

    # Create thread to run the camera and process hand gestures
    first_thread = threading.Thread(target=cv_window.run_camera)
    first_thread.start()

    # Create second thread to get the output from model
    second_thread = threading.Thread(target=send_output)
    second_thread.start()
Beispiel #5
0
 def __init__(self):
     self.current_command = ''
     self.keyboard_commands = KeyboardCommands()
     self.screen_position = []
     self.resolution = ''
Beispiel #6
0
class GoogleEarth():
    def __init__(self):
        self.current_command = ''
        self.keyboard_commands = KeyboardCommands()
        self.screen_position = []
        self.resolution = ''

    def check_process_running(self, process_name):
        #Check if there is any running process that contains the given name processName.
        #Iterate over the all the running process
        for proc in psutil.process_iter():
            try:
                # Check if process name contains the given name string.
                if process_name.lower() in proc.name().lower():
                    return True
            except (psutil.NoSuchProcess, psutil.AccessDenied,
                    psutil.ZombieProcess):
                pass
        return False

    def initialize_google_earth(self):

        #Checking if Google Earth is already running
        if self.check_process_running('google-earth'):
            self.start_google_earth()

        else:
            #Start Google Earth if it is not already running and resize window
            os.system("nohup google-earth-pro </dev/null >/dev/null 2>&1 &")
            time.sleep(2)
            self.start_google_earth()

        sidebar_coords = self.keyboard_commands.locate_image(
            'clicked_sidebar.png')

        if sidebar_coords:
            self.keyboard_commands.click_with_location(sidebar_coords)
            time.sleep(2)
            self.keyboard_commands.drag_mouse(80, 200, 1)
            self.keyboard_commands.click_without_location()

        else:
            os.system("wmctrl -a Google Earth Pro")
            sidebar_coords = self.keyboard_commands.locate_image(
                'unclicked_sidebar.png')
            self.keyboard_commands.move_mouse_to_coords(sidebar_coords)
            self.keyboard_commands.drag_mouse(80, 200, 1)
            self.keyboard_commands.click_without_location()

    def start_google_earth(self):
        self.set_screen_resolution()
        comm = "wmctrl -r 'Google Earth' -e 0," + str(int(self.resolution[0])) + "," + \
                str(int(self.resolution[1])) + "," + str(self.screen_position[0]) + "," + \
                str(self.screen_position[1])
        os.system(comm)
        time.sleep(2)

    def set_screen_resolution(self):
        output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',
                                  shell=True,
                                  stdout=subprocess.PIPE).communicate()[0]
        self.resolution = output.split()[0].split(b'x')

        self.screen_position.append(int((int(self.resolution[0]) * (1 / 2))))
        self.screen_position.append(int((int(self.resolution[1]) * (2 / 3))))
        self.resolution[0] = (int(self.resolution[0]) /
                              2) - (self.screen_position[0] / 2)
        self.resolution[1] = (int(self.resolution[1]) /
                              2) - (self.screen_position[1] / 2)

    def get_screen_resolution(self):
        return self.resolution

    def get_screen_position(self):
        return self.screen_position
Beispiel #7
0
class GoogleEarth():
    """
    This is a class that starts Google Earth and performs operations
    on the Google Earth window.

    Attributes:
        keyboard_commands : KeyboardCommand class object
        new_width (int) : Width for repositioning Google Earth window
        new_height (int) : Height for repositioning Google Earth window
        desktop_geometry : Available screen geometry based on current resolution
        screen_geometry : Total screen geometry based on current resolution

    Args:
        desktop_geo : Available screen geometry
        screen_geo : Total screen geometry
    """
    def __init__(self, desktop_geo, screen_geo):
        """
        Please see help(GoogleEarth) for more info.
        """
        self.keyboard_commands = KeyboardCommands()

        self.new_width = None
        self.new_height = None

        self.desktop_geometry = desktop_geo
        self.screen_geometry = screen_geo

    def check_process_running(self, process_name):
        """
        Checks if there are any processes currently running that contain a given name.

        Parameters:
            process_name (string) : Name of the process to be checked

        Returns:
            bool: Returns True if process found, else returns False
        """
        # Check if there is any running process that contains the given name processName.
        # Iterate over the all the running process
        for proc in psutil.process_iter():
            try:
                if process_name.lower() in proc.name().lower():
                    return True
            except (psutil.NoSuchProcess, psutil.AccessDenied,
                    psutil.ZombieProcess):
                pass
        return False

    def initialize_google_earth(self):
        """
        Checks if Google Earth is running and starts Google Earth if it is not running.
        Calls toggle_button_off to check if Google Earth toolbar is open.
        """
        #Checking if Google Earth is already running
        if self.check_process_running('google-earth'):
            self.start_google_earth()

        else:
            #Start Google Earth if it is not already running and resize window
            os.system("nohup google-earth-pro </dev/null >/dev/null 2>&1 &")
            time.sleep(2)
            self.start_google_earth()
        # Calls toggle_button_off to check if toolbar open
        self.toggle_button_off()

    def start_google_earth(self):
        """
        Checks if Google Earth is maximized and then sets width and height for positioning
        Google Earth. Repositions Google Earth to upper 3/4 of screen and resizes Google
        Earth to take up width of available screen.
        """
        # Calls check_if_fullscreen to check if Google Earth is maximized
        self.check_if_fullscreen()
        # Calls set_screen_resolution to set new width and height based on current resolution
        self.set_screen_resolution()
        # Sets command to reposition and resize Google Earth window
        comm = "wmctrl -r 'Google Earth' -e 0,0,0," + str(int(self.new_width)) + "," + \
                str(int(self.new_height))
        # Sends command to reposition and resize Google Earth window
        os.system(comm)
        # Pause to make sure command issued successfully
        time.sleep(2)

    def set_screen_resolution(self):
        """
        Sets the Google Earth window width and height values based on monitor resolution.
        """
        # Sets new width for Google Earth based on available screen width
        self.new_width = self.desktop_geometry.width()
        # Sets new height for Google Earth to be 3/4 of available height minus offset
        # for window title
        self.new_height = ((self.desktop_geometry.height() * (3 / 4)) - 37)

    def reposition_earth_small(self):
        """
        Checks if Google Earth is maximized, then repositions and resizes Google Earth to
        have half the width of current available screen space and 3/4 of available screen
        height.
        """
        # Checks if Google Earth is maximized
        self.check_if_fullscreen()
        # Sets command to reposition and resize Google Earth window
        comm = "wmctrl -r 'Google Earth' -e 0,0,0," + str(int(self.new_width / 2)) + "," + \
                str(int(self.new_height))
        # Sends command to reposition and resize Google Earth window
        os.system(comm)
        # Pause to make sure command issued successfully
        time.sleep(2)

    def reposition_earth_large(self):
        """
        Checks if Google Earth is maximized, then repositions and resizes Google Earth to
        have the fulls width of current available screen space and 3/4 of available screen
        height.
        """
        # Checks if Google Earth is maximized
        self.check_if_fullscreen()
        # Sets command to reposition and resize Google Earth window
        comm = "wmctrl -r 'Google Earth' -e 0,0,0," + str(int(self.new_width)) + "," + \
                str(int(self.new_height))
        # Sends command to reposition and resize Google Earth window
        os.system(comm)
        # Pause to make sure command issued successfully
        time.sleep(2)

    def toggle_button_off(self):
        """
        Closes sidebar in Google Earth that cause problems when sending commands
        to the Google Earth window.
        """
        # Checks to see if the Google Earth sidebar is open
        sidebar_coords = self.keyboard_commands.locate_image(
            'images/clicked_sidebar.png')
        # If the sidebar is open, call close_sidebar to close it
        if sidebar_coords:
            self.keyboard_commands.close_sidebar()

    def start_up_tips(self):
        """
        Checks if Start-up Tips widnow is open in Google Earth.

        Returns:
            bool: Returns True if Google Earth Start Up Tips window open, else returns False
        """
        # Checks to see if Google Earth start up tips window is open
        start_up_tips_coords = self.keyboard_commands.locate_image(
            'images/close.png')
        # If the Google Earth start up tips window is open, return True, else False
        if start_up_tips_coords:
            return True
        else:
            return False

    def check_if_fullscreen(self):
        """
        Checks if Google Earth window is maximized, which prevents Google Earth from
        receiving resize commands. If Google Earth window is maximized, minimizes the
        window.
        """
        # Checks if Google Earth window is maximized
        fullscreen = self.keyboard_commands.locate_image(
            'images/fullscreen.png')
        # If the window is maximized, minimizes it
        if fullscreen:
            self.keyboard_commands.click_with_location(fullscreen)
            time.sleep(2)

    def close_earth(self):
        """
        Closes the Google Earth window.
        """
        os.system("wmctrl -c Google Earth Pro")
Beispiel #8
0
 def __init__(self, cv_window):
     self.commands = KeyboardCommands()
     self.cv_window = cv_window
Beispiel #9
0
class GoogleEarth():
    """
    This is a class that starts Google Earth and performs operations
    on the Google Earth window.

    Attributes:
        keyboard_commands : Instantiates keyboard command class object for sending
            commands to Google Earth
        screen_position (list) : Position values for moving and resizing windows based
            on screen resolution
        resolution (string) : Resolution of the current monitor
    """
    def __init__(self, desktop_geometry):
        """
        Please see help(GoogleEarth) for more info
        """
        self.keyboard_commands = KeyboardCommands()
        self.screen_position = []
        self.screen_resize = []
        self.desktop_geometry = desktop_geometry

    def check_process_running(self, process_name):
        """
        Checks if there are any processes currently running that contain a given name.

        Parameters:
        process_name (string) : Name of the process to be checked

        Returns:
        bool: Returns True if process found, else returns False
        """
        # Check if there is any running process that contains the given name processName.
        # Iterate over the all the running process
        for proc in psutil.process_iter():
            try:
                if process_name.lower() in proc.name().lower():
                    return True
            except (psutil.NoSuchProcess, psutil.AccessDenied,
                    psutil.ZombieProcess):
                pass
        return False

    def initialize_google_earth(self):
        """
        Checks if Google Earth is running and starts Google Earth if it is not running.
        Following start up, closes two Google Earth toolbars.
        """
        #Checking if Google Earth is already running
        if self.check_process_running('google-earth'):
            self.start_google_earth()

        else:
            #Start Google Earth if it is not already running and resize window
            os.system("nohup google-earth-pro </dev/null >/dev/null 2>&1 &")
            time.sleep(2)
            self.start_google_earth()

        self.toggle_buttons_off()

    def start_google_earth(self):
        """
        Gets the current screen resolution and starts Google Earth. Repositions Google Earth
        in the center of the screen with a size determined by the current screen resolution.
        """
        self.set_screen_resolution()

        sidebar_coords = self.keyboard_commands.locate_image('fullscreen.png')

        if sidebar_coords:
            self.keyboard_commands.click_with_location(sidebar_coords)
            time.sleep(2)

        comm = "wmctrl -r 'Google Earth' -e 0,0,0," + str(int(self.screen_resize[0])) + "," + \
                str(int(self.screen_resize[1]))
        os.system(comm)
        time.sleep(2)

    def set_screen_resolution(self):
        """
        Sets the Google Earth window resize and screen position based on monitor resolution.
        """
        #print(self.desktop_geometry.width(), self.desktop_geometry.height())
        self.screen_resize.append(self.desktop_geometry.width())
        self.screen_resize.append(self.desktop_geometry.height() * (3 / 4))

        #print(self.screen_resize[0], self.screen_resize[1])

        self.screen_position.append((self.desktop_geometry.width() / 2))
        self.screen_position.append((self.desktop_geometry.height() / 2))

    def reposition_earth_small(self):
        # REPLACE with hotkey for ALT + F10 to toggle full screen
        sidebar_coords = self.keyboard_commands.locate_image('fullscreen.png')

        if sidebar_coords:
            self.keyboard_commands.click_with_location(sidebar_coords)
            time.sleep(2)

        comm = "wmctrl -r 'Google Earth' -e 0,0,0," + str(int(self.screen_resize[0] / 2)) + "," + \
                str(int(self.screen_resize[1]))
        os.system(comm)
        time.sleep(2)

    def reposition_earth_large(self):
        # REPLACE with hotkey for ALT + F10 to toggle full screen
        sidebar_coords = self.keyboard_commands.locate_image('fullscreen.png')

        if sidebar_coords:
            self.keyboard_commands.click_with_location(sidebar_coords)
            time.sleep(2)

        comm = "wmctrl -r 'Google Earth' -e 0,0,0," + str(int(self.screen_resize[0])) + "," + \
                str(int(self.screen_resize[1]))
        os.system(comm)
        time.sleep(2)

    def toggle_buttons_off(self):
        """
        Toggles two toolbars in Google Earth that cause problems when sending commands
        to the Google Earth window.
        """
        sidebar_coords = self.keyboard_commands.locate_image('close.png')

        if sidebar_coords:
            self.keyboard_commands.click_with_location(sidebar_coords)
            time.sleep(2)

        sidebar_coords = self.keyboard_commands.locate_image(
            'clicked_sidebar.png')

        if sidebar_coords:
            self.keyboard_commands.click_with_location(sidebar_coords)
            time.sleep(2)

    def get_screen_resize(self):
        """
        Returns the values used to resize the Google Earth window.

        Returns:
            resolution (str) : Current Google Earth window size.
        """
        return self.screen_resize

    def get_screen_position(self):
        """
        Returns the Google Earth window X and Y positions.

        Returns:
            screen_position (list) : X and Y positions determined by current screen resolution.
        """
        return self.screen_position

    def close_earth(self):
        """
        Closes the Google Earth window.
        """
        os.system("wmctrl -c Google Earth Pro")
Beispiel #10
0
    def __init__(self, earth, desk_geo, screen_geo, *args, **kwargs):
        """
        Please see help(MainWindow) for more info.
        """
        super(MainWindow, self).__init__(*args, **kwargs)
        # Instantiate KeyboardCommands class
        self.commands = KeyboardCommands()
        # Will hold hand_recognition QtCapture class
        self.capture = None
        # Will hold camera object for OpenCV
        self.camera = None
        # Will hold thread for issuing GE commands
        self.command_thread = None
        # Make Qt gesture icon window frameless
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        # Get resolution, window size, and offsets for positioning
        self.google_earth = earth
        # Variables for popup windows
        self.popup_window = None
        self.popup_title = ""
        self.popup_text = ""
        # Available screen geometry
        self.desktop = desk_geo
        # Total screen geometry
        self.screen = screen_geo
        # Sets gesture icon window to be 1/4 of available screen space
        self.qt_window_height = int(self.desktop.height() * 1 / 4)
        # Set geometry of Qt gesture icon window
        self.setGeometry(
            QtWidgets.QStyle.alignedRect(
                QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
                QtCore.QSize(self.desktop.width(), self.qt_window_height),
                self.desktop))
        # Create layouts for organizing Qt gesture icon window
        self.layout = QVBoxLayout()
        self.layout1 = QHBoxLayout()
        self.layout2 = QHBoxLayout()
        self.layout3 = QHBoxLayout()
        # Dictionary to hold labels once they are created
        self.label_dict = dict()
        # Lists hold gesture icon file names and gesture icon titles
        self.image_list = [
            'images/index_up.png', 'images/v_sign.png',
            'images/thumb_left.png', 'images/thumb_right.png',
            'images/fist.png', 'images/five_wide.png', 'images/palm.png',
            'images/shaka.png'
        ]
        self.title_list = [
            'Move Up', 'Move Down', 'Move Left', 'Move Right', 'Zoom In',
            'Zoom Out', 'Tilt Up', 'Tilt Down'
        ]
        # Create and add 6 labels containing hand gesture image to layout2 and 6
        # labels with the gesture descriptions to layout1
        for num in range(0, 8):
            # Each label is created to hold gesture icon image
            self.label = QLabel(self)
            # Pixmap is created with the current gesture icon image
            self.pixmap = QPixmap(self.image_list[num])
            # Breakpoints to scale size of gesture icons for different resolutions
            if self.screen.width() >= 2560:
                self.pixmap = self.pixmap.scaledToWidth(225)
            elif self.screen.width() >= 1920:
                self.pixmap = self.pixmap.scaledToWidth(185)
            elif self.screen.width() > 1280 and self.screen.height() >= 1200:
                self.pixmap = self.pixmap.scaledToWidth(175)
            elif self.screen.width() > 800 and self.screen.height() >= 1024:
                self.pixmap = self.pixmap.scaledToWidth(125)
            elif self.screen.width() > 800:
                self.pixmap = self.pixmap.scaledToWidth(100)
            else:
                self.pixmap = self.pixmap.scaledToWidth(50)
            # Assigns gesture icon image to the current label
            self.label.setPixmap(self.pixmap)
            # Create gesture title label for the image
            self.label_title = QLabel(self.title_list[num])
            # Store current icon image label in dictionary
            self.label_dict[num] = self.label
            # Place current icon image label in layout
            self.layout2.addWidget(self.label_dict[num],
                                   alignment=QtCore.Qt.AlignCenter)
            # Place current icon image title label in layout
            self.layout1.addWidget(self.label_title,
                                   alignment=QtCore.Qt.AlignCenter)

        # Create state machine to reliably handle state changes during threading
        self.state_machine = QStateMachine()
        # Create button to handle state changes when pressed
        self.state_button = QPushButton(self)
        self.state_button.setStyleSheet("background-color: silver")
        # Connect button released signal to check_state slot
        self.state_button.released.connect(self.check_state)
        # Create on state for state machine
        self.on = QState()
        # Create off state for state machine
        self.off = QState()
        # Add transition for on state to off state when offSignal is emitted
        self.on.addTransition(self.offSignal, self.off)
        # Add transition for on state to on state when state_button clicked signal emitted
        self.on.addTransition(self.state_button.clicked, self.on)
        # Add transition for off state to on state when onSignal is emitted
        self.off.addTransition(self.onSignal, self.on)
        # Assign text property to state_button in on state
        self.on.assignProperty(self.state_button, "text",
                               "Start Gesture Navigation")
        # Assign text property to state_button in off state
        self.off.assignProperty(self.state_button, "text",
                                "Stop Gesture Navigation")
        # Add off state to state machine
        self.state_machine.addState(self.off)
        # Add on state to state machine
        self.state_machine.addState(self.on)
        # Set state machine initial state to on
        self.state_machine.setInitialState(self.on)
        # State state machine
        self.state_machine.start()
        # Create gesture tips button and connect it to start_gesture_tips slot
        self.tips_button = QPushButton("Gesture Navigation Tips")
        self.tips_button.setStyleSheet("background-color: silver")
        self.tips_button.pressed.connect(self.start_gesture_tips)
        # Create exit button and connect it to exit slot
        self.exit_button = QPushButton("Exit Program")
        self.exit_button.setStyleSheet("background-color: silver")
        self.exit_button.pressed.connect(self.exit)
        # Add tips, state, and exit button to layout 3
        self.layout3.addWidget(self.tips_button)
        self.layout3.addWidget(self.state_button)
        self.layout3.addWidget(self.exit_button)
        # Add layout 1, 2, and 3 to layout
        self.layout.addLayout(self.layout1)
        self.layout.addLayout(self.layout2)
        self.layout.addLayout(self.layout3)
        # Create widget to hold layout, add layout to widget
        self.widget = QWidget()
        self.widget.setLayout(self.layout)
        # Set widget with layouts as central widget
        self.setCentralWidget(self.widget)
Beispiel #11
0
    def __init__(self, earth, *args, **kwargs):
        """
        Please see help(MainWindow) for more info
        """
        super(MainWindow, self).__init__(*args, **kwargs)
        # Instantiate KeyboardCommands class
        self.commands = KeyboardCommands()
        # Flag for stopping commands to GE window
        self.stop_commands = False
        # Will hold hand_recognition QtCapture class
        self.capture = None
        # Make Qt gesture icon window frameless
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        # Get resolution, window size, and offsets for positioning
        self.google_earth = earth
        self.new_position = self.google_earth.get_screen_position()
        self.window_resize = self.google_earth.get_screen_resize()
        self.title_bar_offset = 35
        # Set geometry of Qt gesture icon window
        # (this window is the parent of all other Qt windows)
        self.setGeometry(
            QtWidgets.QStyle.alignedRect(
                QtCore.Qt.LeftToRight,
                QtCore.Qt.AlignCenter,
                # Width of Qt gesture window based on width of GE window
                QtCore.QSize(int(self.window_resize[0]), 100),
                QtWidgets.qApp.desktop().availableGeometry()))

        # Initialize threadpool object
        self.threadpool = QThreadPool()
        # Create layouts for organizing Qt gesture icon window
        self.layout = QVBoxLayout()
        self.layout1 = QHBoxLayout()
        self.layout2 = QHBoxLayout()
        self.layout3 = QHBoxLayout()

        self.label_dict = dict()
        self.image_list = [
            'index_up.png', 'fist.png', 'palm.png', 'thumb_left.png',
            'thumb_right.png', 'five_wide.png'
        ]
        self.title_list = [
            'Move Up', 'Zoom In', 'Placeholder', 'Move Left', 'Move Right',
            'Zoom Out'
        ]
        # Create and add 6 labels containing hand gesture image to layout2 and 6
        # labels with the gesture descriptions to layout1
        for num in range(0, 6):

            self.label = QLabel(self)
            self.pixmap = QPixmap(self.image_list[num])
            self.pixmap = self.pixmap.scaledToWidth(100)
            self.label.setPixmap(self.pixmap)

            self.label_title = QLabel(self.title_list[num])
            # Modify left margins for images and labels positioning
            if num == 0:
                self.label.setContentsMargins(25, 0, 0, 0)
                self.label_title.setContentsMargins(35, 0, 0, 0)
            elif num == 5:
                self.label.setContentsMargins(25, 0, 0, 0)
                self.label_title.setContentsMargins(35, 0, 0, 0)
            else:
                self.label.setContentsMargins(15, 0, 0, 0)
                self.label_title.setContentsMargins(25, 0, 0, 0)

            self.label_dict[num] = self.label

            self.layout2.addWidget(self.label_dict[num])

            self.layout1.addWidget(self.label_title)
        # Create start button and connect it to start_opencv function
        self.start_button = QPushButton("Start Video")
        self.start_button.setStyleSheet("background-color: silver")
        self.start_button.pressed.connect(self.start_opencv)
        # Create stop button and connect it to stop_opencv function
        self.stop_button = QPushButton("Stop Video")
        self.stop_button.setStyleSheet("background-color: silver")
        self.stop_button.pressed.connect(self.stop_opencv)
        # Create stop button and connect it to stop_opencv function
        self.exit_button = QPushButton("Exit Program")
        self.exit_button.setStyleSheet("background-color: silver")
        self.exit_button.pressed.connect(self.exit)
        # Add start and stop button to layout 3
        self.layout3.addWidget(self.start_button)
        self.layout3.addWidget(self.stop_button)
        self.layout3.addWidget(self.exit_button)
        # Add layout 1, 2, and 3 to layout
        self.layout.addLayout(self.layout1)
        self.layout.addLayout(self.layout2)
        self.layout.addLayout(self.layout3)
        # Create widget to hold layout, add layout to widget
        self.widget = QWidget()
        self.widget.setLayout(self.layout)
        # Set widget with layouts as central widget
        self.setCentralWidget(self.widget)
Beispiel #12
0
class MainWindow(QMainWindow):
    """
    This is a class that creates a PyQt5 window containing start, stop, and exit buttons as
    well as gesture icons. The window is the main window for our program.

    Attributes:
        commands: Instantiate keyboard command class object for sending commands to Google Earth
        stop_commands (bool) : Flag for enabling and disabling worker thread
        capture: Instantiate QtCapture class when start video button is pressed
        google_earth: GoogleEarth class object
        new_position (int) : X and Y position values of Google Earth window
        window_resize (int) : Width and height of resized Google Earth window
        title_bar_offset (int) : Offset value to account for windowless frame when positioning
            window
        threadpool : Instantiate QThreadpool class
        layout : Instantiate QVBoxLayout or QHBoxLayout classes, used for aligning images and
            buttons
        label_dict (dict) : Contains labels that are used to display gesture icon images
        image_list (list) : Contains names of image files for gesture icon images
        title_list (list) : Contains names of titles for corresponding gesture images
        label : Instantiate QLabel class, labels used to hold gesture icon images and text
        button : Instantiate QPushButton class, buttons used to start, stop, and exit program
        widget : Insantiate QWidget class, contains and displays labels and buttons

    Args:
        earth : GoogleEarth class object

    """
    def __init__(self, earth, *args, **kwargs):
        """
        Please see help(MainWindow) for more info
        """
        super(MainWindow, self).__init__(*args, **kwargs)
        # Instantiate KeyboardCommands class
        self.commands = KeyboardCommands()
        # Flag for stopping commands to GE window
        self.stop_commands = False
        # Will hold hand_recognition QtCapture class
        self.capture = None
        # Make Qt gesture icon window frameless
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        # Get resolution, window size, and offsets for positioning
        self.google_earth = earth
        self.new_position = self.google_earth.get_screen_position()
        self.window_resize = self.google_earth.get_screen_resize()
        self.title_bar_offset = 35
        # Set geometry of Qt gesture icon window
        # (this window is the parent of all other Qt windows)
        self.setGeometry(
            QtWidgets.QStyle.alignedRect(
                QtCore.Qt.LeftToRight,
                QtCore.Qt.AlignCenter,
                # Width of Qt gesture window based on width of GE window
                QtCore.QSize(int(self.window_resize[0]), 100),
                QtWidgets.qApp.desktop().availableGeometry()))

        # Initialize threadpool object
        self.threadpool = QThreadPool()
        # Create layouts for organizing Qt gesture icon window
        self.layout = QVBoxLayout()
        self.layout1 = QHBoxLayout()
        self.layout2 = QHBoxLayout()
        self.layout3 = QHBoxLayout()

        self.label_dict = dict()
        self.image_list = [
            'index_up.png', 'fist.png', 'palm.png', 'thumb_left.png',
            'thumb_right.png', 'five_wide.png'
        ]
        self.title_list = [
            'Move Up', 'Zoom In', 'Placeholder', 'Move Left', 'Move Right',
            'Zoom Out'
        ]
        # Create and add 6 labels containing hand gesture image to layout2 and 6
        # labels with the gesture descriptions to layout1
        for num in range(0, 6):

            self.label = QLabel(self)
            self.pixmap = QPixmap(self.image_list[num])
            self.pixmap = self.pixmap.scaledToWidth(100)
            self.label.setPixmap(self.pixmap)

            self.label_title = QLabel(self.title_list[num])
            # Modify left margins for images and labels positioning
            if num == 0:
                self.label.setContentsMargins(25, 0, 0, 0)
                self.label_title.setContentsMargins(35, 0, 0, 0)
            elif num == 5:
                self.label.setContentsMargins(25, 0, 0, 0)
                self.label_title.setContentsMargins(35, 0, 0, 0)
            else:
                self.label.setContentsMargins(15, 0, 0, 0)
                self.label_title.setContentsMargins(25, 0, 0, 0)

            self.label_dict[num] = self.label

            self.layout2.addWidget(self.label_dict[num])

            self.layout1.addWidget(self.label_title)
        # Create start button and connect it to start_opencv function
        self.start_button = QPushButton("Start Video")
        self.start_button.setStyleSheet("background-color: silver")
        self.start_button.pressed.connect(self.start_opencv)
        # Create stop button and connect it to stop_opencv function
        self.stop_button = QPushButton("Stop Video")
        self.stop_button.setStyleSheet("background-color: silver")
        self.stop_button.pressed.connect(self.stop_opencv)
        # Create stop button and connect it to stop_opencv function
        self.exit_button = QPushButton("Exit Program")
        self.exit_button.setStyleSheet("background-color: silver")
        self.exit_button.pressed.connect(self.exit)
        # Add start and stop button to layout 3
        self.layout3.addWidget(self.start_button)
        self.layout3.addWidget(self.stop_button)
        self.layout3.addWidget(self.exit_button)
        # Add layout 1, 2, and 3 to layout
        self.layout.addLayout(self.layout1)
        self.layout.addLayout(self.layout2)
        self.layout.addLayout(self.layout3)
        # Create widget to hold layout, add layout to widget
        self.widget = QWidget()
        self.widget.setLayout(self.layout)
        # Set widget with layouts as central widget
        self.setCentralWidget(self.widget)

    def start_opencv(self):
        """
        Slot function for the start button signal. Instantiates Qt opencv window if not created,
        then starts and shows the window. Once the window is opened, starts worker thread to send
        commands to Google Earth.
        """
        # If opencv window not initialized,
        if not self.capture:
            # Instantiate QtCapture class, set parent and window flags
            self.capture = QtCapture(self.google_earth)
            self.capture.setParent(self)
            self.capture.setWindowFlags(QtCore.Qt.Tool)
            self.capture.setWindowTitle("OpenCV Recording Window")
            self.capture.setGeometry(
                int(self.window_resize[0] + self.new_position[0]),
                int(self.window_resize[1] + self.title_bar_offset), -1, -1)

        # Start video capture and show it
        self.capture.start()
        self.capture.show()
        # Set stop command flag, create worker attached to send_output
        # function, start worker as new thread
        self.stop_commands = False
        worker_one = Worker(self.send_output)
        self.threadpool.start(worker_one)

    def stop_opencv(self):
        """
        Slot function for stop button signal. Stops Qt timer in opencv loop and sets
        stop_commands to True to kill worker thread.
        """
        # Stop timer in hand_recognition, set flag to kill worker thread
        self.capture.stop()
        self.stop_commands = True

    def exit(self):
        """
        Slot function for exit button signal. Sets stop_commands to True to kill worker
        thread, then calls close_earth to close Google Earth window, and finally terminates
        the QApplication.
        """
        self.stop_commands = True
        self.google_earth.close_earth()
        QtCore.QCoreApplication.instance().quit()

    def send_output(self):
        """
        Gets current output from opencv window and sends the command to the Google Earth window
        """
        # While stop command false, get commands from hand_recognition
        # and send commands to Google Earth window
        while True:
            self.commands.set_command(self.capture.get_output())
            self.commands.send_command()

            if self.stop_commands:
                break