Пример #1
0
    def _match_auto(self, screen, search_img, threshold):
        """Maybe not a good idea
        """
        # 1. try template first
        ret = ac.find_template(screen, search_img)
        print "template/conficence:", ret
        if ret and ret['confidence'] > threshold:
            return FindPoint(ret['result'],
                             ret['confidence'],
                             'template',
                             matched=True)

        # 2. try sift
        ret = ac.find_sift(screen, search_img, min_match_count=10)
        print "sift/conficence:", ret
        if ret is None:
            return None

        matches, total = ret['confidence']
        print "other/conficence:", matches, total
        if 1.0 * matches / total > 0.5:  # FIXME(ssx): sift just write here
            return FindPoint(ret['result'],
                             ret['confidence'],
                             'sift',
                             matched=True)
        return None
Пример #2
0
def click(self, imgsrc, imgobj):
    '''
    @Time    : 2018/9/17
    @Author  : 诸葛小心
    @File    : Locate_Image_Click.py

    instruction:
    基于aircv图像识别,传入待查找的图片、原图,则定位到带查找图片在原图上的坐标点

    usage:
    imgsr = 'imgsr.png'
    imgobj = 'imgobj.png'
    self.Locate_Image_Click.click(imgsrc, imgobj)
    '''

    imsrc = ac.imread(imgsrc)  # 原始图像
    imsch = ac.imread(imgobj)  # 待查找的部分
    position = ac.find_sift(imsrc, imsch)

    # print(position)

    x, y = position['result']

    print("x = ", x)
    print("y = ", y)

    self.driver.swipe(x, y, x, y, 50)
Пример #3
0
def sift_test():
    t1 = ac.imread("testdata/1s.png")
    t2 = ac.imread("testdata/2s.png")
    print ac.sift_count(t1), ac.sift_count(t2)

    result = ac.find_sift(t1, t2, min_match_count=ac.sift_count(t1)*0.4) # after many tests, 0.4 may be best
    if result:
        print 'Same'
    else:
        print 'Not same'
Пример #4
0
 def get_money_pos(pic):
     im = ac.imread(pic)
     im2 = ac.imread('standard/money.png')
     try:
         match_result = ac.find_sift(im, im2)
         if match_result and match_result['result'][0] > 0 and match_result[
                 'confidence'][1] > 10:
             return match_result['result']
     except Exception as e:
         print('get_money_pos exception', e)
         return
Пример #5
0
def sift_test():
    t1 = ac.imread("tests/testdata/1s.png")
    t2 = ac.imread("tests/testdata/2s.png")
    print((ac.sift_count(t1), ac.sift_count(t2)))

    result = ac.find_sift(t1, t2, min_match_count=ac.sift_count(t1) *
                          0.4)  # after many tests, 0.4 may be best
    if result:
        print('Same')
    else:
        print('Not same')
Пример #6
0
def findImage():
    imgfound = [None, None, None, None]
    # imsrc = ac.imread(self.d.screenshot(os.getcwd() + "/tmp.png"))
    # src = self.minicap_ins.crop_image()
    # src.save(os.getcwd() + "/tmp.png")
    imgsrc = ac.imread("tmp.png")
    imobj = ac.imread('w1.720x1512_480x960.png')
    rt = ac.find_template(imgsrc, imobj)
    print "rt:", rt
    rts = ac.find_sift(imgsrc, imobj)
    print "snift:", rts
Пример #7
0
    def _match_auto(self, screen, search_img, threshold):
        """Maybe not a good idea
        """
        # 1. try template first
        ret = ac.find_template(screen, search_img)
        if ret and ret['confidence'] > threshold:
            return FindPoint(ret['result'], ret['confidence'], consts.IMAGE_MATCH_METHOD_TMPL, matched=True)

        # 2. try sift
        ret = ac.find_sift(screen, search_img, min_match_count=10)
        if ret is None:
            return None

        matches, total = ret['confidence']
        if 1.0*matches/total > 0.5: # FIXME(ssx): sift just write here
            return FindPoint(ret['result'], ret['confidence'], consts.IMAGE_MATCH_METHOD_SIFT, matched=True)
        return None
