Example #1
0
    def find(
        self,
        target_pic_name: str,
        target_pic_path: str = None,
        target_pic_object: np.ndarray = None,
        *args,
        **kwargs,
    ) -> dict:
        """
        start match

        :param target_pic_name: eg: 'your_target_picture_1'
        :param target_pic_path: '/path/to/your/target.png'
        :param target_pic_object: your_pic_cv_object (loaded by cv2)

        kwargs here will be used to engine.execute(), which starts with engine_{engine_name}:

            # ocr
            engine_ocr_offset: int = None,
            engine_ocr_deep: bool = None,

            # template
            engine_template_mask_pic_object: np.ndarray = None,
            engine_template_mask_pic_path: str = None,
        :return:
        """

        # pre assert
        assert (target_pic_path
                is not None) or (target_pic_object
                                 is not None), "need path or cv object"

        # load target
        logger.info("start finding ...")
        target_pic_object = toolbox.pre_pic(target_pic_path, target_pic_object)

        if self._need_template():
            find_func = self._find_with_template
        else:
            find_func = self._find_without_template
        result = find_func(
            target_pic_object,
            target_pic_name=target_pic_name,
            target_pic_path=target_pic_path,
            *args,
            **kwargs,
        )
        return {
            "target_name": target_pic_name,
            "target_path": target_pic_path,
            "data": result,
        }
Example #2
0
    def execute(
        self,
        template_object: np.ndarray,
        target_object: np.ndarray,
        engine_template_mask_pic_object: np.ndarray = None,
        engine_template_mask_pic_path: str = None,
        *_,
        **__,
    ) -> FindItEngineResponse:
        resp = FindItEngineResponse()
        resp.append("conf", self.__dict__)

        # mask
        if (engine_template_mask_pic_path
                is not None) or (engine_template_mask_pic_object is not None):
            logger.info("mask detected")
            engine_template_mask_pic_object = toolbox.pre_pic(
                engine_template_mask_pic_path, engine_template_mask_pic_object)

        # template matching
        min_val, max_val, min_loc, max_loc, point_list = self._compare_template(
            template_object,
            target_object,
            self.engine_template_scale,
            engine_template_mask_pic_object,
        )

        # 'target_point' must existed
        resp.append("target_point", max_loc, important=True)
        resp.append("target_sim", max_val, important=True)
        resp.append(
            "raw",
            {
                "min_val": min_val,
                "max_val": max_val,
                "min_loc": min_loc,
                "max_loc": max_loc,
                "all": point_list,
            },
        )
        resp.append("ok", True, important=True)

        return resp
Example #3
0
    def find(self,
             target_pic_name: str,
             target_pic_path: str = None,
             target_pic_object: np.ndarray = None,
             *args,
             **kwargs) -> dict:
        """
        start match

        :param target_pic_name: eg: 'your_target_picture_1'
        :param target_pic_path: '/path/to/your/target.png'
        :param target_pic_object: your_pic_cv_object (loaded by cv2)
        :return:
        """

        # pre assert
        assert (target_pic_path
                is not None) or (target_pic_object
                                 is not None), 'need path or cv object'

        # load target
        logger.info('start finding ...')
        target_pic_object = toolbox.pre_pic(target_pic_path, target_pic_object)

        if self._need_template():
            find_func = self._find_with_template
        else:
            find_func = self._find_without_template
        result = find_func(target_pic_object,
                           target_pic_name=target_pic_name,
                           target_pic_path=target_pic_path,
                           *args,
                           **kwargs)
        return {
            'target_name': target_pic_name,
            'target_path': target_pic_path,
            'data': result
        }
