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
def find_image(self, to_find, search_id, acceptable_score=None): ''' Searches if the given image(s) can be found, returns a SearchResult object. The image is expected to be an ImageHolder or an array of them ''' LOGGER.debug("Searching for %s", search_id) if type(to_find) != types.ListType: to_find = [to_find] search_in = image.ImageHolder(self._get_source_image()) old_acceptable_score = self._acceptable_score if not acceptable_score is None: self._acceptable_score = acceptable_score #test if image is in the last coordinates where it was last spotted if _CHECK_LAST_POSITION: ret = self._is_in_last_position_it_was(to_find, search_id, search_in) if ret.result: LOGGER.debug("Found %s in it's last position" % search_id) self._acceptable_score = old_acceptable_score return ret #try search by hints, still faster than full search ret, false_positives = self._find_image_by_hint(to_find, search_id, search_in) if ret.result: self._results_cache[search_id] = (ret.index, ret.x_pos, ret.y_pos) self._acceptable_score = old_acceptable_score return ret if not search_id in self._last_full_search: self._last_full_search[search_id] = datetime.now() #do full search no more ofthen than once every 10 seconds if hinted #search can be used if (self._can_do_hinted_search(to_find) == True and (datetime.now() - self._last_full_search[search_id]).seconds < 10): self._acceptable_score = old_acceptable_score return ret best_score, best_index = 0, 0 best_x, best_y = 0, 0 for index in range(len(to_find)): to_search = to_find[index] self._search_counter += 1 score, x_pos, y_pos = image.find_image(to_search, search_in) if self._is_score_acceptable(score, to_search.pixel_count): self._results_cache[search_id] = (index, x_pos, y_pos) LOGGER.debug("Found %s at %s %s" % (search_id, x_pos, y_pos)) self._last_full_search[search_id] = datetime.now() self._acceptable_score = old_acceptable_score return SearchResult(True, score, (x_pos, y_pos), index) if score > best_score: best_score = score best_index = index best_x = x_pos best_y = y_pos #Saves each search for debugging purposes if _SAVE_SEARCHES: guess = search_in.crop((best_x, best_y, best_x + to_find[best_index].size[0], best_y + to_find[best_index].size[1])) guess.save("test %i best candidate %s.bmp" % (self._search_counter, best_score), "BMP") to_search.image.save(("test %i to search.bmp" % self._search_counter), "BMP") search_in.image.save("test %i screen.bmp" % self._search_counter, "BMP") LOGGER.debug("Can't find %s" % search_id) self._last_full_search[search_id] = datetime.now() self._acceptable_score = old_acceptable_score return SearchResult(False, best_score, (best_x, best_y), best_index)