def handle_custom_commands(self, entry):
        if entry['command'] == 'train':
            box = entry['params']['box']
            sample_count = entry['params']['sample_count']
            name = entry['params']['name']
            if not self.__last_surface == None:
                colorlist = self.get_colorlist(name)
                if colorlist == None:
                    raise Exception(
                        "Specified colorlist with filename %s not found!" %
                        name)
                colorlist.empty_list()
                for i in range(sample_count):
                    x_range = box[1][0] - box[0][0]
                    y_range = box[1][1] - box[0][1]
                    x = int(random.random() * x_range) + box[1][0]
                    y = int(random.random() * y_range) + (box[1][1] - y_range)

                    #Only add if pixel location is valid:
                    if x > self.__last_surface.get_width() or x < 0:
                        continue
                    if y > self.__last_surface.get_height() or y < 0:
                        continue
                    colorlist.add_color(self.__last_surface.get_at((x, y)))
                colorlist.filter_outliers()
                colorlist.save()
Пример #2
0
    def handle_custom_commands(self, entry):
        if entry['command'] == 'train':
            box = entry['params']['box']  
            sample_count = entry['params']['sample_count']  
            name = entry['params']['name']  
            if not self.__last_surface == None:
                colorlist = self.get_colorlist(name)
                if colorlist == None:
                    raise Exception("Specified colorlist with filename %s not found!" % name)
                colorlist.empty_list()
                for i in range(sample_count):
                    x_range = box[1][0] - box[0][0]
                    y_range = box[1][1] - box[0][1]
                    x = int(random.random() * x_range) + box[1][0]
                    y = int(random.random() * y_range) + (box[1][1] - y_range)

                    #Only add if pixel location is valid:
                    if x > self.__last_surface.get_width() or x < 0:
                        continue
                    if y > self.__last_surface.get_height() or y < 0:
                        continue
                    colorlist.add_color(self.__last_surface.get_at((x, y)))
                colorlist.filter_outliers()
                colorlist.save()
    def handle_events(self):
        for event in pygame.event.get():
            #Handle keyboard input:
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_q:
                    print "Quit program..."
                    self.__run = False
                if event.key == pygame.K_f:
                    print "Freezing image..."
                    self.__freeze = not self.__freeze
                if event.key == pygame.K_s:
                    print "Saving current selected color list..."
                    self.__colorlist.save()
                if event.key == pygame.K_k:
                    print "Saving all color lists..."
                    for colorlist in self.__colorlist_list:
                        colorlist.save()
                if event.key == pygame.K_l:
                    print "(Re)loading current selected color list..."
                    self.__colorlist.load()
                if event.key == pygame.K_d:
                    print "Deleting color list..."
                    self.__colorlist.empty_list()
                if event.key == pygame.K_a:
                    self.__colorlist.set_cube_size(
                        self.__colorlist.get_cube_size() + 1)
                    print "Increased cube size to: %d" % self.__colorlist.get_cube_size(
                    )
                if event.key == pygame.K_z:
                    self.__colorlist.set_cube_size(
                        self.__colorlist.get_cube_size() - 1)
                    print "Decreased cube size to: %d" % self.__colorlist.get_cube_size(
                    )
                if event.key == pygame.K_u:
                    self.__colorlist.remove_last()
                    print "Removed previous color, color list now holds %d colors." % len(
                        self.__colorlist.get_list())
                if event.key == pygame.K_p:
                    self.select_next_colorlist()
                if event.key == pygame.K_o:
                    self.select_previous_colorlist()
                if event.key == pygame.K_r:
                    if self.__show_all:
                        print "Displaying all colors..."
                        self.__show_all = False
                    else:
                        print "NOT Displaying all colors..."
                        self.__show_all = True
                if event.key == pygame.K_t:
                    if self.__show_detect:
                        print "Toggling show detecting mode OFF"
                        self.__show_detect = False
                    else:
                        print "Toggling show detecting mode ON"
                        self.__show_detect = True

            #Handle mouse input:
            if event.type == pygame.MOUSEBUTTONUP:
                (m_x, m_y) = pygame.mouse.get_pos()
                if m_x < 0 or m_x > self.__source_surface.get_width() \
                        or m_y < 0 or m_y > self.__source_surface.get_height():
                    return

                current_color = self.__source_surface.get_at((m_x, m_y))

                if self.__colorlist.add_color(current_color):
                    print "Adding RGB color to list: (%d, %d, %d), now holds %d item(s)." \
                            % (current_color[0], current_color[1], current_color[2], len(self.__colorlist.get_list()) + 1)
                else:
                    print "This RGB color is already included by a cube: %d, %d, %d" % (
                        current_color[0], current_color[1], current_color[2])
Пример #4
0
    def handle_events(self):
        for event in pygame.event.get():
            #Handle keyboard input:
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_q:
                    print "Quit program..."
                    self.__run = False
                if event.key == pygame.K_f:
                    print "Freezing image..."
                    self.__freeze = not self.__freeze
                if event.key == pygame.K_s:
                    print "Saving current selected color list..."
                    self.__colorlist.save()
                if event.key == pygame.K_k:
                    print "Saving all color lists..."
                    for colorlist in self.__colorlist_list:
                        colorlist.save()
                if event.key == pygame.K_l:
                    print "(Re)loading current selected color list..."
                    self.__colorlist.load()
                if event.key == pygame.K_d:
                    print "Deleting color list..."
                    self.__colorlist.empty_list()
                if event.key == pygame.K_a:
                    self.__colorlist.set_cube_size(self.__colorlist.get_cube_size() + 1)
                    print "Increased cube size to: %d" % self.__colorlist.get_cube_size()
                if event.key == pygame.K_z:
                    self.__colorlist.set_cube_size(self.__colorlist.get_cube_size() - 1)
                    print "Decreased cube size to: %d" % self.__colorlist.get_cube_size()
                if event.key == pygame.K_u:
                    self.__colorlist.remove_last()
                    print "Removed previous color, color list now holds %d colors." % len(self.__colorlist.get_list())
                if event.key == pygame.K_p:
                    self.select_next_colorlist()
                if event.key == pygame.K_o:
                    self.select_previous_colorlist()
                if event.key == pygame.K_r:
                    if self.__show_all:
                        print "Displaying all colors..."
                        self.__show_all = False
                    else:
                        print "NOT Displaying all colors..."
                        self.__show_all = True
                if event.key == pygame.K_t:
                    if self.__show_detect:
                        print "Toggling show detecting mode OFF"
                        self.__show_detect = False
                    else:
                        print "Toggling show detecting mode ON"
                        self.__show_detect = True


            #Handle mouse input:                        
            if event.type == pygame.MOUSEBUTTONUP:
                (m_x, m_y) = pygame.mouse.get_pos()
                if m_x < 0 or m_x > self.__source_surface.get_width() \
                        or m_y < 0 or m_y > self.__source_surface.get_height():
                    return

                current_color = self.__source_surface.get_at((m_x, m_y))

                if self.__colorlist.add_color(current_color):
                    print "Adding RGB color to list: (%d, %d, %d), now holds %d item(s)." \
                            % (current_color[0], current_color[1], current_color[2], len(self.__colorlist.get_list()) + 1)
                else:
                    print "This RGB color is already included by a cube: %d, %d, %d" % (current_color[0], current_color[1], current_color[2])