How to use get_predict_area method in Airtest

Best Python code snippet using Airtest Github


Full Screen

...160 if not self.record_pos:161 return None162 # calc predict area in screen163 screen_resolution = aircv.get_resolution(screen)164 xmin, ymin, xmax, ymax = Predictor.get_predict_area(self.record_pos, screen_resolution)165 # crop predict image from screen166 predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax))167 # # find sift in that image169 ret_in_area = aircv.find_sift(predict_area, image, threshold=self.threshold, rgb=self.rgb)170 # calc cv ret if found171 if not ret_in_area:172 return None173 ret = deepcopy(ret_in_area)174 ret["result"] = (ret_in_area["result"][0] + xmin, ret_in_area["result"][1] + ymin)175 return ret176 def _resize_image(self, image, screen, resize_method):177 """模板匹配中,将输入的截图适配成 等待模板匹配的截图."""178 # 未记录录制分辨率,跳过179 if not self.resolution:180 # return image181 self.resolution = aircv.get_resolution(image)182 screen_resolution = aircv.get_resolution(screen)183 # 如果分辨率一致,则不需要进行im_search的适配:184 if tuple(self.resolution) == tuple(screen_resolution) or resize_method is None:185 return image186 if isinstance(resize_method, types.MethodType):187 resize_method = resize_method.__func__188 # 分辨率不一致则进行适配,默认使用cocos_min_strategy:189 h, w = image.shape[:2]190 w_re, h_re = resize_method(w, h, self.resolution, screen_resolution)191 # 确保w_re和h_re > 0, 至少有1个像素:192 w_re, h_re = max(1, w_re), max(1, h_re)193 # 调试代码: 输出调试信息.194 # G.LOGGING.debug("resize: (%s, %s)->(%s, %s), resolution: %s=>%s" % (195 # w, h, w_re, h_re, self.resolution, screen_resolution))196 # 进行图片缩放:197 image = cv2.resize(image, (w_re, h_re))198 return image199class Predictor(object):200 """201 this class predicts the press_point and the area to search im_search.202 """203 RADIUS_X = 250204 RADIUS_Y = 250205 @staticmethod206 def count_record_pos(pos, resolution):207 """计算坐标对应的中点偏移值相对于分辨率的百分比"""208 _w, _h = resolution209 # 都按宽度缩放,针对G18的实验结论210 delta_x = (pos[0] - _w * 0.5) / _w211 delta_y = (pos[1] - _h * 0.5) / _w212 delta_x = round(delta_x, 3)213 delta_y = round(delta_y, 3)214 return delta_x, delta_y215 @classmethod216 def get_predict_point(cls, record_pos, screen_resolution):217 """预测缩放后的点击位置点"""218 delta_x, delta_y = record_pos219 _w, _h = screen_resolution220 target_x = delta_x * _w + _w * 0.5221 target_y = delta_y * _w + _h * 0.5222 return target_x, target_y223 @classmethod224 def get_predict_area(cls, record_pos, screen_resolution):225 x, y = cls.get_predict_point(record_pos, screen_resolution)226 area = (x - cls.RADIUS_X, y - cls.RADIUS_Y, x + cls.RADIUS_X, y + cls.RADIUS_Y)...

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?