| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- # This Python file uses the following encoding: utf-8
- # @author runhey
- # github https://github.com/runhey
- import numpy as np
- from module.atom.image import RuleImage
- class RuleGif:
- # 大部分实现同RuleImage 的接口
- @property
- def name(self) -> str:
- return self.targets[0].name
- def __init__(self, targets: list[RuleImage]):
- self.targets = targets
- self.roi_front: list = [0, 0, 0, 0]
- self.roi_back: list = targets[0].roi_back
- self._match_init = False
- self.appear_target = targets[0]
- def pre_process(self, image):
- return image
- def search(self, image, roi: list = None, threshold: float = None) -> tuple:
- """
- :param image:
- :param roi:
- :param threshold:
- :return: bool
- 第一项是否为出现, 第二项为匹配的RuleImage
- """
- image = self.pre_process(image)
- #
- threshold = self.targets[0].threshold if threshold is None else threshold
- roi = self.targets[0].roi_back if roi is None else roi
- for target in self.targets:
- target.roi_back = roi
- if target.match(image, threshold):
- self.roi_front = target.roi_front
- self.appear_target = target
- return True, target
- return False, None
- def search_with_multi_scale(self, image, roi=None, threshold=None, scales=None, scale_range=(0.8, 1.1, 0.05)):
- image = self.pre_process(image)
- threshold = self.targets[0].threshold if threshold is None else threshold
- roi = self.targets[0].roi_back if roi is None else roi
- for target in self.targets:
- target.roi_back = roi
- if target.match_multi_scale(image, threshold=threshold, scales=scales, scale_range=scale_range):
- self.roi_front = target.roi_front
- self.appear_target = target
- return True, target
- return False, None
- def match_multi_scale(self, image, threshold: float = None, scales: list = None, scale_range: tuple = None) -> bool:
- return self.search_with_multi_scale(image, threshold=threshold, scales=scales, scale_range=scale_range)[0]
- def match(self, image, threshold: float = None) -> bool:
- return self.search(image, threshold=threshold)[0]
- def coord(self) -> tuple:
- x, y, w, h = self.roi_front
- return x + np.random.randint(0, w), y + np.random.randint(0, h)
- def front_center(self) -> tuple:
- x, y, w, h = self.roi_front
- return int(x + w//2), int(y + h//2)
|