def main(): #imgsrc = cv2.imread("D:/PCP/04ad16730a0-b/04ad16730a0-b-ng/04ad16730a0-b-2.jpg") imgsrc = cv2.imread("C:/Users/lenovo/Pictures/Saved Pictures/part1.jpg") imgsrc2 = cv2.imread("C:/Users/lenovo/Pictures/Saved Pictures/part2.jpg") #imgobj = cv2.imread("C:/Users/lenovo/Desktop/train_data/A1/04ad16730a0-b-30.jpg") imgobj = cv2.imread( "C:/Users/lenovo/Pictures/Saved Pictures/04ad16730a0-b-1 (4).jpg") size = imgsrc.shape h = size[0] w = size[1] scale = 480 / h th, tw = h * scale, w * scale pos_list = ac.find_all_template(imgsrc, imgobj) pos_list2 = ac.find_all_template(imgsrc2, imgobj) print(pos_list) print(pos_list2) color = (0, 255, 0) line_width = 10 for pos in pos_list: conf = pos['confidence'] pos = pos['result'] if conf < 0.9: continue draw_rectangle(imgsrc, pos, imgobj.shape[0], imgobj.shape[1], color, line_width) cv2.namedWindow("Detection", 0) cv2.resizeWindow("Detection", int(tw), int(th)) cv2.imshow('Detection', imgsrc) cv2.waitKey(0)
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 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 check_pic_xyz(str_check_file, str_base_file, var_find=None, num_resolution_x=None, num_resolution_y=None, num_confidence=0.9, num_base_x=0, num_base_y=0): # base是大图,check是小图,能找到图片的完整路径 obj_base = aircv.imread(str_base_file) #obj_base = aircv.imread(str_base_file) obj_check = aircv.imread(str_check_file) # 分辨率x,y if num_resolution_x: num_use_x = int(int(num_resolution_x) / obj_base.shape[1]) else: num_use_x = 1 if num_resolution_y: num_use_y = int(int(num_resolution_y) / obj_base.shape[0]) else: num_use_y = 1 list_tmp = list() # 最后得到的坐标x,y和识别准确率 var_out = None # 如果存在多个可以设置使用第几个匹配到的 try: if var_find: if var_find != "all": var_tmp = aircv.find_all_template( obj_base, obj_check, float(num_confidence))[int(var_find)] else: var_tmp = aircv.find_all_template(obj_base, obj_check, float(num_confidence)) else: var_tmp = aircv.find_template(obj_base, obj_check, float(num_confidence)) if var_tmp: if type(var_tmp) == dict: list_tmp.append(var_tmp) else: list_tmp = var_tmp list_out = list() for dict_value in list_tmp: # num_base_x,num_base_y是基础偏移,默认不偏移 list_out.append([ int(num_base_x + dict_value["result"][0] * num_use_x), int(num_use_y + dict_value["result"][1] * num_use_y), dict_value["confidence"] ]) if len(list_out) == 1: var_out = list_out[0] else: var_out = list_out return var_out except Exception as e: print(e) return 0
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 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 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 find_elements(self, comment, timeout, threshold=0.9, rect=(0, 25, 480, 800)): ret = [] img_obj = ac.imread(self._work_path + '\\Controller\\' + self._PIC_PATH[comment]) self.get_capture() while timeout > 0: pos_list = ac.find_all_template(self._capture_obj, img_obj, threshold=threshold) for pos in pos_list: (x, y) = pos['result'] ret.append((int(x), int(y))) if len(ret) > 0: self._log('<<info>> 匹配到:' + comment, str(len(ret)) + '个') break else: time.sleep(1) timeout -= 1 self.get_capture() self._log('<<info>> 未匹配:', comment + ' ' + str(timeout) + 's') return len(ret) > 0, ret
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 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 _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 _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 _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 _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 find_pic(source, tmp): res = aircv.find_all_template(source, tmp) r = [] for dic in res: if dic['confidence'] < 0.9: continue r.append([int(x) for x in dic['result']]) return r
def find_all_img_in_img(src_img_path, target_img_path, accuracy): src_img = cv2.imdecode(np.fromfile(src_img_path, dtype=np.uint8), -1) target_img = cv2.imdecode(np.fromfile(target_img_path, dtype=np.uint8), -1) pos = ac.find_all_template(src_img, target_img, accuracy) if pos is None: return -1 return len(pos)
def findImg(obj,find,all="false"): imgobj = ac.imread(obj) imgfind = ac.imread(find) if(all == "true"): result = ac.find_all_template(imgobj, imgfind, 0.9) else: result = ac.find_template(imgobj, imgfind, 0.9) return result
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_all(w, imgsrc, imgobj, confidencevalue=0.75): # imgsrc=原始图像,imgobj=待查找的图片 imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) if imsrc.shape[0] != w.template_height and imsrc.shape[1] != w.template_width: size = (int(imobj.shape[1] * imsrc.shape[1] / w.template_width), int(imobj.shape[0] * imsrc.shape[0] / w.template_height)) imobj = cv.resize(imobj, size, interpolation=cv.INTER_AREA) match_result = ac.find_all_template(imsrc, imobj, confidencevalue) return match_result
def find_all_template(imgsrc,imgobj,confidence=0.9):#imgsrc=原始图像,imgobj=待查找的图片 imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_all_template(imsrc,imobj,confidence) # {'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
def find_all_template(cls, image_current, image_template, min_confidence): match_results = ac.find_all_template(image_current, image_template, min_confidence) if match_results is None: return None res_list = [] for match_result in match_results: res = cls._change_result(match_result) res_list.append(res) return res_list
def match_all(self, pattern): """ Test method, not suggested to use """ pattern = self.pattern_open(pattern) search_img = pattern.image screen = self.region_screenshot() screen = imutils.from_pillow(screen) points = ac.find_all_template(screen, search_img, maxcnt=10) return points
def matchImg(imgobj, confidence=0.5, quzhi='result'): #传入小图、相似度、取值,返回小图所在屏幕的坐标 '''取值分别为result【中心点坐标】,rectangle【四角坐标】,confidence【相似度】''' imobj = cv2.imdecode(numpy.fromfile(imgobj, dtype=numpy.uint8), -1) #解决imread不能读取中文路径的问题 imgsrc = 'pm001.jpg' window_capture(imgsrc) #截屏 imsrc = cv2.imdecode(numpy.fromfile(imgsrc, dtype=numpy.uint8), -1) #解决imread不能读取中文路径的问题 match_result = aircv.find_all_template(imsrc, imobj, confidence) if match_result: return [i[quzhi] for i in match_result] else: return False # ;print('%s查找失败'% imgobj[:-4])
def find_all_image_position(origin='origin.png', query='query.png', confidence=0.9): imsrc = ac.imread(origin) # 原始图像 imsch = ac.imread(query) # 带查找的部分 posArray = ac.find_all_template(imsrc, imsch) t = [] for pos in posArray: if pos['confidence'] > confidence: t.append(pos['result']) return t
def get_locations_picture(self, filename, num=0.5, cut_zone=None): if cut_zone == None: imsrc = ac.imread('D:\\dh2\\system\\0.png') else: imsrc = ac.imread('D:\\dh2\\system\\1.png') imobj = ac.imread(filename) # find the match position pos = ac.find_all_template(imsrc, imobj, num) if pos == None: return 0 else: return pos
def vertex_position(self): # 计算数独数组所在大框的左上和右下角的坐标 im_ver = ac.imread('./pic/pad/ver.jpg') im_lat = ac.imread('./pic/pad/lat.jpg') lef_up_x = lef_up_y = right_down_x = right_down_y = 0 all_pos_ver = ac.find_all_template(self.im_all, im_ver, threshold=0.9) for i in all_pos_ver: # print(i['rectangle']) lef_up_x = i['rectangle'][0][0] right_down_x = i['rectangle'][3][0] all_pos_lat = ac.find_all_template(self.im_all, im_lat, threshold=0.9) for i in all_pos_lat: # print(i['rectangle']) lef_up_y = i['rectangle'][0][1] right_down_y = i['rectangle'][3][1] if lef_up_y and lef_up_x and right_down_y and right_down_x: x1_y1 = (lef_up_x, lef_up_y) x2_y2 = (right_down_x, right_down_y) else: print("没有获取左上角和右下角坐标") print(lef_up_x, lef_up_y, right_down_x, right_down_y) raise return x1_y1, x2_y2
def match_all(self, pattern, screen=None, rect=None, offset=None, threshold=None): 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) = [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) ret = None confidence = None position = None position_list = [] ret_all = ac.find_all_template(screen, search_img, maxcnt=10) if not ret_all: return None for ret in ret_all: confidence = ret['confidence'] if confidence > threshold: (x, y) = ret['result'] position = (x + dx, y + dy) position_list.append(position) return position_list
def find_all_imgs(self, part_name, accuracy=0.9): if not self.hwnd or win32gui.IsIconic(self.hwnd): # 窗口不存在或最小化 return None img = self.screen_shot() img_mat = cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR) # img_mat = aircv.imread('screenshot.png') matches = aircv.find_all_template(img_mat, self.imgs[part_name]) positions = [] for match in matches: if match['confidence'] < accuracy: continue positions.append(match['result']) # draw_circle(img_mat, positions, 60, (20, 200, 20), 3) return positions
def get_attack_pos(pic, all_pos=False, pic_as_img=False): im = pic if pic_as_img else ac.imread(pic) im2 = ac.imread('standard/attack.png') try: if all_pos: match_result = ac.find_all_template(im, im2, 0.95, 6) if match_result: return [x['result'] for x in match_result] else: match_result = ac.find_template(im, im2, 0.95) if match_result: return match_result['result'] except Exception as e: print('get_attack_pos exception', e) return
def find_image_position(self, origin='origin.png', query='query.png'): imsrc = ac.imread(origin) # 原始图像 imsch = ac.imread(query) # 带查找的部分 posArray = ac.find_all_template(imsrc, imsch) if len(posArray) == 0: return None t = posArray[0] for pos in posArray: if t == None: t = pos else: if pos['confidence'] > t['confidence']: t = pos print 'find', t return t
def get_photo_top_y(self, img_obj): # 照片图框 152 * 152 or 128 * 128 img_border = ac.imread(self.border_path) h, w, _ = img_border.shape ret = ac.find_all_template(img_obj, img_border, threshold=0.5) l = [] for r in ret: (x, y) = r['result'] l.append(y) if l: ret = min(l) else: ret = -1 return ret
def tap_btn(self, btn, bmp=None, threshold=0.9, ignore_error=False): if bmp is None: path = self.screencap('btn_%s.png' % self.uid) bmp = cv2.imread(path) btn_pt = None pts = aircv.find_all_template(bmp, btn, threshold=threshold) if pts is not None and len(pts) > 0: btn_pt = pts[0]['result'] elif ignore_error: return None else: raise NoMatchException('could not match the button') return self.tap(btn_pt)
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._mobilelib._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: if self._mobilelib._is_android(): self._mobilelib.click_a_point( result[i]['result'][0], result[i]['result'][1]) if self._mobilelib._is_ios(): _scale = self.scale() self._mobilelib.click_a_point( result[i]['result'][0] / _scale, result[i]['result'][1] / _scale) elif 'coordinate' in phase_type: if self._mobilelib._is_android(): return result[i]['result'][0], result[i][ 'result'][1] if self._mobilelib._is_ios(): _scale = self.scale() return result[i]['result'][0] / _scale, result[ i]['result'][1] / _scale except Exception, e: print '[xxx]: %s ' % traceback.format_exc()
def matchImg1(imgobj, imgsrc, confidence=0.5, quzhi='result'): #传入小图、大图、相似度、取值,返回小图所在大图的坐标 '''取值分别为result【中心点坐标】,rectangle【四角坐标】,confidence【相似度】''' ## imdecode读取的是rgb,如果后续需要opencv处理的话,需要转换成bgr,转换后图片颜色会变化 ##cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR) imobj = cv2.imdecode(numpy.fromfile(imgobj, dtype=numpy.uint8), -1) #解决imread不能读取中文路径的问题 imsrc = cv2.imdecode(numpy.fromfile(imgsrc, dtype=numpy.uint8), -1) #解决imread不能读取中文路径的问题 #imsrc = aircv.imread(imgsrc)#读取图像 #imobj = aircv.imread(imgobj)#读取图像 #match_result = aircv.find_template(imsrc,imobj,confidence) match_result = aircv.find_all_template(imsrc, imobj, confidence) #match_result[0]={'result': (1481.0, 856.0), 'rectangle': ((1459, 845), (1459, 867), (1503, 845), (1503, 867)), 'confidence': 0.6873950958251953} if match_result: return [i[quzhi] for i in match_result] else: return False #;print('%s查找失败'% imgobj[:-4])
def crop_check(): rget = flask.request.args.get screen = rget('screen') x, y = int(rget('x')), int(rget('y')) width, height = int(rget('width')), int(rget('height')) screen_file = screen.lstrip('/').replace('/', os.sep) screen_path = os.path.join(utils.selfdir(), screen_file) imsrc = cv2.imread(screen_path) imsch = imsrc[y:y+height, x:x+width] # crop image siftcnt = ac.sift_count(imsch) tmpl = ac.find_all_template(imsrc, imsch) sift = ac.find_all_sift(imsrc, imsch) #match_result = 'template: %s\nsift: %s' % (str(tmpl), str(sift)) return flask.jsonify(dict(siftcnt=siftcnt, result_template=str(tmpl), result_sift = str(sift)))