Skip to content

Commit

Permalink
Merge pull request #244 from niuhuan/assets-image-cache
Browse files Browse the repository at this point in the history
cache image
  • Loading branch information
moesnow authored May 2, 2024
2 parents 17d6946 + 78c898e commit 22c1135
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/Fhoe-Rail
Submodule Fhoe-Rail updated 109 files
13 changes: 10 additions & 3 deletions module/automation/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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: 目标图像路径。
Expand All @@ -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) # 执行缩放并匹配模板
Expand Down

0 comments on commit 22c1135

Please sign in to comment.