From 78c898e8f4fbade423c91353d202b4a7bf76c92b Mon Sep 17 00:00:00 2001 From: niuhuan Date: Tue, 30 Apr 2024 10:56:14 +0800 Subject: [PATCH] cache image --- 3rdparty/Fhoe-Rail | 2 +- module/automation/automation.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/3rdparty/Fhoe-Rail b/3rdparty/Fhoe-Rail index fafe324b..bebbbe7d 160000 --- a/3rdparty/Fhoe-Rail +++ b/3rdparty/Fhoe-Rail @@ -1 +1 @@ -Subproject commit fafe324b6b555f60a8f928fcdfb88e5dfd5df569 +Subproject commit bebbbe7d660263f775e008f576aeae33fc6c3093 diff --git a/module/automation/automation.py b/module/automation/automation.py index 2490a79d..bf94501c 100644 --- a/module/automation/automation.py +++ b/module/automation/automation.py @@ -27,6 +27,7 @@ def __init__(self, window_title, logger: Optional[Logger] = None): self.logger = logger self.screenshot = None self._init_input() + self.img_cache = {} def _init_input(self): """ @@ -78,7 +79,7 @@ def calculate_positions(self, template, max_loc, relative): bottom_right = (top_left[0] + int(width / scale_factor), top_left[1] + int(height / scale_factor)) return top_left, bottom_right - def find_image_element(self, target, threshold, scale_range, relative=False): + def find_image_element(self, target, threshold, scale_range, relative=False, cacheable=True): """ 查找图像元素。 :param target: 目标图像路径。 @@ -88,8 +89,14 @@ def find_image_element(self, target, threshold, scale_range, relative=False): :return: 最佳匹配位置和相似度。 """ try: - mask = ImageUtils.read_template_with_mask(target) # 读取模板图片掩码 - template = cv2.imread(target) # 读取模板图片 + if cacheable and target in self.img_cache: + mask = self.img_cache[target]['mask'] + template = self.img_cache[target]['template'] + else: + mask = ImageUtils.read_template_with_mask(target) # 读取模板图片掩码 + template = cv2.imread(target) # 读取模板图片 + if cacheable: + self.img_cache[target] = {'mask': mask, 'template': template} screenshot = cv2.cvtColor(np.array(self.screenshot), cv2.COLOR_BGR2RGB) # 将截图转换为RGB if mask is not None: matchVal, matchLoc = ImageUtils.scale_and_match_template(screenshot, template, threshold, scale_range, mask) # 执行缩放并匹配模板