Пример #8
0
    def match(self, pattern, screen=None, threshold=None):
        """Check if image position in screen

        Args:
            - pattern: Image file name or opencv image object
            - screen: opencv image, optional, if not None, screenshot method will be called

        Returns:
            None or FindPoint, For example:

            FindPoint(pos=(20, 30), method='tmpl', confidence=0.801, matched=True)

            Only when confidence > self.image_match_threshold, matched will be True

        Raises:
            TypeError: when image_match_method is invalid
        """
        pattern = self.pattern_open(pattern)
        search_img = pattern.image

        pattern_scale = self._cal_scale(pattern)
        if pattern_scale != 1.0:
            search_img = cv2.resize(search_img, (0, 0), 
                fx=pattern_scale, fy=pattern_scale,
                interpolation=cv2.INTER_CUBIC)
        
        screen = screen or self.region_screenshot()
        threshold = threshold or self.image_match_threshold

        dx, dy = pattern.offset
        dx, dy = int(dx*pattern_scale), int(dy*pattern_scale)

        # image match
        screen = imutils.from_pillow(screen) # convert to opencv image
        match_method = self.image_match_method
        ret = None
        if match_method == consts.IMAGE_MATCH_METHOD_TMPL:
            ret = ac.find_template(screen, search_img)
        elif match_method == consts.IMAGE_MATCH_METHOD_SIFT:
            ret = ac.find_sift(screen, search_img, min_match_count=10)
        else:
            raise TypeError("Invalid image match method: %s" %(match_method,))

        if ret is None:
            return None
        (x, y) = ret['result']
        # fix by offset
        position = (x+dx, y+dy)
        if self.bounds:
            x, y = position
            position = (x+self.bounds.left, y+self.bounds.top)
        confidence = ret['confidence']

        matched = True
        if match_method == consts.IMAGE_MATCH_METHOD_TMPL:
            if confidence < threshold:
                matched = False
        elif match_method == consts.IMAGE_MATCH_METHOD_SIFT:
            matches, total = confidence
            if 1.0*matches/total > 0.5: # FIXME(ssx): sift just write here
                matched = True
        return FindPoint(position, confidence, match_method, matched=matched)
