How to use _get_template_result_matrix method in Airtest

Best Python code snippet using Airtest

template.py

Source: template.py Github

copy

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

Blogs

Check out the latest blogs from LambdaTest on this topic:

QA&#8217;s and Unit Testing &#8211; Can QA Create Effective Unit Tests

Unit testing is typically software testing within the developer domain. As the QA role expands in DevOps, QAOps, DesignOps, or within an Agile team, QA testers often find themselves creating unit tests. QA testers may create unit tests within the code using a specified unit testing tool, or independently using a variety of methods.

LIVE With Automation Testing For OTT Streaming Devices ????

People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.

Why Agile Is Great for Your Business

Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.

Options for Manual Test Case Development &#038; Management

The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.

Test strategy and how to communicate it

I routinely come across test strategy documents when working with customers. They are lengthy—100 pages or more—and packed with monotonous text that is routinely reused from one project to another. Yawn once more— the test halt and resume circumstances, the defect management procedure, entrance and exit criteria, unnecessary generic risks, and in fact, one often-used model replicates the requirements of textbook testing, from stress to systems integration.

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:

YouTube

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?

Helpful

NotHelpful