Beispiel #1
0
 def _translate_code(self, step):
     if step["tag"] != "function":
         return None
     step_data = step["data"]
     args = []
     code = {
         "name": step_data["name"],
         "args": args,
     }
     for key, value in step_data["call_args"].items():
         args.append({
             "key": key,
             "value": value,
         })
     for k, arg in enumerate(args):
         value = arg["value"]
         if isinstance(value, dict) and value.get("__class__") == "Template":
             if self.export_dir:  # all relative path
                 image_path = value['filename']
                 if not os.path.isfile(os.path.join(self.script_root, image_path)) and value['_filepath']:
                     # copy image used by using statement
                     shutil.copyfile(value['_filepath'], os.path.join(self.script_root, value['filename']))
             else:
                 image_path = os.path.abspath(value['_filepath'] or value['filename'])
             arg["image"] = image_path
             if not value['_filepath'] and not os.path.exists(value['filename']):
                 crop_img = imread(os.path.join(self.script_root, value['filename']))
             else:
                 crop_img = imread(value['_filepath'] or value['filename'])
             arg["resolution"] = get_resolution(crop_img)
     return code
Beispiel #2
0
    def snapshot(self, filename="tmp.png"):
        """
        Take a screenshot and save it to `tmp.png` filename by default

        Args:
            filename: name of file where to store the screenshot

        Returns:
            display the screenshot

        """
        if not filename:
            filename = "tmp.png"
        if self.handle:
            try:
                screenshot(filename, self.handle)
            except win32gui.error:
                self.handle = None
                screenshot(filename)
        else:
            screenshot(filename)

        img = aircv.imread(filename)
        os.remove(filename)

        return img
Beispiel #3
0
 def _translate_code(self, step):
     if step["tag"] != "function":
         return None
     step_data = step["data"]
     args = []
     code = {
         "name": step_data["name"],
         "args": args,
     }
     for key, value in step_data["call_args"].items():
         args.append({
             "key": key,
             "value": value,
         })
     for k, arg in enumerate(args):
         value = arg["value"]
         if isinstance(value,
                       dict) and value.get("__class__") == "Template":
             image_path = str(value['filename'])
             if not self.export_dir:
                 if os.path.isfile(
                         os.path.join(self.script_root, image_path)):
                     image_path = os.path.join(self.script_root, image_path)
                 else:
                     image_path = value['_filepath']
             else:
                 if not os.path.isfile(
                         os.path.join(self.script_root, image_path)):
                     shutil.copy(value['_filepath'], self.script_root)
             arg["image"] = image_path
             crop_img = imread(
                 os.path.join(self.script_root, str(value['filename'])))
             arg["resolution"] = get_resolution(crop_img)
     return code
Beispiel #4
0
 def screenshot(self, file_path=None):
     if file_path:
         self.save_screenshot(file_path)
     else:
         file_path = os.path.join(ST.LOG_DIR, "temp.jpg")
         self.save_screenshot(file_path)
         screen = aircv.imread(file_path)
         return screen
Beispiel #5
0
def loop_find4(total_pic,
               query,
               timeout=ST.FIND_TIMEOUT,
               threshold=None,
               interval=0.5,
               intervalfunc=None):
    """
    Search for image template in the screen until timeout

    Args:
        query: image template to be found in screenshot
        timeout: time interval how long to look for the image template
        threshold: default is None
        interval: sleep interval before next attempt to find the image template
        intervalfunc: function that is executed after unsuccessful attempt to find the image template

    Raises:
        TargetNotFoundError: when image template is not found in screenshot

    Returns:
        TargetNotFoundError if image template not found, otherwise returns the position where the image template has
        been found in screenshot

    """
    G.LOGGING.info("Try finding:\n%s", query)
    start_time = time.time()
    while True:
        # screen = G.DEVICE.snapshot(filename=None, quality=ST.SNAPSHOT_QUALITY)
        screen = aircv.imread(total_pic)
        if screen is None:
            G.LOGGING.warning("Screen is None, may be locked")
        else:
            if threshold:
                query.threshold = threshold
            match_pos = query.match_in(screen)
            if match_pos:
                try_log_screen(screen)
                return match_pos

        if intervalfunc is not None:
            intervalfunc()

        # 超时则raise,未超时则进行下次循环:
        if (time.time() - start_time) > timeout:
            try_log_screen(screen)
            raise TargetNotFoundError('Picture %s not found in screen' % query)
        else:
            time.sleep(interval)
Beispiel #6
0
 def screenshot(self, file_path=None):
     if file_path:
         file = self.capture_page_screenshot(file_path)
         with open(file, 'rb') as fp:
             allure.attach(fp.read(), '截图{}'.format(file_path),
                           allure.attachment_type.PNG)
     else:
         if not self.screenshot_directory:
             file_path = "temp.png"
         else:
             file_path = os.path.join('', "temp.png")
         file = self.capture_page_screenshot(file_path)
         with open(file, 'rb') as fp:
             allure.attach(fp.read(), '截图{}'.format(file_path),
                           allure.attachment_type.PNG)
         screen = aircv.imread(file_path)
         return screen