Пример #9
0
    def match(self, pattern, screen=None, rect=None, offset=None, threshold=None, method=None):
        """Check if image position in screen

        Args:
            - pattern: Image file name or opencv image object
            - screen (PIL.Image): optional, if not None, screenshot method will be called
            - threshold (float): it depends on the image match method
            - method (string): choices on <template | sift>

        Returns:
            None or FindPoint, For example:

            FindPoint(pos=(20, 30), method='tmpl', confidence=0.801, matched=True)

            Only when confidence > self.image_match_threshold, matched will be True

        Raises:
            TypeError: when image_match_method is invalid
        """
        pattern = self.pattern_open(pattern)
        search_img = pattern.image

        pattern_scale = self._cal_scale(pattern)
        if pattern_scale != 1.0:
            search_img = cv2.resize(search_img, (0, 0), 
                fx=pattern_scale, fy=pattern_scale,
                interpolation=cv2.INTER_CUBIC)
        
        screen = screen or self.region_screenshot()
        threshold = threshold or pattern.threshold or self.image_match_threshold

        # handle offset if percent, ex (0.2, 0.8)
        dx, dy = offset or pattern.offset or (0, 0)
        dx = pattern.image.shape[1] * dx # opencv object width
        dy = pattern.image.shape[0] * dy # opencv object height
        dx, dy = int(dx*pattern_scale), int(dy*pattern_scale)

        # image match
        screen = imutils.from_pillow(screen) # convert to opencv image
        if rect and isinstance(rect, tuple) and len(rect) == 4:
            (x0, y0, x1, y1) = [int(v*pattern_scale) for v in rect]
            (dx, dy) = dx+x0, dy+y0
            screen = imutils.crop(screen, x0, y0, x1, y1)
            #cv2.imwrite('cc.png', screen)

        match_method = method or self.image_match_method
        
        ret = None
        confidence = None
        matched = False
        position = None
        if match_method == consts.IMAGE_MATCH_METHOD_TMPL: #IMG_METHOD_TMPL
            ret = ac.find_template(screen, search_img)
            if ret is None:
                return None
            confidence = ret['confidence']
            if confidence > threshold:
                matched = True
            (x, y) = ret['result']
            position = (x+dx, y+dy) # fix by offset
        elif match_method == consts.IMAGE_MATCH_METHOD_SIFT:
            ret = ac.find_sift(screen, search_img, min_match_count=10)
            if ret is None:
                return None
            confidence = ret['confidence']
            matches, total = confidence
            if 1.0*matches/total > 0.5: # FIXME(ssx): sift just write here
                matched = True
            (x, y) = ret['result']
            position = (x+dx, y+dy) # fix by offset
        elif match_method == consts.IMAGE_MATCH_METHOD_AUTO:
            fp = self._match_auto(screen, search_img, threshold)
            if fp is None:
                return None
            (x, y) = fp.pos
            position = (x+dx, y+dy)
            return FindPoint(position, fp.confidence, fp.method, fp.matched)
        else:
            raise TypeError("Invalid image match method: %s" %(match_method,))

        (x, y) = ret['result']
        position = (x+dx, y+dy) # fix by offset
        if self.bounds:
            x, y = position
            position = (x+self.bounds.left, y+self.bounds.top)

        return FindPoint(position, confidence, match_method, matched=matched)
Пример #10
0
import aircv as ac
import cv2
import matplotlib.pyplot as plt

imsrc = ac.imread('test_images/img_0001_new.png')
imsch = ac.imread('test_images/button.png')

result = ac.find_sift(imsrc, imsch)
result_positon = result['rectangle']
src_img = cv2.imread('test_images/img_0001_new.png', cv2.COLOR_RGB2BGR)
cv2.line(src_img, result_positon[0], result_positon[1], (255, 0, 0), 5)
cv2.line(src_img, result_positon[1], result_positon[2], (255, 0, 0), 5)
cv2.line(src_img, result_positon[2], result_positon[3], (255, 0, 0), 5)
cv2.line(src_img, result_positon[3], result_positon[0], (255, 0, 0), 5)
plt.imshow(src_img)
plt.show()
Пример #11
0
def main():
    bg = aircv.imread('testdata/bg.png')
    search = aircv.imread('testdata/guard.png')
    
    print aircv.find_sift(bg, search)
Пример #12
0
def main():
    bg = aircv.imread('testdata/bg.png')
    search = aircv.imread('testdata/guard.png')

    print aircv.find_sift(bg, search)
