How to use _resize_by_ratio method in Airtest

Best Python code snippet using Airtest Github


Full Screen

...75 rectangle = (left_top_pos, left_bottom_pos,76 right_bottom_pos, right_top_pos)77 return middle_point, rectangle78 @staticmethod79 def _resize_by_ratio(src, templ, ratio=1.0, templ_min=10, src_max=800):80 """根据模板相对屏幕的长边 按比例缩放屏幕"""81 # 截屏最大尺寸限制82 sr = min(src_max/max(src.shape),1.0)83 src = cv2.resize(src, (int(src.shape[1]*sr), int(src.shape[0]*sr)))84 # 截图尺寸缩放85 h, w = src.shape[0], src.shape[1]86 th, tw = templ.shape[0], templ.shape[1]87 if th/h >= tw/w:88 tr = (h*ratio)/th89 else:90 tr = (w*ratio)/tw91 templ = cv2.resize(templ, (max(int(tw*tr), 1), max(int(th*tr), 1)))92 return src, templ, tr, sr93 @staticmethod94 def _org_size(max_loc, w, h, tr, sr):95 """获取原始比例的框"""96 max_loc = (int((max_loc[0]/sr)), int((max_loc[1]/sr)))97 w, h = int((w/sr)), int((h/sr))98 return max_loc, w, h99 def multi_scale_search(self, org_src, org_templ, templ_min=10, src_max=800, ratio_min=0.01, ratio_max=0.99, step=0.01, threshold=0.8):100 """多尺度模板匹配"""101 mmax_val = 0102 max_info = None103 r = ratio_min104 while r <= ratio_max:105 src, templ, tr, sr = self._resize_by_ratio(106 org_src.copy(), org_templ.copy(), r, src_max=src_max)107 if min(templ.shape) > templ_min:108 src[0,0] = templ[0,0] = 0109 src[0,1] = templ[0,1] = 255110 result = cv2.matchTemplate(src, templ, cv2.TM_CCOEFF_NORMED)111 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)112 h, w = templ.shape113 if mmax_val < max_val:114 mmax_val = max_val115 max_info = (r, max_val, max_loc, w, h, tr, sr)116 # print((r, max_val, max_loc, w, h, tr, sr))117 if max_val >= threshold:118 omax_loc, ow, oh = self._org_size(max_loc, w, h, tr, sr)119 confidence = self._get_confidence_from_matrix(omax_loc, ow, oh)...

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?