Beispiel #7
0
 def _translate_code(self, step):
     if step["tag"] != "function":
         return None
     step_data = step["data"]
     args = []
     code = {
         "name": step_data["name"],
         "args": args,
     }
     for key, value in step_data["call_args"].items():
         args.append({
             "key": key,
             "value": value,
         })
     for k, arg in enumerate(args):
         value = arg["value"]
         if isinstance(value,
                       dict) and value.get("__class__") == "Template":
             if self.export_dir:  # all relative path
                 image_path = value['filename']
                 if not os.path.isfile(
                         os.path.join(self.script_root, image_path)):
                     shutil.copy(value['_filepath'], self.script_root
                                 )  # copy image used by using statement
             else:
                 if self.online_path:
                     _filepath = os.path.abspath(value['_filepath'])
                     jk_filepath = _filepath.split(self.proj_name)[1]
                     image_path = self.online_path + jk_filepath.replace(
                         '\\', '/')
                 else:
                     image_path = os.path.abspath(value['_filepath']
                                                  or value['filename'])
             arg["image"] = image_path
             print('    arg["image"]: ', arg["image"])
             crop_img = imread(value['_filepath'] or value['filename'])
             arg["resolution"] = get_resolution(crop_img)
     return code
Beispiel #8
0
 def _imread(self):
     return aircv.imread(self.filepath)
Beispiel #9
0
def loop_find3(total_pic,
               query,
               posi,
               tag=None,
               timeout=ST.FIND_TIMEOUT,
               threshold=None,
               interval=0.5,
               intervalfunc=None):
    # print("ST.FIND_TIMEOUT",ST.FIND_TIMEOUT)
    G.LOGGING.info("Try finding:\n%s", query.record_pos)
    G.LOGGING.info(query._imread().shape)
    # screen_resolution =  aircv.get_resolution(screen)
    part_size_width = query._imread().shape[0]
    part_size_height = query._imread().shape[1]
    part_resolution_width = query.resolution[0]
    part_resolution_height = query.resolution[1]

    # pairt_origin_xmin_relative = posi[0];
    # pairt_origin_xmax_relative = posi[2] ;
    pairt_origin_ymin_relative = posi[0] / part_resolution_height
    pairt_origin_ymax_relative = posi[1] / part_resolution_height

    # print("香菇:",(pairt_origin_ymin_relative,pairt_origin_ymax_relative))

    posi_x = query.record_pos[0]
    posi_y = query.record_pos[1]
    # print("posi_x",posi_x)

    start_time = time.time()
    screen = aircv.imread(total_pic)
    G.LOGGING.debug("query path %s" % query.filepath)
    # predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax))
    screen_resolution = aircv.get_resolution(screen)
    # print("总图片的分辨率",screen_resolution);
    total_resolution_width = screen_resolution[0]
    total_resolution_height = screen_resolution[1]
    width_scale = round(total_resolution_width / part_resolution_width, 2)
    height_scale = round(total_resolution_height / part_resolution_height, 2)
    # print("width_scale",width_scale)
    # print("width_scale",height_scale)
    # print("总图的分辨率",(total_resolution_width,total_resolution_height))
    # print("缩放之后的相对坐标",(pairt_origin_xmin_relative * width_scale,pairt_origin_ymin_relative * height_scale,pairt_origin_xmax_relative * width_scale,pairt_origin_ymax_relative * height_scale))
    # pairt_end_xmin_absolute = round( (pairt_origin_xmin_relative * width_scale) * total_resolution_width );
    pairt_end_xmin_absolute = 0
    # pairt_end_xmax_absolute = round( (pairt_origin_xmax_relative * width_scale) * total_resolution_width );
    pairt_end_xmax_absolute = total_resolution_width
    pairt_end_ymin_absolute = round(
        (pairt_origin_ymin_relative * height_scale) *
        total_resolution_height) - 30
    pairt_end_ymax_absolute = round(
        (pairt_origin_ymax_relative * height_scale) *
        total_resolution_height) + 30
    # print("enddddd",(pairt_end_xmin_absolute,pairt_end_ymin_absolute,pairt_end_xmax_absolute,pairt_end_ymax_absolute))

    # part_resolution_width_scaled = part_size_width * width_scale;
    # part_resolution_height_scaled = part_size_height * height_scale;

    # part_end_xmax = ((posi_x + 0.5) if (posi_x + 0.5)<=1 else 1)*total_resolution_width
    # part_end_ymax = ((posi_y + 0.5) if (posi_y + 0.5)<=1 else 1)*total_resolution_height
    # part_end_xmin = part_end_xmax - part_resolution_width_scaled
    # part_end_ymin = part_end_ymax - part_resolution_height_scaled
    predict_area = aircv.crop_image(
        screen, (pairt_end_xmin_absolute, pairt_end_ymin_absolute,
                 pairt_end_xmax_absolute, pairt_end_ymax_absolute))
    screen[0:pairt_end_ymin_absolute, 0:total_resolution_width] = 0
    screen[pairt_end_ymax_absolute:total_resolution_height,
           0:total_resolution_width] = 0
    # print("黑素区域",(pairt_end_ymin_absolute,pairt_end_ymax_absolute))
    # screen[200:250,0:50] = 0;
    aircv.imwrite("part_black.png", screen, 99)
    # predict_area = aircv.crop_image(screen, (0, 0, 1126, 2436));

    # print("hahahahah")
    # print((part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax))
    # print(predict_area.shape)
    aircv.imwrite("predict_area3.png", predict_area, 99)
    while True:

        if predict_area is None:
            G.LOGGING.warning("预测区域有问题!")
        else:
            if threshold:
                query.threshold = threshold
            match_pos = query.match_in(screen)
            if match_pos:
                try_log_screen(screen)
                return match_pos

        if intervalfunc is not None:
            intervalfunc()

        # 超时则raise,未超时则进行下次循环:
        if (time.time() - start_time) > timeout:
            try_log_screen(screen)
            # raise TargetNotFoundError('Picture %s not found in screen' % query)
            G.LOGGING.warning("图片查找超时!")
            break
        else:
            time.sleep(interval)
