Example #1
0
    def match_score_internal(self, img_obj, debug=None):
        debug = debug or self._debug

        bg_pixels = 0
        bg_ratio = 0.0
        bg_matched = False

        fg_pixels = 0
        fg_ratio = 0.0
        fg_matched = False

        # Phase 2: Background check
        try:
            if img_obj['bg'] is None:
                if self._bg_method.want_grayscale_image and (img_obj['gray'] is
                                                             None):
                    self.generate_grayscale_image(img_obj)

                img_bg = 255 - \
                    self._bg_method(img_bgr=img_obj['bgr'], img_gray=img_obj['gray'])
                img_obj['bg'] = self._kernel.encode(img_bg)
            bg_pixels = self._kernel.logical_and_popcnt(img_obj['bg'])

            # fixme: zero division
            bg_ratio = bg_pixels / (self._width * self._height)
            bg_matched = (bg_ratio <= self._orig_threshold)
        except:
            IkaUtils.dprint(
                '%s (%s): bg_method %s caused a exception.' %
                (self, self._label, self._bg_method.__class__.__name__))
            IkaUtils.dprint(traceback.format_exc())

        # Phase 3: Foreground check
        if bg_matched:
            if img_obj['fg'] is None:
                if self._fg_method.want_grayscale_image and (img_obj['gray'] is
                                                             None):
                    self.generate_grayscale_image(img_obj)

                img_fg = self._fg_method(img_bgr=img_obj['bgr'],
                                         img_gray=img_obj['gray'])
                img_obj['fg'] = self._kernel.encode(img_fg)
            fg_pixels = self._kernel.logical_or_popcnt(img_obj['fg'])

            # fixme: zero division
            fg_ratio = fg_pixels / (self._width * self._height)
            fg_matched = (fg_ratio > self._threshold)

        if debug:
            label = self._label
            print(
                "%s: result=%s raito BG %s FG %s (threshold BG %1.3f FG %1.3f) label:%s"
                % (self.__class__.__name__, fg_matched, bg_ratio, fg_ratio,
                   self._orig_threshold, self._threshold, label))
        # ToDo: imshow

        # ToDo: on_mark_rect_in_preview

        return (fg_matched, fg_ratio, bg_ratio)
Example #2
0
    def match_score_internal(self, img_obj, debug=None):
        debug = debug or self._debug

        bg_pixels = 0
        bg_ratio = 0.0
        bg_matched = False

        fg_pixels = 0
        fg_ratio = 0.0
        fg_matched = False

        # Phase 2: Background check
        try:
            if img_obj['bg'] is None:
                if self._bg_method.want_grayscale_image and (img_obj['gray'] is None):
                    self.generate_grayscale_image(img_obj)

                img_bg = 255 - \
                    self._bg_method(img_bgr=img_obj['bgr'], img_gray=img_obj['gray'])
                img_obj['bg'] = self._kernel.encode(img_bg)
            bg_pixels = self._kernel.logical_and_popcnt(img_obj['bg'])

            # fixme: zero division
            bg_ratio = bg_pixels / (self._width * self._height)
            bg_matched = (bg_ratio <= self._orig_threshold)
        except:
            IkaUtils.dprint('%s (%s): bg_method %s caused a exception.' % (
                self, self._label, self._bg_method.__class__.__name__))
            IkaUtils.dprint(traceback.format_exc())

        # Phase 3: Foreground check
        if bg_matched:
            if img_obj['fg'] is None:
                if self._fg_method.want_grayscale_image and (img_obj['gray'] is None):
                    self.generate_grayscale_image(img_obj)

                img_fg = self._fg_method(img_bgr=img_obj['bgr'], img_gray=img_obj['gray'])
                img_obj['fg'] = self._kernel.encode(img_fg)
            fg_pixels = self._kernel.logical_or_popcnt(img_obj['fg'])

            # fixme: zero division
            fg_ratio = fg_pixels / (self._width * self._height)
            fg_matched = (fg_ratio > self._threshold)

        if debug:
            label = self._label
            print("%s: result=%s raito BG %s FG %s (threshold BG %1.3f FG %1.3f) label:%s" %
                  (self.__class__.__name__, fg_matched, bg_ratio, fg_ratio,
                   self._orig_threshold, self._threshold, label))
        # ToDo: imshow

        # ToDo: on_mark_rect_in_preview

        return (fg_matched, fg_ratio, bg_ratio)
Example #3
0
    def __init__(self, left, top, width, height, img=None, img_file=None, threshold=0.9, fg_method=None, bg_method=None, orig_threshold=0.7, debug=False, label=None, call_plugins=None, kernel_class=None):
        """
        Constructor

        Args:
            self                 The object.
            left                 Left of the mask.
            top                  Top of the mask.
            width                Width of the mask.
            height               Height of the mask.
            img                  Instance of the mask image.
            img_file             Filename of the mask image.
            threshold            Threshold
            orig_threshold       Target frame must be lower than this raito.
            pre_threshold_value  Threshold target frame with this level before matching.
            debug                If true, show debug information.
            label                Label (text data) to distingish this mask.
        """
        self._top = top
        self._left = left
        self._width = width
        self._height = height
        self._threshold = threshold
        self._orig_threshold = orig_threshold
        self._debug = debug
        self._label = label
        self._call_plugins = call_plugins

        self._fg_method = fg_method or MM_WHITE()
        self._bg_method = bg_method or MM_NOT_WHITE()
        if not img_file is None:
            img_file2 = find_image_file(img_file)
            img = cv2.imread(img_file2)  # FIXME: use own imread

            if img is None:
                IkaUtils.dprint(
                    '%s is not available. Retrying with %s' % (img_file2, img_file))
                img = cv2.imread(img_file)  # FIXME

        if img is None:
            raise Exception('Could not load mask image %s (%s)' %
                            (label, img_file))

        if len(img.shape) > 2 and img.shape[2] != 1:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        if not self._is_cropped(img):
            img = img[top: top + height, left: left + width]

        # Initialize kernel
        kernel_class = kernel_class or default_kernel
        self._kernel = kernel_class(self._width, self._height)
        self._kernel.load_mask(img)
