Пример #1
0
    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
Пример #2
0
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
Пример #3
0
    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"]
Пример #4
0
    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']
Пример #5
0
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)
Пример #6
0
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,
    )
Пример #7
0
    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
Пример #8
0
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"]
Пример #9
0
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)
Пример #10
0
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import json

from findit import FindIt

from fitch import config
from fitch.logger import logger

TEMP_TEMPLATE_NAME = 'cur_template'
TEMP_TARGET_NAME = 'cur_target'

fi = FindIt(
    engine=['template'],
    engine_template_cv_method_name=config.CV_METHOD_NAME,
    engine_template_scale=config.CV_PIC_SCALE,
    pro_mode=True,
)


def detect(template: str, target: str) -> dict:
    # load template picture
    fi.load_template(TEMP_TEMPLATE_NAME, pic_path=template)
    # and find it
    result = fi.find(TEMP_TARGET_NAME, target_pic_path=target)
    fi.clear()
    logger.debug('Detect result: {}'.format(json.dumps(result)))
    return result


def cal_location(result_dict: dict) -> (list, tuple):
Пример #11
0
import time

# 初始化
fi = FindIt(
    # 是否需要log,默认否
    need_log=False,
    # 支持 模板匹配 与 特征识别,默认都使用
    engine=['template', 'feature'],
    # pro模式会带有更为丰富的结果数据,默认不打开
    pro_mode=False,

    # 引擎相关参数均为engine开头
    # 模板匹配相关
    # 为了更好的检测率,findit专门设计了scale参数
    # 该参数会在一定范围内缩放模板图片,并进行逐一匹配
    # 默认为 (1, 3, 10) ,意思是:
    # 步长 = (3 - 1) / 10 = 0.2
    # 那么,模板图片会依次进行 (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. 通过图片路径加载图片
Пример #12
0
from stagesep2.analyser.base import BaseAnalyser
from stagesep2.config import MatchTemplateConfig
from findit import FindIt

fi = FindIt(
    engine=['template'],
    cv_method_name=MatchTemplateConfig.cv_method,
)


class MatchTemplateAnalyser(BaseAnalyser):
    """ match-template analyser """
    name = 'match_template'

    @classmethod
    def run(cls, frame, ssv):
        template_pic_dict = ssv.template_manager.get_dict()
        for each_pic_name, each_pic in template_pic_dict.items():
            fi.load_template(each_pic_name, pic_object=each_pic.cv_object)

        match_result = fi.find(
            'temp',
            target_pic_object=frame,
            engine_template_scale=(1, 2, 5)
        )['data']

        result_dict = dict()
        for each_pic_name, each_pic_result in match_result.items():
            result_dict[each_pic_name] = {
                # no min any more
                'min': -1,
Пример #13
0
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)
Пример #14
0
from stagesep2.analyser.base import BaseAnalyser
from stagesep2.config import MatchTemplateConfig
from findit import FindIt

fi = FindIt(cv_method_name=MatchTemplateConfig.cv_method)


class MatchTemplateAnalyser(BaseAnalyser):
    """ match-template analyser """
    name = 'match_template'

    @classmethod
    def run(cls, frame, ssv):
        result_dict = dict()
        template_pic_dict = ssv.template_manager.get_dict()

        for each_pic_name, each_pic in template_pic_dict.items():
            fi.load_template(pic_object_list=(each_pic.pic_path,
                                              each_pic.cv_object))
            match_result = fi.find(target_pic_object=frame)['data'][0]
            max_val, min_val = match_result['max_val'], match_result['min_val']

            result_dict[each_pic_name] = {
                'min': min_val,
                'max': max_val,
            }
            fi.reset()
        return result_dict