Example #4
0
    def execute(self,
                template_object: np.ndarray,
                target_object: np.ndarray,
                mask_pic_object: np.ndarray = None,
                mask_pic_path: str = None,
                *_,
                **__) -> dict:
        # mask
        if (mask_pic_path is not None) or (mask_pic_object is not None):
            logger.info('mask detected')
            mask_pic_object = toolbox.pre_pic(mask_pic_path, mask_pic_object)

        # template matching
        min_val, max_val, min_loc, max_loc, point_list = self._compare_template(
            template_object, target_object, self.scale, mask_pic_object)

        # 'target_point' must existed
        return {
            'target_point': max_loc,
            'target_sim': max_val,
            'conf': {
                'engine_template_cv_method_name':
                self.cv_method_name,
                'engine_template_scale':
                self.scale,
                'engine_template_multi_target_max_threshold':
                self.multi_target_max_threshold,
                'engine_template_multi_target_distance_threshold':
                self.multi_target_distance_threshold
            },
            'raw': {
                'min_val': min_val,
                'max_val': max_val,
                'min_loc': min_loc,
                'max_loc': max_loc,
                'all': point_list,
            }
        }
Example #5
0
    def execute(self,
                template_object: np.ndarray,
                target_object: np.ndarray,
                engine_template_mask_pic_object: np.ndarray = None,
                engine_template_mask_pic_path: str = None,
                *_,
                **__) -> FindItEngineResponse:
        resp = FindItEngineResponse()
        resp.append('conf', self.__dict__)

        # mask
        if (engine_template_mask_pic_path
                is not None) or (engine_template_mask_pic_object is not None):
            logger.info('mask detected')
            engine_template_mask_pic_object = toolbox.pre_pic(
                engine_template_mask_pic_path, engine_template_mask_pic_object)

        # template matching
        min_val, max_val, min_loc, max_loc, point_list = self._compare_template(
            template_object, target_object, self.engine_template_scale,
            engine_template_mask_pic_object)

        # 'target_point' must existed
        resp.append('target_point', max_loc, important=True)
        resp.append('target_sim', max_val, important=True)
        resp.append(
            'raw', {
                'min_val': min_val,
                'max_val': max_val,
                'min_loc': min_loc,
                'max_loc': max_loc,
                'all': point_list,
            })
        resp.append('ok', True, important=True)

        return resp
Example #6
0
    def find(self,
             target_pic_name: str,
             target_pic_path: str = None,
             target_pic_object: np.ndarray = None,
             mark_pic: bool = None,
             *args,
             **kwargs):
        """
        start match

        :param target_pic_name: eg: 'your_target_picture_1'
        :param target_pic_path: '/path/to/your/target.png'
        :param target_pic_object: your_pic_cv_object (loaded by cv2)
        :param mark_pic: enable this, and you will get a picture file with a mark of result
        :return:
        """

        # pre assert
        assert self.template, 'template is empty'
        assert (target_pic_path
                is not None) or (target_pic_object
                                 is not None), 'need path or cv object'

        # load target
        logger.info('start finding ...')
        target_pic_object = toolbox.pre_pic(target_pic_path, target_pic_object)

        start_time = toolbox.get_timestamp()
        result = dict()
        for each_template_name, each_template_object in self.template.items():
            logger.debug(
                'start analysing: [{}] ...'.format(each_template_name))

            current_result = dict()
            for each_engine in self.engine_list:
                each_result = each_engine.execute(each_template_object,
                                                  target_pic_object, *args,
                                                  **kwargs)

                # need mark?
                if mark_pic:
                    target_pic_object_with_mark = toolbox.mark_point(
                        target_pic_object,
                        each_result['target_point'],
                        cover=False)
                    os.makedirs(start_time, exist_ok=True)
                    mark_pic_path = '{}/{}_{}.png'.format(
                        start_time, each_template_name, each_engine.get_type())
                    cv2.imwrite(mark_pic_path, target_pic_object_with_mark)
                    logger.debug(
                        'save marked picture to {}'.format(mark_pic_path))

                # result filter
                each_result = self._prune_result(each_result)

                current_result[each_engine.get_type()] = each_result

            logger.debug('result for [{}]: {}'.format(
                each_template_name, json.dumps(current_result)))
            result[each_template_name] = current_result

        final_result = {
            'target_name': target_pic_name,
            'target_path': target_pic_path,
            'data': result,
        }
        logger.info('result: {}'.format(json.dumps(final_result)))
        return final_result