Ejemplo n.º 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
Ejemplo n.º 2
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"]
Ejemplo n.º 3
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
Ejemplo n.º 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']
Ejemplo n.º 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)
Ejemplo n.º 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,
    )
Ejemplo n.º 7
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"]
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
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)
    # 同样的,你可以传入图片路径或对象
    # mask_pic_path='wechat_logo.png',
    # mask_pic_object=some_object,

    # 如果你希望确认分析结果,你可以打开 mark_pic 开关
    # 打开后,将会保存一张 标记了最终结果 的图片到本地,供参考用
    # mark_pic=True,
)

# 在分析后,你可以通过 clear 重置所有模板。当然你也可以选择保留以进行其他分析。
fi.clear()

# sample result
time.sleep(1)
pprint.pprint(result)
# {'data': {'app_store_logo': {'FeatureEngine': (94.66734313964844,
Ejemplo n.º 10
0
Archivo: demo.py Proyecto: lycfr/findit
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)