def _parse_image_in(self, parent_image, sub_image, phase_type='click'): print 'Here is sub search img' self._prepare() if self.img_path: parent_image = os.path.join(self.img_path, parent_image) sub_image = os.path.join(self.img_path, sub_image) else: self._info("[>>>] img path not set") im_source = ac.imread(self._screen.decode('utf-8').encode('gbk')) im_parent = ac.imread(parent_image.decode('utf-8').encode('gbk')) im_sub = ac.imread(sub_image.decode('utf-8').encode('gbk')) intermediate = ac.find_template(im_source, im_parent, self.TH) in_rect = intermediate['rectangle'] result = ac.find_all_template(im_source, im_sub, self.TH) for i in range(0, len(result), 1): result_rect = result[i]['rectangle'] # only cmp left-top && right-down 's coordinate # rectangle[0~1]: left-top,left-down # rectangle[2~3]: right-top,right-down if self._coordinate_cmp(result_rect[0], in_rect[0]): # left-top if self._coordinate_cmp(in_rect[3], result_rect[3]): # right-down try: if 'click' in phase_type: self.click_a_point(result[i]['result'][0], result[i]['result'][1]) elif 'coordinate' in phase_type: return result[i]['result'][0], result[i]['result'][1] except Exception, e: print '[xxx]: %s ' % traceback.format_exc()
def mobile_screen_should_contain(self, target): """assert current screen should contain target image """ self._prepare() im_source = ac.imread(self._screen) im_search = ac.imread(target) re = ac.find_template(im_source, im_search, self.TH) if re: return True return False
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
def _imfind(self, bgimg, search): method = self._image_match_method print 'match-method:', method imsrc, imsch = ac.imread(bgimg), ac.imread(search) if method == 'auto': point = ac.find(imsrc, imsch) elif method == 'template': res = ac.find_template(imsrc, imsch, self._threshold) if res: point, score = res print 'match result:', point, score return point return None elif method == 'sift': point = imtsift.find(search, bgimg) else: raise RuntimeError("Unknown image match method: %s" %(method)) return point
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
def get_coordinate_by_image_identify(source, template, get_rect=False): """获取目标图片在原图片中的坐标 :param source: :param template: :param get_rect: :return: get_rect=True """ try: imsrc = ac.imread(template) # 原始图像 imsch = ac.imread(source) # 待查找的部分 position = ac.find_template(imsrc, imsch) if position is not None: x, y = position['result'] rect = position['rectangle'] else: raise Exception("Cannot find the image that you provided.") if get_rect: return x, y, rect else: return x, y except Exception as msg: raise Exception(msg)
def pic_locate(pic_match, pic_origin, thresh, findall=True, rgb_bool=True ): #pic_match is the dir path, pic_origin is the data array """ :param pic_match: 源图像路径或图像数组 :param pic_origin: 背景图像,ndarray :param thresh: 阈值,数值 :param findall: true 为寻找全部匹配图像,false为只返回一个 :param rgb_bool: true为匹配颜色,false为不匹配颜色 :return: """ if (isinstance(pic_match, str)): #若为路径,则根据当前分辨率动态调整实际对比图像 pic_test = Image.open(pic_match, 'r') resolution = globalvar.get_window_resolution() max_resolution = globalvar.get_max_resolution() width = int(resolution[0] / max_resolution[0] * pic_test.size[0]) height = int(resolution[1] / max_resolution[1] * pic_test.size[1]) pic_test = np.array(pic_test.resize((width, height), Image.ANTIALIAS)) elif (isinstance(pic_match, np.ndarray)): pic_test = pic_match if findall: position = aircv.find_all_template(pic_origin, pic_test, thresh, rgb=rgb_bool) else: position = aircv.find_template(pic_origin, pic_test, thresh, rgb=rgb_bool) # C = np.fft.ifft2(np.fft.fft2(pic_origin)*fftpack.fft2(pic_match_path,(888,1435,3))) return position
def search_img(imgsrc, imgobj, confidencevalue=0.7, rgb=True): # imgsrc=原始图像,imgobj=待查找的图片 try: import aircv as ac print(imgsrc, imgobj) imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) except Exception as e: print(e) match_result = {'result': (-1, -1)} return match_result['result'][0], match_result['result'][1] match_result = ac.find_template( imsrc, imobj, confidencevalue, rgb ) # {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)} if match_result is not None: match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽 else: match_result = {'result': (-1, -1)} print(match_result) return match_result['result'][0], match_result['result'][1]
def MathPIC(Fpic,Cpic,confidence=0.95): """ 对比图片,默认相似度95%,则认为是同一图片 :param Fpic:需要查找的图片 :param Cpic:目标图片 :param confidence:相似度 :return: 找到的坐标点,和匹配后相似度 """ im_fpic = ac.imread(Fpic) im_cpic = ac.imread(Cpic) match_result = ac.find_template(im_fpic,im_cpic,confidence) if match_result and 'confidence' in match_result and match_result["confidence"]>=confidence: start= list(match_result["rectangle"][0]) end = list(match_result["rectangle"][-1]) # 展示匹配区域 # tmp = cv2.rectangle(im_fpic, match_result["rectangle"][0], match_result["rectangle"][-1], (0, 0, 225), 2) # cv2.imshow("",tmp) # cv2.waitKey() # cv2.destroyAllWindows() start.extend(end) return [*start,match_result["confidence"]] return "ERROR: 没有匹配到图片标志"
def process_cut(start_index=0): order_anchor_image = ac.imread('order_anchor.png') src_image = ac.imread('pic.png') # 标准截图左上角应该是(668, X) # 最左边的Quick卡是这个截图位置加一个截距(34, 72) # 一张卡的大小是121 × 156 # 两张卡的距离为178 pos = ac.find_template(src_image, order_anchor_image) pos_x, pos_y = pos['rectangle'][0] delta_x = 34 delta_y = 72 card_w = 121 card_h = 156 delta_card = 178 for i in range(5): crop_image = src_image[pos_y+delta_y:pos_y+delta_y+card_h, pos_x+delta_x+delta_card*i:pos_x+delta_x+delta_card*i+card_w] cv2.imwrite("pic\\card%d.png" % (i + start_index), crop_image) print('已保存指令卡截图……')
def changeaccount(self): time.sleep(2) try: automation.SendKeys('{Ctrl}k{Ctrl}k') time.sleep(2) automation.SendKeys( 'http://yct.sh.gov.cn/portal_yct/webportal/handle_progress.do?x=12{Enter}' ) except Exception as e: print(e) else: time.sleep(5) pyautogui.screenshot(IMGSRC) imgobj = file + r'\tc.jpg' imsrc = ac.imread(IMGSRC) imobj = ac.imread(imgobj) match_result = ac.find_template(imsrc, imobj, 0.8) if match_result: automation.HyperlinkControl(Depth=12, Name='退出').Click() time.sleep(5) REDIS_GZ.hset('specify_account_session', {'session': 'false'}) automation.HyperlinkControl(Depth=11, Name='账号密码登录').Click() else: self.restart_login = True
def matchImg(imgobj, imgsrc=None, confidencevalue=0.5):#imgsrc=原始图像,imgobj=待查找的图片 ''' 功能:在指定图片或当前桌面上定位目标图片所在位置 输入: imgobj:必须,需定位目的图片路径。 imgsrc:可选,默认为None。源图片路径,若为空,默认为当前桌面截屏 返回: 图片在给定图片中的位置以及可靠率 ''' imobj = ac.imread(imgobj) if not imgsrc: current_abs_path = os.path.split(os.path.realpath(__file__))[0] file_name = os.path.join(current_abs_path, 'total.png') im = ImageGrab.grab() im.save(file_name,'png') imsrc = ac.imread(file_name) else: imsrc = ac.imread(imgsrc) match_result = ac.find_template(imsrc,imobj,confidencevalue) # {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)} if match_result is not None: match_result['shape']=(imsrc.shape[1],imsrc.shape[0])#0为高,1为宽 return match_result
# # code = pytesseract.image_to_string(image) # print code # print circle_center_pos def draw_circle(img, p1, p2, color, line_width): #cv2.circle(img, pos, circle_radius, color, line_width) cv2.rectangle(img, p1, p2, color, line_width) cv2.imshow('objDetect', img) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == "__main__": im = cv2.imread(path + "3.png") obj = cv2.imread(path + "obj.png") pos = ac.find_template(im, obj) ran = pos["rectangle"] objrex = ran[2][0] - ran[0][0] objrey = ran[1][1] - ran[0][1] re = im[ran[0][1] + int(1.0 / 2.0 * objrey):ran[3][1], ran[0][0] + int(1.0 / 4.0 * objrex):ran[3][0] - int(1.0 / 2.0 * objrex)] cv2.imwrite('./aa.png', re) ima = cv2.imread('./aa.png') yx = ima.shape[1] yy = ima.shape[0] res = cv2.resize(ima, (yx * 2, yy * 2), interpolation=cv2.INTER_CUBIC) cv2.imwrite('./aa1.png', res) image = Image.open('./aa1.png') # code = pytesseract.image_to_string(image) print code
import cv2 import aircv import numpy partImg = aircv.imread("D:/part.png") allImg = aircv.imread("D:/all.png") match_result = aircv.find_template(allImg, partImg, 0.6) print(match_result) data = match_result["rectangle"] points = numpy.array([data[0], data[1], data[3], data[2]], numpy.int) cv2.polylines(allImg, [points], True, (0, 0, 255), 2) cv2.imwrite("D:/result.png", allImg) cv2.imshow("pic", allImg) key = cv2.waitKey(0) if 27 == key: print("ecs") cv2.destroyAllWindows()
# img = image.load_img('test2.png', target_size=(40, 65)) # img = np.array(img) src = 't1.png' sch = 't2.png' imsrc = ac.imread(src) # 原始图像 imsch = ac.imread(sch) # 带查找的部分 print(src, sch) res1 = ac.find_all_template(imsrc, imsch, threshold=0.5, maxcnt=0, rgb=False, bgremove=True) print(res1) res = ac.find_template(imsrc, imsch) print(res) # path = 'sliceImage/{num}/{imgUrl}'.format(num=num, imgUrl=imgUrl) # path = 'test.png' # path = 't1.png' # path = 'newSliceImage/113/0054c0fa-9191-11e9-be10-f018980779c1.png' # path = 'test.png' path = '/Users/zejun/Downloads/test1.png' print(path) imsrc = cv2.imread(path, 0) im = imsrc cv2.namedWindow("Python opencv") length = 0 # cv2.setMouseCallback("Python opencv", on_EVENT_LBUTTONDOWN, length)
def match(modeImage): srcImage = cut(savePicture=False) pos = ac.find_template(srcImage, modeImage) if pos is not None and pos['confidence'] > 0.9: return True return False
import cv2 import aircv as ac # print circle_center_pos def draw_circle(img, pos, circle_radius, color, line_width): cv2.circle(img, pos, circle_radius, color, line_width) cv2.imshow('objDetect', imsrc) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == "__main__": imsrc = ac.imread('test2.png') imobj = ac.imread('test1.png') # find the match position pos = ac.find_template(imsrc, imobj) circle_center_pos = pos['result'] circle_radius = 50 color = (0, 255, 0) line_width = 10 # draw circle draw_circle(imsrc, circle_center_pos, circle_radius, color, line_width)
def find_template(source, target): result = ac.find_template(source, target) print(3, '直接模版匹配结果:') print(result)
def filterImage(base): test = base + 'test.png' testBg = base + 'testBg.png' imsrc = cv2.imread(test, 0) # 原始图像 imsch = cv2.imread(testBg, 0) print(imsch.shape) height, width = imsch.shape print(height, width) top, bottom, left, right = 0, 100000, 10000, 0 for i in range(height): for j in range(width): if imsch[i][j] > 0: if top <= i: top = i if bottom >= i: bottom = i if left >= j: left = j if right <= j: right = j imsch1 = imsch[bottom:top, left:right] cv2.imwrite(base + 'testBg1.png', imsch1) imsch2 = ac.imread(base + 'testBg1.png') imsrc2 = ac.imread(test) res1 = ac.find_all_template(imsrc2, imsch2, threshold=0.5, maxcnt=0, rgb=False, bgremove=True) print(res1) res = None if (len(res1) > 0): res = res1[0] for final in res1: if res['confidence'] < final['confidence']: res = final else: return base + 'test.png' left = res['rectangle'][0][0] right = left + 50 print('res-->', res, len(res1), res['rectangle'][0][0]) print('1: ', res1) res = ac.find_template(imsrc2, imsch2) print('2: ', res) height1, width1 = imsrc.shape # left = res['rectangle'][0][0] # right = left + 50 print(height1, width1) for i in range(height1): for j in range(width1): if i <= top and i >= bottom and j >= left and j <= right: # print('ixj: ', i, ' ', j) pass else: imsrc[i][j] = 0 print(top, bottom, left, right) cv2.imwrite(base + 'test1.png', imsrc) return (base + 'test1.png', left)
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)
def find_status_tansuo(statusNow, imsrc): status = -1 imobj = imobj_combat res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = status_combat print("---This is in battle t...") if status < 0: imobj = imobj_tansuo10 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = status_begin print("---There is a battle begin t...") if status < 0: imobj = imobj_tansuo13 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold - 0.1: status = status_ts_choose print("---There choose tansuo t3...") if status < 0: imobj = imobj_tansuo11 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold - 0.1: status = status_ts_choose print("---There choose tansuo t1...") if status < 0: imobj = imobj_tansuo12 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold - 0.1: status = status_ts_choose print("---There choose tansuo t2...") if status < 0: imobj = imobj_tansuo31 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = status_end2 print("---This is in end t1...") if status < 0: imobj = imobj_yuhun32 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = status_end2 print("---This is in end t2...") # 从结果中读取坐标 if status >= 0: statusNow = status pos = res['result'] # 在目标范围内做一次随机取点 w, h, r = imobj.shape posaim = (int(posbase[0] + pos[0] + w * (random.random() - 0.5) * 0.3), int(posbase[1] + pos[1] + h * (random.random() - 0.5) * 0.3)) print(posaim, res['confidence']) else: posaim = (int(posbase[0]), int(posbase[1])) # 如果是第二次进入choose则可执行点击 if statusNow == status_ts_choose: status = status_choose return status, posaim
def match(target, ignore_err=False): global ROOT_DIR # global threshold if '-' in target: target_path = target.split("-")[0] target_name = target.split("-")[1] appname = ROOT_DIR + '/testimage/' + target_path + '/' + target_name + '.png' appname_2 = ROOT_DIR + '/testimage/' + target_path + '/' + target_name + '_2.png' appname_3 = ROOT_DIR + '/testimage/' + target_path + '/' + target_name + '_3.png' else: appname = ROOT_DIR + '/testimage/' + target + '.png' appname_2 = ROOT_DIR + '/testimage/' + target + '_2.png' appname_3 = ROOT_DIR + '/testimage/' + target + '_3.png' ##根据桌面路径进行截屏 ##获取当前桌面路径 path = os.path.join(os.path.expanduser('~'), "Desktop") hwnd = win32gui.FindWindow(None, path) app = QApplication(sys.argv) screen = QApplication.primaryScreen() img = screen.grabWindow(hwnd).toImage() # img = ImageGrab.grab() ##保存截屏 root_desk = ROOT_DIR + '/desktop.png' img.save(root_desk) ##等待图片保存 time.sleep(0.5) # 支持图片名为中英文名,也支持路径中英文 imsrc = cv2.imdecode(np.fromfile(root_desk, dtype=np.uint8), -1) imobj = cv2.imdecode(np.fromfile(appname, dtype=np.uint8), -1) # 匹配图标位置 pos = ac.find_template(imsrc, imobj, threshold) if pos == None and os.path.exists(appname_2): print(u"第一张图标没匹配到,2秒后匹配第二张:") time.sleep(2) print(appname_2) imobj_2 = cv2.imdecode(np.fromfile(appname_2, dtype=np.uint8), -1) pos = ac.find_template(imsrc, imobj_2, threshold) if pos == None and os.path.exists(appname_3): print(u"第二张图标没匹配到,2秒后匹配第三张:") time.sleep(2) print(appname_3) imobj_3 = cv2.imdecode(np.fromfile(appname_3, dtype=np.uint8), -1) pos = ac.find_template(imsrc, imobj_3, threshold) # 如果第三张还未匹配到,用另一种方法重新截图 if pos == None: print(u"2秒后重新截全屏...") time.sleep(2) ##保存截屏 root_desk = ROOT_DIR + '/desktop_2.png' img = ImageGrab.grab() img.save(root_desk) ##等待图片保存 time.sleep(0.5) # 支持图片名为中英文名,也支持路径中英文 imsrc = cv2.imdecode(np.fromfile(root_desk, dtype=np.uint8), -1) imobj = cv2.imdecode(np.fromfile(appname, dtype=np.uint8), -1) # 匹配图标位置 pos = ac.find_template(imsrc, imobj, threshold) if pos == None and os.path.exists(appname_2): print(u"第一张图标没匹配到,2秒后匹配第二张:") time.sleep(2) print(appname_2) imobj_2 = cv2.imdecode(np.fromfile(appname_2, dtype=np.uint8), -1) # imobj_2 = ac.imread(appname_2) pos = ac.find_template(imsrc, imobj_2, threshold) if pos == None and os.path.exists(appname_3): print(u"第二张图标没匹配到,2秒后匹配第三张:") time.sleep(2) print(appname_3) imobj_3 = cv2.imdecode(np.fromfile(appname_3, dtype=np.uint8), -1) # imobj_3 = ac.imread(appname_3) pos = ac.find_template(imsrc, imobj_3, threshold) if pos == None: print(u"第二种截图方式也没有匹配到:" + target) if not ignore_err: raise Exception('匹配失败') elif pos != None: x, y = pos['result'] print(x, y, 'pos') pyautogui.moveTo(x=x, y=y, duration=0.5) print(u"匹配成功:{},坐标位置为:{}".format(target, pyautogui.position())) time.sleep(0.5) return x, y
def get_cookie_by_phantomjs(self): """ 获得cookie,需要selenium和phantomjs, 在建立webdriver时需要指定phantomjs的安装路径 """ result = {'result': False} params = webdriver.common.desired_capabilities.DesiredCapabilities.PHANTOMJS.copy( ) params['phantomjs.page.settings.userAgent'] = ( 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0' ) url = 'http://search.anccnet.com/searchResult2.aspx' # windows环境指定目录driver = webdriver.PhantomJS(executable_path='phantomjs.exe', desired_capabilities=params) driver = webdriver.PhantomJS(executable_path='phantomjs', desired_capabilities=params) # centos环境 driver.set_window_size(382, 415) driver.get(url) sleep(10) if driver.title == '401 - 未授权: 由于凭据无效,访问被拒绝。': print(driver.title) sys.exit() iframe = driver.find_element_by_id('captcha_widget') driver.switch_to.frame(iframe) btn = driver.find_element_by_class_name('captcha-widget-event') btn.click() sleep(5) driver.switch_to.default_content() # 获得验证码图片所在iframe, iframe.size 中保存框架的height, width iframe = driver.find_element_by_id('captcha_frame') driver.switch_to.frame(iframe) # 从iframe中获得验证图片,并转换成aircv对象 img = driver.find_element_by_id( 'lc-image-panel').find_element_by_class_name('captcha-list') imsrc = self.get_imsrc(img) # 从iframe中获得需要验证的点击步骤,可能会需要按顺序点击星形,圆点,方块 checklist = driver.find_element_by_css_selector( 'span#lc-captcha-word i').text checklist = checklist.split(',') for i in checklist: imobj = self.get_shape(i) # 在验证图片中查找验证点 pos = aircv.find_template(im_source=imsrc, im_search=imobj, threshold=0.7)['result'] # 点击验证点 webdriver.ActionChains(driver).move_to_element_with_offset( iframe, pos[0], pos[1]).click().perform() sleep(1) sleep(5) # 获得cookie tmp = driver.get_cookies() driver.quit() if tmp: cookie = '%s=%s' % (tmp[0]['name'], tmp[0]['value']) print('get cookie : %s ' % cookie) self.header['Cookie'] = cookie result['result'] = True result['cookie'] = cookie return result else: print('get cookie failed! check : %s' % ','.join(checklist)) return result
def compare_(objs): imobj = ac.imread(objs) imsrc = ac.imread('%s\\src.jpg' % os.getcwd()) pos = ac.find_template(imsrc, imobj, 0.5) return pos
def FindOnImg(srcImg, targetImg, th=0.7): imobj = ac.imread(targetImg) pos = ac.find_template(srcImg, imobj) if pos: if (pos['confidence'] > th): return pos['result']
#coding=utf-8 ''' Created on 2019/7/17 12:49:54 @author: tsfissure ''' import aircv imgSrc = aircv.imread("a.png") imgSch = aircv.imread("img\challenge.png") rlt = aircv.find_template(imgSrc, imgSch) print(rlt) if __name__ == '__main__': pass
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)
import numpy as np import cv2 import aircv as ac from appium import webdriver import time import unittest from selenium.webdriver.common import desired_capabilities img = cv2.imread('D:\\GitRex\\pic.png') x = type(img) print(x) y = img.shape print(y) #cv2.imshow('My Image', img) # open the picture #cv2.waitKey(0) #cv2.destroyAllWindows() img2 = cv2.imread('D:\\GitRex\\set.png') y2 = img.shape print(y2) pos = ac.find_template(img, img2) print(pos)
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)
cv2.circle(img, pos, circle_radius, color, line_width) cv2.imshow('objDetect', imsrc) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == "__main__": while True: # imsrc = ac.imread('C:\\Users\\SakataKin\\Desktop\\src.jpg') im = ImageGrab.grab() im.save('d:\\grabtmp.jpg') imsrc = ac.imread('d:\\grabtmp.jpg') imobj = ac.imread('C:\\Users\\SakataKin\\Desktop\\tag.png') # find the match position pos = ac.find_template(imsrc, imobj, rgb=True, bgremove=False) print(pos) if not (pos is None): circle_center_pos = tuple_f2i(pos['result']) print(circle_center_pos) circle_radius = 50 color = (0, 255, 0) line_width = 10 draw_circle(imsrc, circle_center_pos, circle_radius, color, line_width) time.sleep(3) else: print("Cannot find the target") time.sleep(3)
def find_status_tansuo(taskNow, tansuocnt, imsrc, posbase): #使用的临时状态量 status = -1 #检测是否处于战斗状态 imobj = imobj_combat res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_combat #检测是否在探索选择界面 if status < 0: imobj = imobj_tansuo0 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_explore #检测是否有开始按钮 if status < 0: imobj = imobj_tansuo11 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_begin #检测是否在选择界面 if status < 0: imobj = imobj_tansuo20 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_choose if status < 0: imobj = imobj_tansuo21 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_choose if status < 0: imobj = imobj_tansuo22 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_choose #检测是否有准备按钮 if status < 0: imobj = imobj_tansuo2R res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_ready #检查结算界面1 if status < 0: imobj = imobj_tansuo31 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_end if status < 0: imobj = imobj_tansuo32 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_end #检查是否在关卡结算界面 if status < 0: imobj = imobj_tansuo41 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold_H: status = tansuo_over1 if status < 0: imobj = imobj_tansuo42 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_over2 #如果无任何结果判断是否探索中 if status < 0: imobj = imobj_tansuo30 res = ac.find_template(imsrc, imobj) if res != None and res['confidence'] > confthreshold: status = tansuo_find # 从结果中读取坐标 if status >= 0: offset = res['result'] # 在目标范围内做一次随机取点 rect = imobj.shape taskNow, tansuocnt = click_action_tansuo(taskNow, tansuocnt, status, posbase, offset, rect[0], rect[1]) return taskNow, tansuocnt
def compareTwoImg(srcImg, objImg): imgSrc = ac.imread(srcImg) imgObj = ac.imread(objImg) return ac.find_template(imgSrc, imgObj)
def main(): imsrc = ac.imread(sys.argv[1]) imobj = ac.imread(sys.argv[2]) pos = ac.find_template(imsrc, imobj) print(pos['result'])
def 宝具(user, 手动选卡=False, 敌对目标=0, 选卡策略=[]): global remain_phase_number if 敌对目标 > 0: tap_key('234'[敌对目标 - 1], 普通操作时间) tap_key('\n', 进入选卡界面时间) index = 查找从者(user) # 识别5张牌都是啥 # 5张牌在标准化图片(992*611)上的位置: # (10+200X,300)-(190+200X,5320),X=0,1,2,3,4 if 自动选卡策略: src_image = cv2.resize(cut(save_picture=False), (992, 611)) found_card_list = [] for i in range(5): card_area_image = src_image[300:520, 10 + 200 * i:190 + 200 * i] found = '未定义-未定义' for servant_name in 阵容[:3]: if servant_name != '': for color in ['红', '蓝', '绿']: for test_image in 阵容卡组[servant_name][color]: pos = ac.find_template(card_area_image, test_image) # 这里有一个助战图标的问题 if pos is not None and pos['confidence'] > 0.8: found = '%s-%s' % (servant_name, color) break break break found_card_list.append(found) print('检测结果:') print(found_card_list) if not 手动选卡: result = [] if 自动选卡策略: for 策略 in 选卡策略: # 试图匹配策略 result = 选卡匹配(策略, found_card_list) if len(result) > 0: break if len(result) == 0: result = ['WER'[index], 'S', 'D'] tap_key(result[0], 普通操作时间) tap_key(result[1], 普通操作时间) if not 自动选卡策略: tap_key(result[2], treasure_time(user) + 换面时间) else: tap_key(result[2], 普通操作时间) remain_phase_number -= 1 print('剩余面数:%d' % remain_phase_number) wait_until_next_phase() if remain_phase_number <= 0: remain_phase_number = 3 else: tap_key('WER'[index], 普通操作时间) 等待操作() 等待(treasure_time(user) + 换面时间) if user == '大英雄': 阵容[index] = 阵容[3] 阵容[3] = 阵容[4] 阵容[4] = 阵容[5] 阵容[5] = ''
def draw_circle(img, pos, circle_radius, color, line_width): # print(img, pos, circle_radius, color, line_width) cv2.circle(img, pos, circle_radius, color, line_width) cv2.imshow('objDetect', imsrc) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == "__main__": imsrc = ac.imread('0-common_pics/bg.jpg') imobj = ac.imread('0-common_pics/obj.png') imsrc_, imobj_ = imsrc[:], imobj[:] pos = ac.find_template(imsrc_, imobj_) # find the match position # help( ac.find_template ) # print( pos ) # {'result': (793.0, 450.0), 'rectangle': ((744, 405), (744, 495), (842, 405), (842, 495)), 'confidence': 0.543538510799408} circle_center_pos = pos['result'] ''' TypeError: integer argument expected, got float 参数预期应该是 整型,结果得到浮点型 因为 circle_center_pos 是浮点型。所以要转整型为 new_int_circle_center_pos ''' new_int_circle_center_pos = [] for i in list(circle_center_pos): new_int_circle_center_pos.append(int(i)) print(tuple(new_int_circle_center_pos)) # (793, 450) circle_radius = 50 color = (0, 255, 0)