def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._main_component = None self.__sub_components = [] self.__threshold = 0.8 self._objects_finders_caller = [] self._name_with_caller = None self._xy = None self._offset_x = 0 self._offset_y = 0 self._log_manager = None self.__enable_debug_calcperf = False if self.__enable_debug_calcperf is True: self._log_manager = LogManager() self._name = "check_presence" if name is not None: self._name = name if self.__enable_debug_calcperf is True: self._log_manager.set_object_name(self._name)
def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._source_image_color = None self._source_image_gray = None self._objects_found = [] self._log_manager = None #variables for the perfdata self._cacheManager = None self._min_different_contours = 15 self._flag_thread_started = False self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = False self._screen_capture = None #end perfdata section self._time_checked_before_exit_start = 0 self._objects_finders_caller = [] self._name_with_caller = None self._name = name self._log_manager = LogManager() self._log_manager.set_object_name(self._name) self._screen_capture = ScreenManager() self._cacheManager = CacheManager() self._configReader = ConfigReader()
def __init__(self): self.left_button = 1 self.right_button = 2 self.middle_button = 3 self.wheel_up = 4 self.wheel_down = 5 self._info_manager = InfoManager() self._log_manager = LogManager() self._scaling_factor = self._info_manager.get_info("SCALING FACTOR FLOAT")
def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._main_component = None self._sub_components = [] self._timedout_main_components = [] self._timedout_sub_components = [] self._main_extra_img_log = None self._sub_extra_imgages_log = [] self._rect_extra_timedout_image = None self._robot_manager = RobotManager() self._rf_is_set = self._robot_manager.context_is_set() self._source_image_color = None self._source_image_gray = None self._objects_found = [] self._log_manager = None self._timed_out_images = [] self._find_thread_images = [] self._find_thread_images_disappear = [] #self._find_thread_images_copy = [] self._last_thread_image = None self._last_thread_image_copy = None self._heartbeat_images = [] self._heartbeat_images_copy = [] #variables for the perfdata self._cacheManager = None self._min_different_contours = 15 self._flag_thread_started = False self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = False self._screen_capture = None #end perfdata section self._info_manager = InfoManager() self._scaling_factor = self._info_manager.get_info("SCALING FACTOR INT") self._time_checked_before_exit_start = 0 self._objects_finders_caller = [] self._name_with_caller = None self._name = name self._log_manager = LogManager() self._log_manager.set_object_name(self._name) self._screen_capture = ScreenManager() self._cacheManager = CacheManager() self._configReader = ConfigReader() self.__enable_debug_calcperf = False #self._timer_for_disappear = 0 self._object_is_found_flag = False self._is_object_finder = False self._objects_found_of_sub_object_finder = []
class BaseFinder(object): def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._main_component = None self._sub_components = [] self._timedout_main_components = [] self._timedout_sub_components = [] self._main_extra_img_log = None self._sub_extra_imgages_log = [] self._rect_extra_timedout_image = None self._robot_manager = RobotManager() self._rf_is_set = self._robot_manager.context_is_set() self._source_image_color = None self._source_image_gray = None self._objects_found = [] self._log_manager = None self._timed_out_images = [] self._find_thread_images = [] self._find_thread_images_disappear = [] #self._find_thread_images_copy = [] self._last_thread_image = None self._last_thread_image_copy = None self._heartbeat_images = [] self._heartbeat_images_copy = [] #variables for the perfdata self._cacheManager = None self._min_different_contours = 15 self._flag_thread_started = False self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = False self._screen_capture = None #end perfdata section self._info_manager = InfoManager() self._scaling_factor = self._info_manager.get_info("SCALING FACTOR INT") self._time_checked_before_exit_start = 0 self._objects_finders_caller = [] self._name_with_caller = None self._name = name self._log_manager = LogManager() self._log_manager.set_object_name(self._name) self._screen_capture = ScreenManager() self._cacheManager = CacheManager() self._configReader = ConfigReader() self.__enable_debug_calcperf = False #self._timer_for_disappear = 0 self._object_is_found_flag = False self._is_object_finder = False self._objects_found_of_sub_object_finder = [] def _compress_image(self, img): return cv2.imencode('.png', img)[1] def _uncompress_image(self, compressed_img): return cv2.imdecode(compressed_img, cv2.CV_LOAD_IMAGE_GRAYSCALE) def set_name(self, name): """ set the name of the object. :type name: string :param name: the name of the object """ self._name = name self._log_manager.set_object_name(self._name) def get_name(self): """ get the name of the object. :rtype: string :return: the name of the object """ return self._name def set_name_with_caller(self): tmp_name = self._name for object_caller in self._objects_finders_caller: tmp_name = object_caller + os.sep + tmp_name self._name_with_caller = tmp_name self._log_manager.set_object_name(self._name_with_caller) def set_source_image_color(self, image_data): """ set the color image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the color image """ self._source_image_color = image_data.copy() img_gray = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY) self.set_source_image_gray(img_gray) #self._log_manager.set_image(self._source_image) def set_source_image_gray(self, image_data): """ set the gray image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the gray image """ self._source_image_gray = image_data.copy() def get_source_image_color(self): """ get the color image on which the find method will search the object. :rtype: numpy.ndarray :return: the source color image """ return self._source_image_color def get_source_image_gray(self): """ get the gray image on which the find method will search the object. :rtype: numpy.ndarray :return: the source gray image """ return self._source_image_gray def find(self): raise NotImplementedError def wait(self, timeout=-1, wait_disappear=False): """ wait until the object appears on the screen. if timeout value is -1 (default value) then timeout value will be read from config file. if configuration file doesn't exist, then timeout value will be 15 sec. :param timeout: timeout in seconds :type timeout: int """ #cv2.imwrite() #self._robot_manager.write_log_message("wait method: " + self.get_name(), "ERROR", False) #self._robot_manager.write_log_message("wait method: " + self.get_name(), "ERROR", False) #sss = self._robot_manager.get_suite_name() #ttt = self._robot_manager.get_testcase_name() #self._robot_manager.method1().method2() timeout_value = 15 if timeout == -1: timeout_value = self._configReader.get_finder_wait_timeout() else: timeout_value = timeout self._objects_found = [] self._heartbeat_images = [] self._find_thread_images = [] self._flag_thread_started = False self._flag_thread_have_to_exit = False self._heartbeat_images_copy = [] time_elapsed = 0.0 #time_of_last_change = 0.0 self._time_checked_before_exit_start = None self._object_is_found_flag = False #screenCapture = ScreenManager() thread_interval = self._info_manager.get_info("FINDER THREAD INTERVAL") #self._configReader.get_finder_thread_interval() #thread_interval = 0.5 check_diff_interval = self._info_manager.get_info("CHECK DIFF INTERVAL") img1 = self._cacheManager.GetLastObjFoundFullImg() if img1 is None: img1 = self._screen_capture.grab_desktop(self._screen_capture.get_gray_mat) thread_t0 = time.time() time_before_loop = time.time() while True: #txx = time.time() try: if len(self._objects_found) > 0 and self._flag_thread_started is False: #do analysis cjecl_time() """ print "len main:", len(self._objects_found) print "main x, y, w, h:", self._objects_found[0][0].x, self._objects_found[0][0].y, self._objects_found[0][0].width, self._objects_found[0][0].height if self._objects_found[0][1] is not None: print "len secodn:", len(self._objects_found[0][1]) for sub_obj in self._objects_found[0][1]: print "sub x, y, w, h:", sub_obj.x, sub_obj.y, sub_obj.width, sub_obj.height """ self._last_thread_image = self._uncompress_image(self._find_thread_images[-1][1]) #time.sleep(3600) if wait_disappear is False: self._log_manager.save_objects_found(self._name, self.get_source_image_gray(), self._objects_found, [x[1] for x in self._sub_components]) if wait_disappear is True: self._heartbeat_images_copy = copy.deepcopy(self._heartbeat_images) self._last_thread_image_copy = copy.deepcopy(self._last_thread_image) #self._timer_for_disappear = self._heartbeat_images[-1][0] #self._find_thread_images_copy = copy.deepcopy(self._find_thread_images) return -2 else: self._object_is_found_flag = True self._last_thread_image_copy = copy.deepcopy(self._last_thread_image) return self._get_performance() if time_elapsed > timeout_value and self._flag_thread_started is False: self._last_thread_image = self._uncompress_image(self._find_thread_images[-1][1]) #from alyvix.finders.cv.rectfinder import RectFinder #from alyvix.finders.cv.imagefinder import ImageFinder #from alyvix.finders.cv.textfinder import TextFinder from alyvix.finders.cv.objectfinder import ObjectFinder #if not isinstance(self, ObjectFinder): self._log_manager.save_timedout_objects(self._name + "_timedout", self.get_source_image_gray(), self._timedout_main_components, self._timedout_sub_components, self._main_extra_img_log, self._sub_extra_imgages_log) #else: if isinstance(self, ObjectFinder): #self._log_manager.save_timedout_objects(self._name + "_timedout", self._last_thread_image, self._main_component[0]._timedout_main_components, self._main_component[0]._timedout_sub_components, self._main_component[0]._main_extra_img_log, self._main_component[0]._sub_extra_imgages_log, True, self._main_component[0]._name) if len(self._main_component[0]._objects_found) == 0: self._log_manager.save_timedout_objects(self._name + "_timedout", self._last_thread_image, self._main_component[0]._timedout_main_components, self._main_component[0]._timedout_sub_components, self._main_component[0]._main_extra_img_log, self._main_component[0]._sub_extra_imgages_log, True, self._main_component[0]._name) for t_sub in self._sub_components: self._log_manager.save_timedout_objects(self._name + "_timedout", self._last_thread_image, t_sub[0]._timedout_main_components, t_sub[0]._timedout_sub_components, t_sub[0]._main_extra_img_log, t_sub[0]._sub_extra_imgages_log, True, t_sub[0]._name) return -1 t0 = time.time() #cv2.imwrite('img2.png', img2) #if time.time() - thread_t0 >= thread_interval: if time_elapsed < timeout_value and time.time() - thread_t0 >= thread_interval and self._flag_thread_started is False: thread_t0 = time.time() self._flag_thread_started = True """ folder = 'c:\\log\\buffer_images' for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except Exception, e: print e """ #for i in range(len(self._find_thread_images)): #cv2.imwrite("c:\\log\\buffer_images\\_old_" + str(self._find_thread_images[i][0]) + ".png", self._uncompress_image(self._find_thread_images[i][1])) self._find_thread_images = copy.deepcopy(self._heartbeat_images) self._heartbeat_images = [] self.set_source_image_color(img2_color) self.set_source_image_gray(img2_gray) if self._log_manager.is_log_enable() is True: self._log_manager.delete_all_items(keep_items=20, exclude_item="difference") worker = Thread(target=self.find) worker.setDaemon(True) worker.start() img2_color = self._screen_capture.grab_desktop(self._screen_capture.get_color_mat) img2_gray = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY) self._heartbeat_images.append((time_elapsed, self._compress_image(img2_gray))) t1 = time.time() - t0 time_sleep = check_diff_interval - t1 if time_sleep < 0: time_sleep = 0 time.sleep(time_sleep) time_elapsed = time.time() - time_before_loop #print time_elapsed except Exception, err: #print str(err) + " on line " + str(sys.exc_traceback.tb_lineno) self._log_manager.save_exception("ERROR", "an exception has occurred: " + str(err) + " on line " + str(sys.exc_traceback.tb_lineno)) return None
def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._main_component = None self._sub_components = [] self._timedout_main_components = [] self._timedout_sub_components = [] self._main_extra_img_log = None self._sub_extra_imgages_log = [] self._rect_extra_timedout_image = None self._robot_manager = RobotManager() self._rf_is_set = self._robot_manager.context_is_set() self._source_image_color = None self._source_image_gray = None self._objects_found = [] self._log_manager = None self._timed_out_images = [] self._find_thread_images = [] self._find_thread_images_disappear = [] #self._find_thread_images_copy = [] self._last_thread_image = None self._last_thread_image_copy = None self._heartbeat_images = [] self._heartbeat_images_copy = [] #variables for the perfdata self._cacheManager = None self._min_different_contours = 15 self._flag_thread_started = False self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = False self._screen_capture = None #end perfdata section self._info_manager = InfoManager() self._scaling_factor = self._info_manager.get_info( "SCALING FACTOR INT") self._time_checked_before_exit_start = 0 self._objects_finders_caller = [] self._name_with_caller = None self._name = name self._log_manager = LogManager() self._log_manager.set_object_name(self._name) self._screen_capture = ScreenManager() self._cacheManager = CacheManager() self._configReader = ConfigReader() self.__enable_debug_calcperf = False #self._timer_for_disappear = 0 self._object_is_found_flag = False self._is_object_finder = False self._objects_found_of_sub_object_finder = []
class BaseFinder(object): def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._main_component = None self._sub_components = [] self._timedout_main_components = [] self._timedout_sub_components = [] self._main_extra_img_log = None self._sub_extra_imgages_log = [] self._rect_extra_timedout_image = None self._robot_manager = RobotManager() self._rf_is_set = self._robot_manager.context_is_set() self._source_image_color = None self._source_image_gray = None self._objects_found = [] self._log_manager = None self._timed_out_images = [] self._find_thread_images = [] self._find_thread_images_disappear = [] #self._find_thread_images_copy = [] self._last_thread_image = None self._last_thread_image_copy = None self._heartbeat_images = [] self._heartbeat_images_copy = [] #variables for the perfdata self._cacheManager = None self._min_different_contours = 15 self._flag_thread_started = False self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = False self._screen_capture = None #end perfdata section self._info_manager = InfoManager() self._scaling_factor = self._info_manager.get_info( "SCALING FACTOR INT") self._time_checked_before_exit_start = 0 self._objects_finders_caller = [] self._name_with_caller = None self._name = name self._log_manager = LogManager() self._log_manager.set_object_name(self._name) self._screen_capture = ScreenManager() self._cacheManager = CacheManager() self._configReader = ConfigReader() self.__enable_debug_calcperf = False #self._timer_for_disappear = 0 self._object_is_found_flag = False self._is_object_finder = False self._objects_found_of_sub_object_finder = [] def _compress_image(self, img): return cv2.imencode('.png', img)[1] def _uncompress_image(self, compressed_img): return cv2.imdecode(compressed_img, cv2.CV_LOAD_IMAGE_GRAYSCALE) def set_name(self, name): """ set the name of the object. :type name: string :param name: the name of the object """ self._name = name self._log_manager.set_object_name(self._name) def get_name(self): """ get the name of the object. :rtype: string :return: the name of the object """ return self._name def set_name_with_caller(self): tmp_name = self._name for object_caller in self._objects_finders_caller: tmp_name = object_caller + os.sep + tmp_name self._name_with_caller = tmp_name self._log_manager.set_object_name(self._name_with_caller) def set_source_image_color(self, image_data): """ set the color image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the color image """ self._source_image_color = image_data.copy() img_gray = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY) self.set_source_image_gray(img_gray) #self._log_manager.set_image(self._source_image) def set_source_image_gray(self, image_data): """ set the gray image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the gray image """ self._source_image_gray = image_data.copy() def get_source_image_color(self): """ get the color image on which the find method will search the object. :rtype: numpy.ndarray :return: the source color image """ return self._source_image_color def get_source_image_gray(self): """ get the gray image on which the find method will search the object. :rtype: numpy.ndarray :return: the source gray image """ return self._source_image_gray def find(self): raise NotImplementedError def wait(self, timeout=-1, wait_disappear=False): """ wait until the object appears on the screen. if timeout value is -1 (default value) then timeout value will be read from config file. if configuration file doesn't exist, then timeout value will be 15 sec. :param timeout: timeout in seconds :type timeout: int """ #cv2.imwrite() #self._robot_manager.write_log_message("wait method: " + self.get_name(), "ERROR", False) #self._robot_manager.write_log_message("wait method: " + self.get_name(), "ERROR", False) #sss = self._robot_manager.get_suite_name() #ttt = self._robot_manager.get_testcase_name() #self._robot_manager.method1().method2() timeout_value = 15 if timeout == -1: timeout_value = self._configReader.get_finder_wait_timeout() else: timeout_value = timeout self._objects_found = [] self._heartbeat_images = [] self._find_thread_images = [] self._flag_thread_started = False self._flag_thread_have_to_exit = False self._heartbeat_images_copy = [] time_elapsed = 0.0 #time_of_last_change = 0.0 self._time_checked_before_exit_start = None self._object_is_found_flag = False #screenCapture = ScreenManager() thread_interval = self._info_manager.get_info( "FINDER THREAD INTERVAL" ) #self._configReader.get_finder_thread_interval() #thread_interval = 0.5 check_diff_interval = self._info_manager.get_info( "CHECK DIFF INTERVAL") img1 = self._cacheManager.GetLastObjFoundFullImg() if img1 is None: img1 = self._screen_capture.grab_desktop( self._screen_capture.get_gray_mat) thread_t0 = time.time() time_before_loop = time.time() while True: #txx = time.time() try: if len(self._objects_found ) > 0 and self._flag_thread_started is False: #do analysis cjecl_time() """ print "len main:", len(self._objects_found) print "main x, y, w, h:", self._objects_found[0][0].x, self._objects_found[0][0].y, self._objects_found[0][0].width, self._objects_found[0][0].height if self._objects_found[0][1] is not None: print "len secodn:", len(self._objects_found[0][1]) for sub_obj in self._objects_found[0][1]: print "sub x, y, w, h:", sub_obj.x, sub_obj.y, sub_obj.width, sub_obj.height """ self._last_thread_image = self._uncompress_image( self._find_thread_images[-1][1]) #time.sleep(3600) if wait_disappear is False: self._log_manager.save_objects_found( self._name, self.get_source_image_gray(), self._objects_found, [x[1] for x in self._sub_components]) if wait_disappear is True: self._heartbeat_images_copy = copy.deepcopy( self._heartbeat_images) self._last_thread_image_copy = copy.deepcopy( self._last_thread_image) #self._timer_for_disappear = self._heartbeat_images[-1][0] #self._find_thread_images_copy = copy.deepcopy(self._find_thread_images) return -2 else: self._object_is_found_flag = True self._last_thread_image_copy = copy.deepcopy( self._last_thread_image) return self._get_performance() if time_elapsed > timeout_value and self._flag_thread_started is False: self._last_thread_image = self._uncompress_image( self._find_thread_images[-1][1]) #from alyvix.finders.cv.rectfinder import RectFinder #from alyvix.finders.cv.imagefinder import ImageFinder #from alyvix.finders.cv.textfinder import TextFinder from alyvix.finders.cv.objectfinder import ObjectFinder #if not isinstance(self, ObjectFinder): self._log_manager.save_timedout_objects( self._name + "_timedout", self.get_source_image_gray(), self._timedout_main_components, self._timedout_sub_components, self._main_extra_img_log, self._sub_extra_imgages_log) #else: if isinstance(self, ObjectFinder): #self._log_manager.save_timedout_objects(self._name + "_timedout", self._last_thread_image, self._main_component[0]._timedout_main_components, self._main_component[0]._timedout_sub_components, self._main_component[0]._main_extra_img_log, self._main_component[0]._sub_extra_imgages_log, True, self._main_component[0]._name) if len(self._main_component[0]._objects_found) == 0: self._log_manager.save_timedout_objects( self._name + "_timedout", self._last_thread_image, self. _main_component[0]._timedout_main_components, self._main_component[0]. _timedout_sub_components, self._main_component[0]._main_extra_img_log, self._main_component[0]._sub_extra_imgages_log, True, self._main_component[0]._name) for t_sub in self._sub_components: self._log_manager.save_timedout_objects( self._name + "_timedout", self._last_thread_image, t_sub[0]._timedout_main_components, t_sub[0]._timedout_sub_components, t_sub[0]._main_extra_img_log, t_sub[0]._sub_extra_imgages_log, True, t_sub[0]._name) return -1 t0 = time.time() #cv2.imwrite('img2.png', img2) #if time.time() - thread_t0 >= thread_interval: if time_elapsed < timeout_value and time.time( ) - thread_t0 >= thread_interval and self._flag_thread_started is False: thread_t0 = time.time() self._flag_thread_started = True """ folder = 'c:\\log\\buffer_images' for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except Exception, e: print e """ #for i in range(len(self._find_thread_images)): #cv2.imwrite("c:\\log\\buffer_images\\_old_" + str(self._find_thread_images[i][0]) + ".png", self._uncompress_image(self._find_thread_images[i][1])) self._find_thread_images = copy.deepcopy( self._heartbeat_images) self._heartbeat_images = [] self.set_source_image_color(img2_color) self.set_source_image_gray(img2_gray) if self._log_manager.is_log_enable() is True: self._log_manager.delete_all_items( keep_items=20, exclude_item="difference") worker = Thread(target=self.find) worker.setDaemon(True) worker.start() img2_color = self._screen_capture.grab_desktop( self._screen_capture.get_color_mat) img2_gray = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY) self._heartbeat_images.append( (time_elapsed, self._compress_image(img2_gray))) t1 = time.time() - t0 time_sleep = check_diff_interval - t1 if time_sleep < 0: time_sleep = 0 time.sleep(time_sleep) time_elapsed = time.time() - time_before_loop #print time_elapsed except Exception, err: #print str(err) + " on line " + str(sys.exc_traceback.tb_lineno) self._log_manager.save_exception( "ERROR", "an exception has occurred: " + str(err) + " on line " + str(sys.exc_traceback.tb_lineno)) return None
class BaseFinder(object): def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._source_image_color = None self._source_image_gray = None self._objects_found = [] self._log_manager = None #variables for the perfdata self._cacheManager = None self._min_different_contours = 15 self._flag_thread_started = False self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = False self._screen_capture = None #end perfdata section self._time_checked_before_exit_start = 0 self._objects_finders_caller = [] self._name_with_caller = None self._name = name self._log_manager = LogManager() self._log_manager.set_object_name(self._name) self._screen_capture = ScreenManager() self._cacheManager = CacheManager() self._configReader = ConfigReader() def set_name(self, name): """ set the name of the object. :type name: string :param name: the name of the object """ self._name = name self._log_manager.set_object_name(self._name) def get_name(self): """ get the name of the object. :rtype: string :return: the name of the object """ return self._name def set_name_with_caller(self): tmp_name = self._name for object_caller in self._objects_finders_caller: tmp_name = object_caller + os.sep + tmp_name self._name_with_caller = tmp_name self._log_manager.set_object_name(self._name_with_caller) def set_source_image_color(self, image_data): """ set the color image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the color image """ self._source_image_color = image_data.copy() img_gray = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY) self.set_source_image_gray(img_gray) #self._log_manager.set_image(self._source_image) def set_source_image_gray(self, image_data): """ set the gray image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the gray image """ self._source_image_gray = image_data.copy() def find(self): raise NotImplementedError def wait(self, timeout=-1): """ wait until the object appears on the screen. if timeout value is -1 (default value) then timeout value will be read from config file. if configuration file doesn't exist, then timeout value will be 15 sec. :param timeout: timeout in seconds :type timeout: int """ timeout_value = 15 if timeout == -1: timeout_value = self._configReader.get_finder_wait_timeout() else: timeout_value = timeout self._objects_found = [] self._flag_thread_started = False self._flag_thread_have_to_exit = False time_elapsed = 0 time_of_last_change = 0 self._time_checked_before_exit_start = None #screenCapture = ScreenManager() thread_interval = self._configReader.get_finder_thread_interval() check_diff_interval = self._configReader.get_finder_diff_interval() img1 = self._cacheManager.GetLastObjFoundFullImg() if img1 is None: img1 = self._screen_capture.grab_desktop(self._screen_capture.get_gray_mat) thread_t0 = time.time() time_before_loop = time.time() while True: try: if time_elapsed > timeout_value: return -1 t0 = time.time() img2_color = self._screen_capture.grab_desktop(self._screen_capture.get_color_mat) img2_gray = cv2.cvtColor(img2_color, cv2.COLOR_BGR2GRAY) #cv2.imwrite('img2.png', img2) if time.time() - thread_t0 >= thread_interval: thread_t0 = time.time() if self._flag_thread_started is False: self._flag_thread_started = True self.set_source_image_color(img2_color) self.set_source_image_gray(img2_gray) if self._log_manager.is_log_enable() is True: self._log_manager.delete_all_items(keep_items=20, exclude_item="difference") worker = Thread(target=self.find) worker.setDaemon(True) worker.start() if len(self._objects_found) > 0: if self._time_checked_before_exit_start is not None: return self._time_checked_before_exit_start else: return time_of_last_change diff_mask = numpy.bitwise_xor(img1, img2_gray) #cv2.imwrite("bit.png", diff_mask) # find the contours contours, hierarchy = cv2.findContours(diff_mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: x,y,w,h = cv2.boundingRect(cnt) #cv2.rectangle(img2,(x,y),(x+w,y+h),(0,0,255),3) #is_images_equal = not(diff_mask.any()) if len(contours) < self._min_different_contours: #if True: is_images_equal = True else: is_images_equal = False if is_images_equal is False: if self._log_manager.is_log_enable() is True: self._log_manager.save_image("difference", "old.png", img1) img1 = img2_gray.copy() if self._log_manager.is_log_enable() is True: self._log_manager.save_image("difference", "current.png", img2_gray) self._log_manager.save_image("difference", "mask.png", diff_mask) self._log_manager.delete_all_items(sub_dir="difference", keep_items=20) if self._flag_check_before_exit is False: self._flag_check_before_exit = True self._time_checked_before_exit_start = time_of_last_change elif self._flag_checked_before_exit is True and self._flag_check_before_exit is True: self._flag_check_before_exit = False self._flag_checked_before_exit = False self._flag_thread_have_to_exit = True self._time_checked_before_exit_start = None time_of_last_change = time_elapsed #if len(self._objects_found) > 0: # return time_of_last_change t1 = time.time() - t0 time_sleep = check_diff_interval - t1 if time_sleep < 0: time_sleep = 0 time.sleep(time_sleep) time_elapsed = time.time() - time_before_loop except Exception, err: #print str(err) + " on line " + str(sys.exc_traceback.tb_lineno) self._log_manager.save_exception("ERROR", "an exception has occurred: " + str(err) + " on line " + str(sys.exc_traceback.tb_lineno)) return None
class CheckPresence: def __init__(self, name=None): """ init the class :type name: string :param name: the object name """ self._main_component = None self.__sub_components = [] self.__threshold = 0.8 self._objects_finders_caller = [] self._name_with_caller = None self._xy = None self._offset_x = 0 self._offset_y = 0 self._log_manager = None self.__enable_debug_calcperf = False if self.__enable_debug_calcperf is True: self._log_manager = LogManager() self._name = "check_presence" if name is not None: self._name = name if self.__enable_debug_calcperf is True: self._log_manager.set_object_name(self._name) def set_name(self, name): """ set the name of the object. :type name: string :param name: the name of the object """ self._name = name if self.__enable_debug_calcperf is True: self._log_manager.set_object_name(self._name) def get_name(self): """ get the name of the object. :rtype: string :return: the name of the object """ return self._name def set_xy(self, x, y): self._xy = (x, y) def set_source_image_color(self, image_data): """ set the color image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the color image """ self._source_image_color = image_data.copy() #img_gray = cv2.cvtColor(image_data, cv2.COLOR_BGR2GRAY) #self.set_source_image_gray(img_gray) #self._log_manager.set_image(self._source_image) def set_source_image_gray(self, image_data, crop_properties = None): """ set the gray image on which the find method will search the object. :type image_data: numpy.ndarray :param image_data: the gray image """ if crop_properties is None: self._source_image_gray = image_data.copy() else: x1 = crop_properties[0] y1 = crop_properties[1] x2 = crop_properties[2] y2 = crop_properties[3] self._offset_x = crop_properties[0] self._offset_y = crop_properties[1] self._source_image_gray = image_data[y1:y2, x1:x2].copy() def set_main_component(self, image, roi_dict=None): """ Set the template image that the find method has to find into the source Image. :type main_template: numpy.ndarray :param main_template: image of the template """ if roi_dict is not None: roi = Roi(roi_dict) else: roi = None self._main_component = (image, roi) #print "main component:",self._main_component def add_sub_component(self, image, roi_dict): """ Add a template that the find method has to find if it has also find the main template. A roi (region of interest) will be cropped from the source image according to the parameters roi_x, roi_y, roi_width and roi_height. roi_x and roi_y are relative to the main template x,y coordinates. The find method will search the sub template only inside the roi area. :type template_image: numpy.ndarray :param template_image: image of the template :type roi_x: int :param roi_x: x-coordinate of roi :type roi_y: int :param roi_y: y-coordinate of roi :type roi_width: int :param roi_width: roi width :type roi_height: int :param roi_height: roi height """ roi = Roi(roi_dict) self.__sub_components.append((image, roi)) def _SetThreshold(self, threshold): """ Set the threshold of template matching algorithm. Min value is 0.0, max value is 1.0 threshold=1.0 means that the source image must contain an exact copy of the template image. threshold=0.0 means that the source image can even not contain the template (with the risk of false positives). The default value for the threshold is 0.7 :type threshold: float :param threshold: threshold value for template matching algorithm """ self.__threshold = threshold def find(self): """ find the main template and sub templates into the source image. :rtype: bool :return: return true if object is present into the image """ try: #print "main comp:",self._main_component source_img_auto_set = False self._objects_found = [] if self.__enable_debug_calcperf is True: self.__find_log_folder = datetime.datetime.now().strftime("%H_%M_%S") + "_" + "searching" offset_x = 0 offset_y = 0 main_template = self._main_component[0] #print "main templ:", main_template roi = self._main_component[1] if roi is not None: y1 = roi.y - self._offset_y y2 = y1 + roi.height x1 = roi.x - self._offset_x x2 = x1 + roi.width offset_x = x1 offset_y = y1 if offset_x < 0: offset_x = 0 if offset_y < 0: offset_y = 0 source_img_height, source_img_width = self._source_image_gray.shape if y1 < 0: y1 = 0 elif y1 > source_img_height: y1 = source_img_height if y2 < 0: y2 = 0 elif y2 > source_img_height: y2 = source_img_height if x1 < 0: x1 = 0 elif x1 > source_img_width: x1 = source_img_width if x2 < 0: x2 = 0 elif x2 > source_img_width: x2 = source_img_width #print x1,x2,y1,y2 source_image = self._source_image_gray[y1:y2, x1:x2] else: source_image = self._source_image_gray if self.__enable_debug_calcperf is True: self._log_manager.save_image(self.__find_log_folder, "source_img.png", source_image) self._log_manager.save_image(self.__find_log_folder, "main_template.png", main_template) objects_found = [] analyzed_points = [] self._objects_found = [] w, h = main_template.shape[::-1] src_w, src_h = source_image.shape[::-1] tpl_w = w tpl_h = h if src_h < tpl_h or src_w < tpl_w: self._flag_thread_have_to_exit = False self._flag_thread_started = False self._source_image_gray = None self._source_image_color = None return [] res = cv2.matchTemplate(source_image, main_template, cv2.TM_CCOEFF_NORMED) loc = numpy.where(res >= self.__threshold) cnt = 0 for point in zip(*loc[::-1]): object_found = [] object_found.append([]) object_found.append([]) x = offset_x + point[0] y = offset_y + point[1] tolerance_region = 5 if not ((x >= self._xy[0] - tolerance_region and x <= self._xy[0] + tolerance_region) and \ (y >= self._xy[1] - tolerance_region and y <= self._xy[1] + tolerance_region)): continue #self._log_manager.set_main_object_points((x, y, w, h)) if self.__enable_debug_calcperf is True: img_copy = source_image.copy() cv2.rectangle(img_copy, ((x-offset_x), (y-offset_y)), ((x-offset_x)+w, (y-offset_y)+h), (0, 0, 255), 2) self._log_manager.save_image(self.__find_log_folder, "object_found.png", img_copy) sub_templates_len = len(self.__sub_components) if sub_templates_len == 0: main_object_result = MatchResult((x, y, w, h)) object_found[0] = main_object_result object_found[1] = None return True else: #print sub_templates_len total_sub_template_found = 0 sub_objects_found = [] for sub_template in self.__sub_components: sub_template_coordinates = self._find_sub_template((x, y), sub_template) if sub_template_coordinates is not None: sub_objects_found.append(sub_template_coordinates) total_sub_template_found = total_sub_template_found + 1 if total_sub_template_found == sub_templates_len: #good_points.append((x, y, w, h)) main_object_result = MatchResult((x, y, w, h)) object_found[0] = main_object_result object_found[1] = sub_objects_found #objects_found.append(object_found) return True #self._log_manager.save_object_image("img_" + str(cnt) + ".png") cnt = cnt + 1 #if len(objects_found) > 0: # return True except Exception, err: self._log_manager.save_exception("ERROR", "an exception has occurred: " + str(err) + " on line " + str(sys.exc_traceback.tb_lineno)) return False