Пример #13
0
def get_image_cv_pos(img_res_path=None,
                     image=None,
                     pos_curson="Center",
                     pos_offsetY=0,
                     pos_offsetX=0,
                     confidence_df=0.85,
                     waitfor=WAIT_FOR_IMG):
    '''
    截屏并根据图片计算其位置
    '''
    __logger.debug(r'ready OpenCV Recognition operation')
    pos = None
    try:
        starttime = time.time()
        image = img_res_path + '\\res\\' + image
        while True:
            try:
                pic = TEMP_PATH + 'ScreenCapture.png'
                screen_capture(pic)
                imsrc = ac.imread(pic)  # 原始图像
                imsch = ac.imread(image)
                #                 imsch = ac.imread('d:/1.png') # 带查找的部分
                #                 print(str(ac.sift_count(r'd:\1.png')))
                sift_json = ac.find_sift(imsrc, imsch)
                if sift_json == None:
                    print(
                        u'sift_json return null,OpenCV cannot recognize picture'
                    )
                else:
                    sift_dict = json.loads(json.dumps(sift_json))
                    __logger.warning(
                        u'OpenCV Identifying return information  :' +
                        str(sift_dict))
                    #误取信心度
                    confidence = get_cv_confidence_percent(
                        sift_dict['confidence'])
                    if confidence >= confidence_df:
                        #根据相关信息计算真正位置
                        pos = cal_position(sift_dict['result'],
                                           sift_dict['rectangle'], pos_curson,
                                           pos_offsetY, pos_offsetX)
                        if pos[0] < 0 or pos[1] < 0:
                            __logger.debug(
                                u'Location information is negative, error')
                            pos = None
                            continue
                        __logger.warning(u'OpenCV confidence :' +
                                         str(confidence))
                        break
                    else:
                        __logger.error(u'OpenCV confidence too low:' +
                                       str(confidence))
            except Exception as ex:
                print(str(ex), traceback.format_exc())
                pass

            runtime = time.time() - starttime
            if runtime >= waitfor:
                break
            __logger.debug(
                r'OpenCV Recognize failure and wait for the next time ')
            time.sleep(TRY_INTERVAL)
    except Exception as e:
        print(str(e), traceback.format_exc())
    finally:
        return pos
Пример #14
0
import aircv as ac

imsrc = ac.imread('tests/testdata/g18/screen_big.png')  # 原始图像
imsch = ac.imread('tests/testdata/g18/task.png')  # 带查找的部分
print((ac.find_sift(imsrc, imsch)))
Пример #15
0
def find_sift(source, target):
    result = ac.find_sift(source, target)
    print(1, 'SIFT匹配结果:')
    print(result)
Пример #16
0
    def match(self, pattern, screen=None, threshold=None):
        """Check if image position in screen

        Args:
            - pattern: Image file name or opencv image object
            - screen: opencv image, optional, if not None, screenshot method will be called

        Returns:
            None or FindPoint, For example:

            FindPoint(pos=(20, 30), method='tmpl', confidence=0.801, matched=True)

            Only when confidence > self.image_match_threshold, matched will be True

        Raises:
            TypeError: when image_match_method is invalid
        """
        pattern = self.pattern_open(pattern)
        search_img = pattern.image

        pattern_scale = self._cal_scale(pattern)
        if pattern_scale != 1.0:
            search_img = cv2.resize(search_img, (0, 0),
                                    fx=pattern_scale,
                                    fy=pattern_scale,
                                    interpolation=cv2.INTER_CUBIC)

        screen = screen or self.region_screenshot()
        threshold = threshold or self.image_match_threshold

        dx, dy = pattern.offset
        dx, dy = int(dx * pattern_scale), int(dy * pattern_scale)

        # image match
        screen = imutils.from_pillow(screen)  # convert to opencv image
        match_method = self.image_match_method
        ret = None
        if match_method == consts.IMAGE_MATCH_METHOD_TMPL:
            ret = ac.find_template(screen, search_img)
        elif match_method == consts.IMAGE_MATCH_METHOD_SIFT:
            ret = ac.find_sift(screen, search_img, min_match_count=10)
        else:
            raise TypeError("Invalid image match method: %s" %
                            (match_method, ))

        if ret is None:
            return None
        (x, y) = ret['result']
        # fix by offset
        position = (x + dx, y + dy)
        if self.bounds:
            x, y = position
            position = (x + self.bounds.left, y + self.bounds.top)
        confidence = ret['confidence']

        matched = True
        if match_method == consts.IMAGE_MATCH_METHOD_TMPL:
            if confidence < threshold:
                matched = False
        elif match_method == consts.IMAGE_MATCH_METHOD_SIFT:
            matches, total = confidence
            if 1.0 * matches / total > 0.5:  # FIXME(ssx): sift just write here
                matched = True
        return FindPoint(position, confidence, match_method, matched=matched)