Beispiel #10
0
def loop_find2(total_pic,
               query,
               timeout=ST.FIND_TIMEOUT,
               threshold=None,
               interval=0.5,
               intervalfunc=None):
    G.LOGGING.info("Try finding:\n%s", query.record_pos)
    G.LOGGING.info(query._imread().shape)
    part_size_width = query._imread().shape[0]
    part_size_height = query._imread().shape[1]
    part_resolution_width = query.resolution[0]
    part_resolution_height = query.resolution[1]

    # print("part_resolution_width:" + str(part_resolution_width))
    # print("part_resolution_height:" + str(part_resolution_height))
    posi_x = query.record_pos[0]
    posi_y = query.record_pos[1]
    print("posi_x", posi_x)

    start_time = time.time()
    screen = aircv.imread(total_pic)
    G.LOGGING.debug("query path %s" % query.filepath)
    # predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax))
    screen_resolution = aircv.get_resolution(screen)
    total_resolution_width = query.resolution[0]
    total_resolution_height = query.resolution[1]
    width_scale = total_resolution_width / part_resolution_width
    height_scale = total_resolution_height / part_resolution_height

    print("test_scale:" + str(1 / 2))
    print("width_scale:" + str(width_scale))
    print("height_scase:" + str(height_scale))
    part_resolution_width_scaled = part_size_width * width_scale
    part_resolution_height_scaled = part_size_height * height_scale
    print("part_resolution_width_scaled:" + str(part_resolution_width_scaled))
    print("part_resolution_height_scaled:" +
          str(part_resolution_height_scaled))
    G.LOGGING.debug("图片的分辨率")
    G.LOGGING.debug(screen_resolution)

    part_end_xmax = ((posi_x + 0.5) if
                     (posi_x + 0.5) <= 1 else 1) * total_resolution_width
    part_end_ymax = ((posi_y + 0.5) if
                     (posi_y + 0.5) <= 1 else 1) * total_resolution_height
    part_end_xmin = part_end_xmax - part_resolution_width_scaled
    part_end_ymin = part_end_ymax - part_resolution_height_scaled
    predict_area = aircv.crop_image(
        screen, (part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax))
    print("hahahahah")
    print((part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax))
    print(predict_area.shape)
    aircv.imwrite("predict_area.png", screen, 99)
    while True:

        if predict_area is None:
            G.LOGGING.warning("预测区域有问题!")
        else:
            if threshold:
                query.threshold = threshold
            match_pos = query.match_in(screen)
            if match_pos:
                try_log_screen(screen)
                return match_pos

        if intervalfunc is not None:
            intervalfunc()

        # 超时则raise,未超时则进行下次循环:
        if (time.time() - start_time) > timeout:
            try_log_screen(predict_area)
            # raise TargetNotFoundError('Picture %s not found in screen' % query)
            G.LOGGING.warning("图片查找超时!")
            break
        else:
            time.sleep(interval)
Beispiel #11
0
 def load_images(self, search_file, source_file):
     """加载待匹配图片."""
     self.search_file, self.source_file = search_file, source_file
     self.im_search, self.im_source = imread(self.search_file), imread(self.source_file)
     # 初始化对象
     self.check_macthing_object = CheckKeypointResult(self.im_search, self.im_source)
Beispiel #12
0
    def setUpClass(cls):
        cls.keypoint_sch = imread("matching_images/keypoint_search.png")
        cls.keypoint_src = imread("matching_images/keypoint_screen.png")

        cls.template_sch = imread("matching_images/template_search.png")
        cls.template_src = imread("matching_images/template_screen.png")
Beispiel #13
0
 def _imread(self):
     if not os.path.isfile(self.filepath):
         raise aircv.FileNotExistError("File not exist in current directory!")
     return aircv.imread(self.filepath)
Beispiel #14
0
def load_img(fpn):
    try:
        return aircv.imread(fpn)
    except Exception as e:
        logger.error(f'Load Image Failed({str(e)}),FileTarget is {fpn}')
        return None