def FindFlgFromCap(self, flgPath, confidenceVTH, blockEn, findDelay): ''' @ 在图片中找出特征图案 @ @ return 特征图案中心坐标(x,y) or None @ @ param @ confidenceVTH:0.0-1.0 @ blockEn:是否阻塞等待flg出现 @ findDelay: 找到flg后延时findDelay秒后再返回 @ exception @ notice 必须像素点匹配 ''' #process = subprocess.getoutput('date +%m%d%H%M%S') #date = process.replace('\n','') date = '' #不保存时间戳 while True: screenshotPath = self.pullScreenShot('out/screencap' + date + '.png') # eg. out/cap1006120638.png print(screenshotPath) imsrc = ac.imread(screenshotPath) imdst = ac.imread(flgPath) pos = ac.find_template(imsrc, imdst) if ((pos != None) and (pos['confidence'] > confidenceVTH)): print('find a {para1}:{para2}'.format(para1=flgPath,para2=pos)) flgCenterPosInt = (int(pos['result'][0]), int(pos['result'][1])) time.sleep(findDelay) # draw_circle(imsrc, circleCenterPosInt) # draw circle #str(input('确定开始下一步?[y/n]:')) return flgCenterPosInt elif (blockEn == False): print('Do not find {para1}\n'.format(para1=flgPath)) return None else: print('wait for {para1}\n'.format(para1=flgPath)) time.sleep(1)
def check_circle(circle_path, threshold): print(print_name(circle_path)) i = 0 while i < 60: i += 1 screenshot.window_capture(const.Const.screen_path) try: mainScreen = ac.imread(const.Const.screen_path) circleScreen = ac.imread(circle_path) template = ac.find_all_template(mainScreen, circleScreen, rgb=True, threshold=threshold, bgremove=True) if template is None or len(template) <= 0: continue else: sort_template = sorted(template, key=lambda x: x["result"]) for t in sort_template: # print(t['rectangle'][1]) win32api.SetCursorPos( (int(t['result'][0]), int(t['result'][1]))) # win32api.SetCursorPos((int(t['rectangle'][1][0]), int(t['rectangle'][1][1]))) time.sleep(0.5) time.sleep(1) break except: pass time.sleep(config.conf.screenshot_main_time) else: print(print_name(circle_path) + ",没有识别出来")
def matchImg(imgsrc, imgobj, confidence=0.7): imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_template(imsrc, imobj, confidence) if match_result is not None: match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) return match_result
def get_coordinates_by_image_identify(self, search_img, source_img, get_rect=False): """ 通过图像识别来查找全部符合条件的待查找图片的坐标 :param search_img: 待查找图片的路径 :param source_img: 原始图片的路径 :param get_rect: 是否返回待查找图片的四角坐标 (左上, 左下, 右上, 右下) :return: 所有符合条件的图片坐标列表 例如: get_rect=False -> [(960.5, 243.5), (960.5, 243.5),...] get_rect=True -> [{'center': (x,y), 'rectangle': ((x,y),(x,y),(x,y),(x,y))}, ...]) """ try: source = ac.imread(source_img) # 原始图像 search = ac.imread(search_img) # 待查找的部分 positions = ac.find_all_template(source, search) cor = [] if positions is not None: for position in positions: x, y = position['result'] rect = position['rectangle'] if get_rect: cor.append({'center': (x, y), 'rectangle': rect}) else: cor.append((x, y)) else: raise Exception("Cannot find the images that you provided.") return cor except Exception as msg: raise Exception(msg)
def get_coordinate_by_image_identify(search_img, source_img, get_rect=False): """获取目标图片在原始图片中的坐标 :param search_img: 待查找图片的路径 :param source_img: 原始图片的路径 :param get_rect: 是否返回待查找图片的四角坐标 (左上, 左下, 右上, 右下) :return: 中心点坐标,get_rect为True时返回四角坐标 """ try: source = ac.imread(source_img) # 原始图像 search = ac.imread(search_img) # 待查找的部分 position = ac.find_template(source, search) 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 match_img(self, imgsrc, imgobj, threshold=None): ''' :param imgsrc: 原始图像 :param imgobj: 待查找的图片 :param confidencevalue: 最低相识度 :return:{'result': (1482.0, 748.5), 'rectangle': ((1445, 708), (1445, 789), (1519, 708), (1519, 789)), 'confidence': 0.9999999403953552, 'shape0': (1920, 1080), 'shape1': (74, 81)} ''' imsrc = ac.imread(self.imsrc_dir + r'\\' + imgsrc + '.' + self.format) imobj = ac.imread(self.imobj_dir + r'\\' + imgobj + '.' + self.format) # 是否有指定相识度 if threshold: match_result = ac.find_template(imsrc, imobj, threshold=threshold) else: match_result = ac.find_template(imsrc, imobj, threshold=self.threshold) if match_result is not None: match_result['shape0'] = (imsrc.shape[1], imsrc.shape[0] ) # 0为高,1为宽 match_result['shape1'] = (imobj.shape[1], imobj.shape[0] ) # 0为高,1为宽 match_result['imgsrc_name'] = imgsrc return match_result
def find_image_cv_unused(source_path, part_path): """ 注意:匹配度还存在一定误差,废弃 :param source: 原图 :param part:待匹配的图片 :return: """ # 原始图像 source = ac.imread(source_path) # 待查找的部分 part = ac.imread(part_path) # result = cv2.matchTemplate(source, part, cv2.TM_CCOEFF_NORMED) result = cv2.matchTemplate(source, part, cv2.TM_CCORR_NORMED) pos_start = cv2.minMaxLoc(result)[3] x = int(pos_start[0]) + int(part.shape[1] / 2) y = int(pos_start[1]) + int(part.shape[0] / 2) # 相似度 similarity = cv2.minMaxLoc(result)[1] print("相似度:%f" % similarity) if similarity < 0.85: return -1, -1 else: return [(x, y), (x, y)]
def compare(source_picture, goal): imsrc = ac.imread(source_picture) imobj = ac.imread(goal) # imsrc = cv2.imread(source_picture) # imobj = cv2.imread(goal) pos = ac.find_template(imsrc, imobj) return pos
def crop_basic_info(self, snapshot_img_path): try: imgsrc = snapshot_img_path imgobj = self.now_path + r'\Input\fengzhi.png' imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_template(imsrc, imobj, 0.5) if match_result is not None: match_result['shape'] = (imsrc.shape[1], imsrc.shape[0] ) # 0为高,1为宽 y = match_result['rectangle'][0][1] img = Image.open(imgsrc) # (left, upper, right, lower) cropped = img.crop((0, 1575, img.size[0], y)) basic_info_name = self.num + '_' + self.company_code + '_basic_info.png' basic_info_path = self.output_file_path + '\\' + basic_info_name cropped.save(basic_info_path) pic_code = '<table><img src=\'' + basic_info_path + '\'width=\'300\' height=\'400\'>' pic_code_path = self.output_file_path + r'\0_basicinfo_piccode.txt' with open(pic_code_path, 'a') as f2: f2.write(pic_code + '\n') except Exception as e: print('\n文件路径中不能存在中文字符!\n', e)
def _image_click(self, target, index=1): """ :param target: the target image that should be clicked :param index: select the N-th element :return: match info """ index = int(index) self._prepare() if self.img_path: target = os.path.join(self.img_path, target) else: self._mobilelib._info("[>>>] img path not set") im_source = ac.imread(self._screen.decode('utf-8').encode('gbk')) im_search = ac.imread(target.decode('utf-8').encode('gbk')) result = ac.find_all_template(im_source, im_search, self.TH) result = sorted(result, key=lambda x: (x['result'][1], x['result'][0])) if index == 0: index = len(result) - 1 else: index -= 1 re = result[index] self._mobilelib._info(re) if self._mobilelib._is_android(): self._mobilelib.click_a_point(re['result'][0], re['result'][1]) if self._mobilelib._is_ios(): _scale = self.scale() self._mobilelib.click_a_point(re['result'][0] / _scale, re['result'][1] / _scale) return re
def _parse_images(self, target, index=1, parse_type='num'): """[get the N-th image's coordinate] OR [the sum of the image] :param target: the target image that should be clicked :param index: select the N-th element; 0 -> the last one :return: the target Element's location:<x,y> """ self._prepare() index = int(index) if self.img_path: target = os.path.join(self.img_path, target) else: self._mobilelib._info("[>>>] img path not set") im_source = ac.imread(self._screen.decode('utf-8').encode('gbk')) im_search = ac.imread(target.decode('utf-8').encode('gbk')) result = ac.find_all_template(im_source, im_search, self.TH) if 'num' in parse_type: return len(result) elif 'location' in parse_type: result = sorted(result, key=lambda x: (x['result'][1], x['result'][0])) if index == 0: index = len(result) - 1 else: index -= 1 re = result[index] self._mobilelib._info(re) if self._mobilelib._is_android(): return re['result'][0], re['result'][1] if self._mobilelib._is_ios(): _scale = self.scale() return re['result'][0] / _scale, re['result'][1] / _scale
def matchImg(imgsrc, imgobj, confidencevalue=0.8): #imgsrc=原始图像,imgobj=待查找的图片 sleep(0.2) try: imsrc = ac.imread(imgsrc) except: sleep(1) imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) x1 = 0 y1 = 0 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)} print(match_result) if match_result is not None: match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) #0为高,1为宽 # print(match_result) # print(match_result['rectangle'][0]) #匹配的图片中心点 # x1 = match_result['rectangle'][0][0] # y1 = match_result['rectangle'][0][1] x1 = match_result['result'][0] y1 = match_result['result'][1] # res = (0,0) # res[0] = int(match_result['rectangle'][0][0]) * 2.5 # res[1] = int(match_result['rectangle'][0][1]) * 2.5 # x1 = int(x1 * 2.5) #! 使用minicap截屏的图片,拿到的坐标必须乘以2.5才是实际手机屏幕坐标 # y1 = int(y1 * 2.5) x1 = int(x1) y1 = int(y1) # print(x1,y1) return x1, y1
def locatonScreen(self, imgobj, confidence=0.9, click=True, imsrc=None, shot_name='screenshot'): """ 判断图像是否匹配当前屏幕 Parameters ---------- imgobj : 待查找图片 confidence : 置信度 click : 是否进行点击 imsrc : 原始图像 shot_name : 手机截屏 Returns ------- match_result : 匹配结果 """ if not imsrc: imsrc = ac.imread(self.d.screenshot(f'{shot_name}.png')) else: imsrc = ac.imread(imsrc) imobj = ac.imread(imgobj) match_result = ac.find_template(imsrc, imobj, confidence) if match_result is not None: match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽 if click: x, y = match_result['result'] self.d.click(x, y) return match_result
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 loop_answer(): for index, ans in enumerate(ansarray): get_screenshot() imsrc = ac.imread(sc_path) print u"第%d题答案:%s" % (index + 1, ans) final_ans = ans.split(',') for answer in final_ans: imres = ac.imread('C:/%s.jpg' % answer) pos = ac.find_template(imsrc, imres) if pos is not None: logging.debug(u'%s.jpg相似度%f' % (answer, pos['confidence'])) if pos['confidence'] > CONFIDENCE: m.press(pos['result'][0], pos['result'][1]) else: logging.error(u'无法定位到答案') time.sleep(1) if (index < len(ansarray) - 1): imnext = ac.imread('C:/next.jpg') nextpos = ac.find_template(imsrc, imnext) if nextpos is not None: logging.debug(u'next.jpg相似度%f' % (pos['confidence'])) if nextpos['confidence'] > CONFIDENCE: m.press(nextpos['result'][0], nextpos['result'][1]) else: logging.error(u'无法定位到下一个按钮') time.sleep(1)
def tmpl_test(): t1 = ac.imread("tests/testdata/2s.png") t2 = ac.imread("tests/testdata/2t.png") import time start = time.time() print((ac.find_all_template(t1, t2))) print(('Time used:', time.time() - start))
def find_ico_pos(back_png_url, obj_png_url, find_step): old_imsrc = ac.imread(back_png_url) if find_step == 0: imsrc = old_imsrc[0:820, 200:1080] else: imsrc = old_imsrc # cv2.imwrite("/tmp/Copy.jpg", imsrc) imobj = ac.imread(obj_png_url) # find the match position pos = ac.find_template(imsrc, imobj, 0.85) if not pos: return pos print('相似度 %s' % pos['confidence']) if find_step == 0: x = int(pos['result'][0]) + 200 y = int(pos['result'][1]) else: x = int(pos['result'][0]) y = int(pos['result'][1]) circle_center_pos = (x, y) # draw_circle(imsrc, circle_center_pos, circle_radius, color, line_width) return circle_center_pos
def _parse_images(self, target, index=1, parse_type='num'): """[get the N-th image's coordinate] OR [the sum of the image] :param target: the target image that should be clicked :param index: select the N-th element; 0 -> the last one :return: the target Element's location:<x,y> """ self._prepare() index = int(index) if self.img_path: target = os.path.join(self.img_path, target) else: self._info("[>>>] img path not set") im_source = ac.imread(self._screen.decode('utf-8').encode('gbk')) im_search = ac.imread(target.decode('utf-8').encode('gbk')) result = ac.find_all_template(im_source, im_search, self.TH) if 'num' in parse_type: return len(result) elif 'location' in parse_type: if index == 0: index = len(result) - 1 else: index -= 1 re = result[index] self._info(re) return re['result'][0], re['result'][1]
def ready(): img_src = ac.imread('D:\\verysync\\github\\yys\\yuhun.png') img_btn = ac.imread('D:\\verysync\\github\\yys\\ready_button.jpg') ready_pos = ac.find_template(img_src,img_btn) '''pos的结果可以获得图像匹配的坐标,具体的匹配结果的四个角的坐标 confidence 的值就是匹配程度,匹配失败返回none''' return ready_pos
def submit_answer(): #点击交卷 get_screenshot() imsrc = ac.imread(sc_path) imsubmit = ac.imread('C:/submit.jpg') submitpos = ac.find_template(imsrc, imsubmit) if submitpos is not None: logging.debug(u'交卷按钮相似度%f' % submitpos['confidence']) if (submitpos['confidence'] > CONFIDENCE): m.press(submitpos['result'][0], submitpos['result'][1]) print(u'>>交卷') else: logging.error(u'未找到交卷按钮') else: logging.error(u'无法定位到交卷按钮') time.sleep(2) #点击确定 get_screenshot() imsrc = ac.imread(sc_path) imyes = ac.imread('C:/yes.jpg') yespos = ac.find_template(imsrc, imyes) if yespos is not None: logging.debug(u'确定按钮相似度%f' % yespos['confidence']) if (yespos['confidence'] > CONFIDENCE): m.press(yespos['result'][0], yespos['result'][1]) print(u'>>完成答题') else: logging.error(u'未找到确定按钮') else: logging.error(u'无法定位到确定按钮')
def find_element(hwnd, element_pic_path, timeout): win32gui.SetForegroundWindow(hwnd) left, top, right, bottom = win32gui.GetWindowRect(hwnd) # print(left, top, right, bottom) # print(right - left, bottom - top) app_bg_box = (left, top, right, bottom) im = ImageGrab.grab(app_bg_box) im.save('bg.png') imsrc = ac.imread('bg.png') imobj = ac.imread(element_pic_path) # 匹配element图像 pos = ac.find_template(imsrc, imobj) pprint(pos) if pos['confidence'] > 0.9: print('能够匹配到element图像') x, y = pos['result'] _detect_obj(img=imsrc, circle_center_pos=(int(x), int(y)), circle_radius=40, color=(0, 255, 0), line_width=2) return True else: print('未匹配到element图像') return False
def recognition(image_byte, num): #识别验证码 with open('logincapture.bmp','wb') as f: f.write(image_byte) f.close() imsrc = ac.imread('logincapture.bmp') #imsrc = ac.imread(image_byte) most_confidence = [] #根据模板内容,取出最自信的n个字符 filedir = './template' for file in os.listdir(filedir): if fnmatch(file, '*.jpg'): imobj = ac.imread('./template/'+file) # find the match position pos = ac.find_template(imsrc, imobj) try: most_confidence.append((pos['confidence'],pos['result'][0],file)) except TypeError: pass #排序后输出结果 most_confidence.sort(reverse=True) target = most_confidence[0:num] target.sort(key=lambda x:x[1]) result = '' for i in range(len(target)): # print(target[i][2].strip('.')) result = result + target[i][2].strip('.')[0] print(result) return(result)
def shudu_num_position(self, num_list_orn, num_press_center): # num_list_orn是用来点击空格的 # 生成下面的要点击输入的1-9数字的坐标列表,首先点击没有数字的空格,出现下面输入数字的框 break_logo = i_x = i_y = 0 for i in range(9): for j in range(9): if num_list_orn[i][j] == 0: i_x = i i_y = j break_logo = 1 break if break_logo == 1: break self.pp.click(num_press_center[i_x][i_y][0], num_press_center[i_x][i_y][1]) self.pp.screenshot("temp_all_num.jpg") im_all_num = ac.imread('temp_all_num.jpg') # 获取坐标 press_num_input = [] for i in range(1, 10): img_name = ac.imread('./pic/pad/input_{}.jpg'.format(str(i))) pos_num = ac.find_template(im_all_num, img_name, threshold=0.8) try: press_num_input.append(pos_num['result']) except Exception as e: print('没有获取到下面输入数字{}的坐标匹配图'.format(i)) raise if not press_num_input: print("下面输入数字窗口的中心点坐标数组有误") raise else: return press_num_input
def tmpl_test(): t1 = ac.imread("testdata/2s.png") t2 = ac.imread("testdata/2t.png") import time start = time.time() print ac.find_all_template(t1, t2) print time.time() - start
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)
def _image_click(self, target, index=1): """ :param target: the target image that should be clicked :param index: select the N-th element :return: match info """ index = int(index) self._prepare() if self.img_path: target = os.path.join(self.img_path, target) else: self._mobilelib._info("[>>>] img path not set") im_source = ac.imread(self._screen.decode('utf-8').encode('gbk')) im_search = ac.imread(target.decode('utf-8').encode('gbk')) result = ac.find_all_template(im_source, im_search, self.TH) result = sorted(result, key=lambda x: (x['result'][1], x['result'][0])) if index == 0: index = len(result) - 1 else: index -= 1 re = result[index] self._mobilelib._info(re) if self._mobilelib._is_android(): self._mobilelib.click_a_point(re['result'][0], re['result'][1]) if self._mobilelib._is_ios(): _scale = self.scale() self._mobilelib.click_a_point(re['result'][0]/_scale, re['result'][1]/_scale) return re
def tmpl_test(): t1 = ac.imread("testdata/2s.png") t2 = ac.imread("testdata/2t.png") import time start = time.time() print ac.find_all_template(t1, t2) print 'Time used:', time.time() - start
def get_position(browser): # 截取带验证码的登录页面,保存为im.png(本文的目标网站的登录验证码方式是在大图中点击目标小图) browser.maximize_window() # 本人是mac pro 15.4英寸,最大化chrome窗口后,截图大小为3360*1664 browser.save_screenshot(r'../img/im.png') # 用PIL库操作图片 im = Image.open(r'../img/im.png') # 灰度化,保存为im_grey.png im_grey = im.convert('L') im_grey.save(r'../img/im_grey.png') # 在im.png图中截取目标小图(验证码图片):im_crop.png # 不同大小的显示器可能不同,需要自己手动算一下,计算方法就是在画图工具中看下验证码图片左上角坐标和右下角坐标。可能需要多次试验才能准确截取 im_crop = im.crop((2330, 1350, 2384, 1410)) im_crop.save(r'../img/im_crop.png') im_crop_grey = im_crop.convert('L') # 灰度化 im_crop_grey.save(r'../img/im_crop_grey.png') # 以上是准备要用到图片,下面是找出验证码在网页中的坐标,并模拟点击,以通过验证码校验 # 以下步骤是:在im_grey.png图中的场景图片中找出im_crop_grey.png图一样的图案(这里注意,是在im_grey.png中上半部分场景中去找),并画一个圈 # 1.因为im_grey.png图的场景图片中目标图案比较大,所以要缩小im_grey.png图,保存为result.png,缩小的倍数需要手工计算一下(多试几组值,观察 # 缩小后的result.png中的目标图是否与im_crop.png长和宽相差不大即可) # 2.找出目标图im_crop_grey.png在result.png的位置,并画一个圈,保存为result_img1.png # 3.按比例计算目标图im_crop.png在im_grey原始图中的位置,并画一个圈 # 下面是步骤1、2 # 我的im_grey.png原始大小是3360*1664,计算后需要缩小2.89倍 res = cv2.resize(cv2.imread(r'../img/im_grey.png'), (1162, 576), interpolation=cv2.INTER_CUBIC) # 该方法可缩放扶贫 cv2.imwrite(r'../img/result.png', res) # 保存缩小后的图片 imsrc = ac.imread(r'../img/result.png') imobj = ac.imread(r'../img/im_crop_grey.png') # find the match position try: pos = ac.find_template(imsrc, imobj) # 该方法很厉害,能够找到jmobj在imsrc中的坐标位置。如果找不到返回None circle_center_pos = (int(pos['result'][0]), int(pos['result'][1])) except TypeError as e: print('没有定位到,请重试!') # 定义一些花圈的参数 circle_radius = 25 color = (0, 255, 0) line_width = 3 # 为方便调试打印下坐标 #print(circle_center_pos) # 画圈,并保存画圈后的图片 result_img1 = draw_circle(imsrc, circle_center_pos, circle_radius, color, line_width) cv2.imwrite(r'../img/result_img1.png', result_img1) # 下面是步骤3: # 以上计算的坐标是在缩小2.89倍以后的图片上,想要计算计算在原始图中的坐标需要把坐标*2.89 real_pos = (int(pos['result'][0] * 2.89), int(pos['result'][1] * 2.89)) #print(real_pos) result_img2 = draw_circle(ac.imread(r'../img/im_grey.png'), real_pos, circle_radius, color, line_width) cv2.imwrite(r'../img/result_img2.png', result_img2) return real_pos
def get_deal_locations_picture(self, filename, mubiao_file,num=0.5): imsrc = ac.imread(mubiao_file) imobj = ac.imread(filename) pos = ac.find_all_template(imsrc, imobj, num) if pos == None: return 0 else: return pos
def matchImg(imgsrc, imgobj): # imgsrc=原始图像,imgobj=待查找的图片 imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_template(imsrc, imobj, 0.9) #0.9、confidence是精度,越小对比的精度就越低 {'confidence': 0.5435812473297119, 'rectangle': ((394, 384), (394, 416), (450, 384), (450, 416)), 'result': (422.0, 400.0)} # print(match_result) if match_result is not None: match_result['shape'] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽 return match_result
def match_img(imgsrc,imgobj,confidencevalue=0.7):#imgsrc=原始图像,imgobj=待查找的图片 x_co = None y_co = None imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_template(imsrc,imobj,confidencevalue) return (match_result)
def matchimg(img_old, img_new): #img_old,img_new): imsrc = ac.imread(img_old) imobj = ac.imread(img_new) result = ac.find_template(imobj, imsrc, 0.5) #if result is not None: #result['shape']= (imsrc.shape[1],imsrc.shape[0]) return result['rectangle'][0], result['rectangle'][3]
def __init__(self, imsch, imsrc='screen.png'): try: self.element_name = imsch self.imsrc = ac.imread(os.path.join(SCREENIMG, imsrc)) self.imsch = ac.imread(os.path.join(SCREENIMG, imsch)) except: traceback.print_exc() raise OpenImgError('打开图片失败')
def input_username(self): print("input username") findimg = ac.imread("assets/jh/username.jpg") maiimg = ac.imread('assets/jh/a.jpg') pos = ac.find_template(maiimg, findimg, 0.3) print("this is username pos",pos) #this is username pos {'result': (920.0, 325.0), 'rectangle': ((910, 311), (910, 339), (930, 311), (930, 339)), 'confidence': 0.9712943434715271} print(pos['rectangle'][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'
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 click(self, SF, timeout=None, duration=None): ''' Click function @param seconds: float (if time not exceed, it will retry and retry) ''' if timeout == None: timeout = self._click_timeout log.info('CLICK %s, timeout=%.2fs, duration=%s', SF, timeout, str(duration)) point = self._PS2Point(SF) if point: (x, y) = point else: (x, y) = self.wait(SF, timeout=timeout) log.info('Click %s point: (%d, %d)', SF, x, y) self.dev.touch(x, y, duration) log.debug('delay after click: %.2fs' ,self._delay_after_click) # FIXME(ssx): not tested if self._operation_mark: if self._snapshot_file and os.path.exists(self._snapshot_file): img = ac.imread(self._snapshot_file) ac.mark_point(img, (x, y)) cv2.imwrite(self._snapshot_file, img) if self._devtype == 'android': self.dev.adbshell('am', 'broadcast', '-a', 'MP_POSITION', '--es', 'msg', '%d,%d'%(x, y)) time.sleep(self._delay_after_click)
def _imfindall(self, bgimg, search, maxcnt, sort): if not maxcnt: maxcnt = 0 method = self._image_match_method imsrc, imsch = ac.imread(bgimg), ac.imread(search) if method == 'auto': points = ac.find_all(imsrc, imsch, maxcnt=5) # points = imtauto.locate_more_image_Template(search, bgimg, num=maxcnt) elif method == 'template': points = imttemplate.findall(search, bgimg, self._threshold, maxcnt=maxcnt) elif method == 'sift': points = imtsift.findall(search, bgimg, maxcnt=maxcnt) else: raise RuntimeError("Unknown image match method: %s" %(method)) if sort: def cmpy((x0, y0), (x1, y1)): return y1<y0 def cmpx((x0, y0), (x1, y1)): return x1<x1
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 main(): bg = aircv.imread('testdata/bg.png') search = aircv.imread('testdata/guard.png') print aircv.find_sift(bg, search)