def contain_image(self, image_path: str = None, image_object: np.ndarray = None, threshold: float = None, *args, **kwargs): assert image_path or image_object, 'should fill image_path or image_object' if not threshold: threshold = 0.99 if image_path: logger.debug(f'found image path, use it first: {image_path}') assert os.path.isfile( image_path), f'image {image_path} not existed' image_object = cv2.imread(image_path) image_object = toolbox.turn_grey(image_object) # TODO use client or itself..? fi = FindIt(engine=['template']) fi_template_name = 'default' fi.load_template(fi_template_name, pic_object=image_object) with toolbox.video_capture(self.video.path) as cap: target_id = self.pick(*args, **kwargs)[0] frame = toolbox.get_frame(cap, target_id) frame = toolbox.turn_grey(frame) result = fi.find(str(target_id), target_pic_object=frame) find_result = result['data'][fi_template_name]['TemplateEngine'] position = find_result['target_point'] sim = find_result['target_sim'] logger.debug(f'position: {position}, sim: {sim}') return sim > threshold
def contain_image(self, image_path: str = None, image_object: np.ndarray = None, *args, **kwargs) -> typing.Dict: assert image_path or image_object, "should fill image_path or image_object" if image_path: logger.debug(f"found image path, use it first: {image_path}") assert os.path.isfile( image_path), f"image {image_path} not existed" image_object = toolbox.imread(image_path) image_object = toolbox.turn_grey(image_object) # TODO use client or itself..? fi = FindIt(engine=["template"]) fi_template_name = "default" fi.load_template(fi_template_name, pic_object=image_object) target_id = self.pick(*args, **kwargs)[0] operator = self.video.get_operator() frame = operator.get_frame_by_id(target_id) result = fi.find(str(target_id), target_pic_object=frame.data) return result["data"][fi_template_name]["TemplateEngine"]
class TemplateCompareHook(BaseHook): def __init__(self, template_dict: typing.Dict[str, str], *args, **kwargs): """ args and kwargs will be sent to findit.__init__ :param template_dict: # k: template name # v: template picture path :param args: :param kwargs: """ super().__init__(*args, **kwargs) self.fi = FindIt(*args, **kwargs) self.template_dict = template_dict @change_origin def do( self, frame_id: int, frame: np.ndarray, *_, **__ ) -> typing.Optional[np.ndarray]: super().do(frame_id, frame, *_, **__) for each_template_name, each_template_path in self.template_dict.items(): self.fi.load_template(each_template_name, each_template_path) res = self.fi.find(str(frame_id), target_pic_object=frame) logger.debug(f"compare with template {self.template_dict}: {res}") self.result[frame_id] = res return
def contain_image(self, image_path: str = None, image_object: np.ndarray = None, *args, **kwargs) -> typing.Dict: assert image_path or image_object, 'should fill image_path or image_object' if image_path: logger.debug(f'found image path, use it first: {image_path}') assert os.path.isfile( image_path), f'image {image_path} not existed' image_object = toolbox.imread(image_path) image_object = toolbox.turn_grey(image_object) # TODO use client or itself..? fi = FindIt(engine=['template']) fi_template_name = 'default' fi.load_template(fi_template_name, pic_object=image_object) with toolbox.video_capture(self.video.path) as cap: target_id = self.pick(*args, **kwargs)[0] frame = toolbox.get_frame(cap, target_id) frame = toolbox.turn_grey(frame) result = fi.find(str(target_id), target_pic_object=frame) return result['data'][fi_template_name]['TemplateEngine']
def analyse(): # required # support multi pictures, split with ',' template_name = request.form.get("template_name") template_name_list = template_name.split(",") if template_name else list() template_dict = dict() # optional extra_str = request.form.get("extras") extra_dict = json.loads(extra_str) if extra_str else dict() new_extra_dict = utils.handle_extras(extra_dict) for each_template_name in template_name_list: template_path = utils.get_pic_path_by_name(each_template_name) # file not existed if not template_path: return std_response( status=STATUS_CLIENT_ERROR, msg=f"no template named: {each_template_name}", request=request.form, response=dict(), ) template_dict[each_template_name] = template_path # save target pic target_pic_file = request.files["file"] temp_pic_file_object = tempfile.NamedTemporaryFile(mode="wb+", suffix=".png", delete=False) temp_pic_file_object.write(target_pic_file.read()) temp_pic_file_object.close() # init findit fi = FindIt(need_log=True, **new_extra_dict) # load all templates for each_template_name, each_template_path in template_dict.items(): fi.load_template(each_template_name, pic_path=each_template_path) _response = fi.find( config.DEFAULT_TARGET_NAME, target_pic_path=temp_pic_file_object.name, **new_extra_dict, ) # clean os.remove(temp_pic_file_object.name) return std_response(status=STATUS_OK, msg="", request=request.form, response=_response)
def analyse(): # required # support multi pictures, split with ',' template_name = request.form.get('template_name') template_name_list = template_name.split(',') template_dict = dict() for each_template_name in template_name_list: template_path = utils.get_pic_path_by_name(each_template_name) # file not existed if not template_path: return std_response( status=STATUS_CLIENT_ERROR, msg='no template named {}'.format(each_template_name), request=request.form, response='', ) template_dict[each_template_name] = template_path # optional extra_dict = json.loads(request.form.get('extras')) new_extra_dict = utils.handle_extras(extra_dict) # save target pic target_pic_file = request.files['file'] temp_pic_file_object = tempfile.NamedTemporaryFile(mode='wb+', suffix='.png', delete=False) temp_pic_file_object.write(target_pic_file.read()) temp_pic_file_object.close() # init findit fi = FindIt(need_log=True, **new_extra_dict) # load all templates for each_template_name, each_template_path in template_dict.items(): fi.load_template(each_template_name, pic_path=each_template_path) _response = fi.find(config.DEFAULT_TARGET_NAME, target_pic_path=temp_pic_file_object.name, **new_extra_dict) # clean os.remove(temp_pic_file_object.name) return std_response( status=STATUS_OK, msg='', request=request.form, response=_response, )
def match_template_with_object( template: np.ndarray, target: np.ndarray, engine_template_cv_method_name: str = None, **kwargs, ) -> typing.Dict[str, typing.Any]: # change the default method if not engine_template_cv_method_name: engine_template_cv_method_name = "cv2.TM_CCOEFF_NORMED" fi = FindIt( engine=["template"], engine_template_cv_method_name=engine_template_cv_method_name, **kwargs, ) # load template fi_template_name = "default" fi.load_template(fi_template_name, pic_object=template) result = fi.find(target_pic_name="", target_pic_object=target, **kwargs) logger.debug(f"findit result: {result}") return result["data"][fi_template_name]["TemplateEngine"]
import pprint from findit import FindIt fi = FindIt() fi.load_template('wechat_logo', pic_path='pics/wechat_logo.png') fi.load_template('app_store_logo', pic_path='pics/app_store_logo.png') fi.load_template('music_logo', pic_path='pics/music_logo.png') fi.load_template('album_logo', pic_path='pics/album_logo.png') result = fi.find( target_pic_name='screen', target_pic_path='pics/screen.png', ) pprint.pprint(result)
# 那么,模板图片会依次进行 (1.0, 1.2, 1.4, 1.6, ... 2.6, 2.8, 3.0) 倍的放缩逐一比较,以达到最佳的适配效果 engine_template_scale=(1, 3, 10), # 默认的模板匹配算法为 TM_CCORR_NORMED # 你也可以在此处修改为你偏好的 engine_template_cv_method_name='cv2.TM_CCORR_NORMED', # 特征识别相关 # 距离阈值 engine_feature_distance_threshold=0.75, # 聚类核心数 engine_feature_cluster_num=3, ) # 加载模板 # 1. 通过图片路径加载图片 fi.load_template('wechat_logo', pic_path='pics/wechat_logo.png') # 2. 或者直接加载通过cv2加载进来的图片 pic_object = cv2.imread('pics/app_store_logo.png') # 传入的时候注意,传入的是一个列表,形式为 (名称,图片对象) fi.load_template('app_store_logo', pic_object=pic_object) # 在加载模板后即可开始分析 result = fi.find( target_pic_name='screen', # 目标图片可以直接传入路径 target_pic_path='pics/screen.png', # 当然,也可以是cv2对象 # target_pic_object=some_object, # 支持了蒙版图片的检测 (https://github.com/williamfzc/findit/issues/1)
from findit import FindIt import cv2 import pprint # new one fi = FindIt() # change config fi.config.cv_method = cv2.TM_CCOEFF_NORMED # load template picture fi.load_template('./wechat_logo.png') # and find it # scale will resize template picture and auto-compare # (1, 3, 10) means looping (1.0, 1.2, 1.4, 1.6, ... 2.6, 2.8, 3.0) times result = fi.find('./wechat_screen.png', scale=(1, 3, 10)) # result looks like: # { # 'some desc': 'xxx', # 'data': { # 'template1.png': { # 'position': (100, 500), # 'sim': 0.66, # } # } # } pprint.pprint(result)