How to use _get_template_result_matrix method in Airtest

Best Python code snippet using Airtest Github


Full Screen

...12 """函数功能:找到最优结果."""13 # 第一步:校验图像输入14 check_source_larger_than_search(im_source, im_search)15 # 第二步:计算模板匹配的结果矩阵res16 res = _get_template_result_matrix(im_source, im_search)17 # 第三步:依次获取匹配结果18 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)19 h, w = im_search.shape[:2]20 # 求取可信度:21 confidence = _get_confidence_from_matrix(im_source, im_search, max_loc, max_val, w, h, rgb)22 # 求取识别位置: 目标中心 + 目标区域:23 # middle_point, rectangle = _get_target_rectangle(max_loc, w, h)24 # best_match = generate_result(middle_point, rectangle, confidence)25 # LOGGING.debug("threshold=%s, result=%s" % (threshold, best_match))26 # return best_match if confidence >= threshold else None27 print confidence28 return True if confidence >= threshold else None29def find_all_template(im_source, im_search, threshold=0.8, rgb=False, max_count=10):30 """根据输入图片和参数设置,返回所有的图像识别结果."""31 # 第一步:校验图像输入32 check_source_larger_than_search(im_source, im_search)33 # 第二步:计算模板匹配的结果矩阵res34 res = _get_template_result_matrix(im_source, im_search)35 # 第三步:依次获取匹配结果36 result = []37 h, w = im_search.shape[:2]38 while True:39 # 本次循环中,取出当前结果矩阵中的最优值40 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)41 # 求取可信度:42 confidence = _get_confidence_from_matrix(im_source, im_search, max_loc, max_val, w, h, rgb)43 if confidence < threshold or len(result) > max_count:44 break45 # 求取识别位置: 目标中心 + 目标区域:46 middle_point, rectangle = _get_target_rectangle(max_loc, w, h)47 one_good_match = generate_result(middle_point, rectangle, confidence)48 result.append(one_good_match)49 # 屏蔽已经取出的最优结果,进入下轮循环继续寻找:50 cv2.floodFill(res, None, max_loc, (-1000,), max(max_val, 0), flags=cv2.FLOODFILL_FIXED_RANGE)51 return result if result else None52def _get_confidence_from_matrix(im_source, im_search, max_loc, max_val, w, h, rgb):53 """根据结果矩阵求出confidence."""54 # 求取可信度:55 if rgb:56 # 如果有颜色校验,对目标区域进行BGR三通道校验:57 img_crop = im_source[max_loc[1]:max_loc[1] + h, max_loc[0]: max_loc[0] + w]58 confidence = cal_rgb_confidence(img_crop, im_search)59 else:60 confidence = max_val61 return confidence62def _get_template_result_matrix(im_source, im_search):63 """求取模板匹配的结果矩阵."""64 # 灰度识别: cv2.matchTemplate( )只能处理灰度图片参数65 s_gray, i_gray = img_mat_rgb_2_gray(im_search), img_mat_rgb_2_gray(im_source)66 return cv2.matchTemplate(i_gray, s_gray, cv2.TM_CCOEFF_NORMED)67def _get_target_rectangle(left_top_pos, w, h):68 """根据左上角点和宽高求出目标区域."""69 x_min, y_min = left_top_pos70 # 中心位置的坐标:71 x_middle, y_middle = int(x_min + w / 2), int(y_min + h / 2)72 # 左下(min,max)->右下(max,max)->右上(max,min)73 left_bottom_pos, right_bottom_pos = (x_min, y_min + h), (x_min + w, y_min + h)74 right_top_pos = (x_min + w, y_min)75 # 点击位置:76 middle_point = (x_middle, y_middle)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?