Example #4
0
def load_kernel():
    global default_kernel

    if platform.machine().startswith('armv7'):
        from ikalog.utils.ikamatcher2.arm_neon import NEON
        default_kernel = NEON

    else:
        from ikalog.utils.ikamatcher2.reference import Numpy_uint8_fast
        default_kernel = Numpy_uint8_fast

    IkaUtils.dprint('%s: using kernel %s' % (IkaMatcher2, default_kernel.__name__))
Example #5
0
def print_tsv(summary, tsv_format):
    summary = summary.copy()
    summary['end_at_text'] = time.strftime('%Y-%m-%d %H:%M:%S',
                                           time.localtime(summary['end_at']))
    summary['lobby_text'] = IkaUtils.lobby2text(_get_lobby_id(summary['lobby']),
                                                unknown='')
    summary['rule_text'] = IkaUtils.rule2text(summary['rule'], unknown='')
    summary['map_text'] = IkaUtils.map2text(summary['map'], unknown='')
    summary['weapon_text'] = IkaUtils.weapon2text(summary['weapon'], unknown='')

    keys = tsv_format.split(',')
    values = []
    for key in keys:
        values.append(str(summary[key]))
    print('\t'.join(values))
Example #6
0
def load_kernel():
    global default_kernel

    if platform.machine().startswith('armv7'):
        try:
            from lib.ikamatcher2_kernel_hal import HAL
            default_kernel = HAL
        except:
            from ikalog.utils.ikamatcher2.arm_neon import NEON
            default_kernel = NEON

    else:
        from ikalog.utils.ikamatcher2.reference import Numpy_uint8_fast
        default_kernel = Numpy_uint8_fast

    IkaUtils.dprint('%s: using kernel %s' %
                    (IkaMatcher2, default_kernel.__name__))
Example #7
0
def find_image_file(img_file=None, languages=None):
    if languages is None:
        languages = Localization.get_game_languages()

    if languages is not None:
        if not isinstance(languages, list):
            languages = [lang]

        for lang in languages:
            f = IkaUtils.get_path('masks', lang, img_file)
            if os.path.exists(f):
                return f

    f = IkaUtils.get_path('masks', img_file)
    if os.path.exists(f):
        return f

    f = IkaUtils.get_path(img_file)
    if os.path.exists(f):
        return f

    f = IkaUtils.get_path('masks', 'ja', img_file)
    if os.path.exists(f):
        IkaUtils.dprint('%s: mask %s: using ja version' %
            (self, img_file))
        return f

    raise Exception('Could not find image file %s (lang %s)' % (img_file, lang))
Example #8
0
    def __init__(self,
                 left,
                 top,
                 width,
                 height,
                 img=None,
                 img_file=None,
                 threshold=0.9,
                 fg_method=None,
                 bg_method=None,
                 orig_threshold=0.7,
                 debug=False,
                 label=None,
                 call_plugins=None,
                 kernel_class=None):
        """
        Constructor

        Args:
            self                 The object.
            left                 Left of the mask.
            top                  Top of the mask.
            width                Width of the mask.
            height               Height of the mask.
            img                  Instance of the mask image.
            img_file             Filename of the mask image.
            threshold            Threshold
            orig_threshold       Target frame must be lower than this raito.
            pre_threshold_value  Threshold target frame with this level before matching.
            debug                If true, show debug information.
            label                Label (text data) to distingish this mask.
        """
        self._top = top
        self._left = left
        self._width = width
        self._height = height
        self._threshold = threshold
        self._orig_threshold = orig_threshold
        self._debug = debug
        self._label = label
        self._call_plugins = call_plugins

        self._fg_method = fg_method or MM_WHITE()
        self._bg_method = bg_method or MM_NOT_WHITE()
        if not img_file is None:
            img_file2 = find_image_file(img_file)
            img = cv2.imread(img_file2)  # FIXME: use own imread

            if img is None:
                IkaUtils.dprint('%s is not available. Retrying with %s' %
                                (img_file2, img_file))
                img = cv2.imread(img_file)  # FIXME

        if img is None:
            raise Exception('Could not load mask image %s (%s)' %
                            (label, img_file))

        if len(img.shape) > 2 and img.shape[2] != 1:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        if not self._is_cropped(img):
            img = img[top:top + height, left:left + width]

        # Initialize kernel
        kernel_class = kernel_class or default_kernel
        self._kernel = kernel_class(self._width, self._height)
        self._kernel.load_mask(img)