def __init__(self): self._config_reader = ConfigReader() self.__enable_log = self._config_reader.get_log_enable_value() self.__set_log_path() self._robot_manager = RobotManager() self._info_manager = InfoManager() self._info_manager.tiny_update() self._robot_context = self._info_manager.get_info("ROBOT CONTEXT")
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
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # Developer: Alan Pipitone (Violet Atom) - http://www.violetatom.com/ # Supporter: Wuerth Phoenix - http://www.wuerth-phoenix.com/ # Official website: http://www.alyvix.com/ from alyvix.tools.configreader import ConfigReader from alyvix.tools.screen import ScreenManager from alyvix.bridge.robot import RobotManager import time config_reader = ConfigReader() robot_manager = RobotManager() screen_manager = ScreenManager() _log_folder = None _dict = {} class InfoManager(): def update(self): """ updates all info. """ self.set_info('START TIME', int(time.time()))
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