Example #1
0
    def _is_in_last_position_it_was(self, images, search_id, search_in):
        '''
        Checks if the given images are visible in the same position they were
        spotted last time
        '''
        started_at = datetime.now()
        if search_id in self._results_cache:
            index, x_pos, y_pos = self._results_cache[search_id]
            to_search = images[index]
            max_x = search_in.size[0] - to_search.size[0]
            max_y = search_in.size[1] - to_search.size[1]
            if x_pos >= max_x or y_pos >= max_y:
                self._results_cache.pop(search_id)
                LOGGER.debug("Polluted cache for search id %s with %s %s" % (search_id, x_pos, y_pos))
                return SearchResult(False, 0, (0, 0), 0)
            
            score, x_pos, y_pos = image.test_image(to_search,
                                                   search_in,
                                                   x_pos,
                                                   y_pos)
            took = ((datetime.now() - started_at).microseconds / 1000000.0)
            if self._is_score_acceptable(score, to_search.pixel_count):
                LOGGER.debug("Cache hit for %s took %s" % (search_id, took))
                return SearchResult(True, score, (x_pos, y_pos), index)
            else:
                LOGGER.debug("Cache miss for %s at %s %s %s used %s" % (search_id, x_pos, y_pos, score, took))
                if _SAVE_SEARCHES:
                    search_in.image.save("cache miss %i for %s.bmp" % (self._search_counter, search_id),
                               "BMP")
                    score_area = search_in.crop((x_pos,
                                                 y_pos, 
                                                 x_pos + to_search.size[0],
                                                 y_pos + to_search.size[1]))
                    score_area.save("cache miss area %i for %s.bmp" % (self._search_counter, search_id),
                               "BMP")
                    to_search.image.save("cache miss image %i for %s.bmp" % (self._search_counter, search_id),
                               "BMP")
                return SearchResult(False, score, (x_pos, y_pos), index)

        return SearchResult(False, 0, (0, 0), 0)
Example #2
0
 def _find_image_by_hint(self, to_find, search_id, search_in):
     got_false_positives = False
     if self._can_do_hinted_search(to_find) == False:
         return SearchResult(False, 0, (0, 0), 0), got_false_positives
         
     for index in range(len(to_find)):
         to_search = to_find[index]
         cropped = image.ImageHolder(to_search.crop((0, 0, 32, 32)))
         score, x_pos, y_pos = image.find_image(cropped, search_in)
         if self._is_score_acceptable(score, cropped.pixel_count):
             LOGGER.debug("HINT Found %s at %s %s" % (search_id, x_pos, y_pos))
             score, x_pos, y_pos = image.test_image(to_search,
                                                    search_in,
                                                    x_pos,
                                                    y_pos)
             if self._is_score_acceptable(score, to_search.pixel_count):
                 LOGGER.debug("Found by hint %s at %s %s" % (search_id, x_pos, y_pos))
                 return SearchResult(True, score, (x_pos, y_pos), index), got_false_positives
             else:
                 LOGGER.debug("HINT false %s at %s %s" % (search_id, x_pos, y_pos))
                 got_false_positives = True
     
     return SearchResult(False, 0, [0, 0], 0), got_false_positives