How to use _get_origin_result_with_two_points method in Airtest

Best Python code snippet using Airtest

keypoint_base.py

Source:keypoint_base.py Github

copy

Full Screen

...139 pts_sch1 = int(kp_sch[good[0].queryIdx].pt[0]), int(kp_sch[good[0].queryIdx].pt[1])140 pts_sch2 = int(kp_sch[good[1].queryIdx].pt[0]), int(kp_sch[good[1].queryIdx].pt[1])141 pts_src1 = int(kp_src[good[0].trainIdx].pt[0]), int(kp_src[good[0].trainIdx].pt[1])142 pts_src2 = int(kp_src[good[1].trainIdx].pt[0]), int(kp_src[good[1].trainIdx].pt[1])143 return self._get_origin_result_with_two_points(pts_sch1, pts_sch2, pts_src1, pts_src2)144 def _handle_three_good_points(self, kp_sch, kp_src, good):145 """处理三对特征点的情况."""146 # 拿出sch和src的两个点(点1)和(点2点3的中点),147 # 然后根据两个点原则进行后处理(注意ke_sch和kp_src以及queryIdx和trainIdx):148 pts_sch1 = int(kp_sch[good[0].queryIdx].pt[0]), int(kp_sch[good[0].queryIdx].pt[1])149 pts_sch2 = int((kp_sch[good[1].queryIdx].pt[0] + kp_sch[good[2].queryIdx].pt[0]) / 2), int(150 (kp_sch[good[1].queryIdx].pt[1] + kp_sch[good[2].queryIdx].pt[1]) / 2)151 pts_src1 = int(kp_src[good[0].trainIdx].pt[0]), int(kp_src[good[0].trainIdx].pt[1])152 pts_src2 = int((kp_src[good[1].trainIdx].pt[0] + kp_src[good[2].trainIdx].pt[0]) / 2), int(153 (kp_src[good[1].trainIdx].pt[1] + kp_src[good[2].trainIdx].pt[1]) / 2)154 return self._get_origin_result_with_two_points(pts_sch1, pts_sch2, pts_src1, pts_src2)155 def _many_good_pts(self, kp_sch, kp_src, good):156 """特征点匹配点对数目>=4个,可使用单矩阵映射,求出识别的目标区域."""157 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in good]).reshape(158 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)159 # M是转化矩阵160 M, mask = self._find_homography(sch_pts, img_pts)161 matches_mask = mask.ravel().tolist()162 # 从good中间筛选出更精确的点(假设good中大部分点为正确的,由ratio=0.7保障)163 selected = [v for k, v in enumerate(good) if matches_mask[k]]164 # 针对所有的selected点再次计算出更精确的转化矩阵M来165 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in selected]).reshape(166 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in selected]).reshape(-1, 1, 2)167 M, mask = self._find_homography(sch_pts, img_pts)168 # 计算四个角矩阵变换后的坐标,也就是在大图中的目标区域的顶点坐标:169 h, w = self.im_search.shape[:2]170 h_s, w_s = self.im_source.shape[:2]171 pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)172 dst = cv2.perspectiveTransform(pts, M)173 # trans numpy arrary to python list: [(a, b), (a1, b1), ...]174 def cal_rect_pts(dst):175 return [tuple(npt[0]) for npt in dst.astype(int).tolist()]176 pypts = cal_rect_pts(dst)177 # 注意:虽然4个角点有可能越出source图边界,但是(根据精确化映射单映射矩阵M线性机制)中点不会越出边界178 lt, br = pypts[0], pypts[2]179 middle_point = int((lt[0] + br[0]) / 2), int((lt[1] + br[1]) / 2)180 # 考虑到算出的目标矩阵有可能是翻转的情况,必须进行一次处理,确保映射后的“左上角”在图片中也是左上角点:181 x_min, x_max = min(lt[0], br[0]), max(lt[0], br[0])182 y_min, y_max = min(lt[1], br[1]), max(lt[1], br[1])183 # 挑选出目标矩形区域可能会有越界情况,越界时直接将其置为边界:184 # 超出左边界取0,超出右边界取w_s-1,超出下边界取0,超出上边界取h_s-1185 # 当x_min小于0时,取0。 x_max小于0时,取0。186 x_min, x_max = int(max(x_min, 0)), int(max(x_max, 0))187 # 当x_min大于w_s时,取值w_s-1。 x_max大于w_s-1时,取w_s-1。188 x_min, x_max = int(min(x_min, w_s - 1)), int(min(x_max, w_s - 1))189 # 当y_min小于0时,取0。 y_max小于0时,取0。190 y_min, y_max = int(max(y_min, 0)), int(max(y_max, 0))191 # 当y_min大于h_s时,取值h_s-1。 y_max大于h_s-1时,取h_s-1。192 y_min, y_max = int(min(y_min, h_s - 1)), int(min(y_max, h_s - 1))193 # 目标区域的角点,按左上、左下、右下、右上点序:(x_min,y_min)(x_min,y_max)(x_max,y_max)(x_max,y_min)194 pts = np.float32([[x_min, y_min], [x_min, y_max], [195 x_max, y_max], [x_max, y_min]]).reshape(-1, 1, 2)196 pypts = cal_rect_pts(pts)197 return middle_point, pypts, [x_min, x_max, y_min, y_max, w, h]198 def _get_origin_result_with_two_points(self, pts_sch1, pts_sch2, pts_src1, pts_src2):199 """返回两对有效匹配特征点情形下的识别结果."""200 # 先算出中心点(在self.im_source中的坐标):201 middle_point = [int((pts_src1[0] + pts_src2[0]) / 2), int((pts_src1[1] + pts_src2[1]) / 2)]202 pypts = []203 # 如果特征点同x轴或同y轴(无论src还是sch中),均不能计算出目标矩形区域来,此时返回值同good=1情形204 if pts_sch1[0] == pts_sch2[0] or pts_sch1[1] == pts_sch2[1] or pts_src1[0] == pts_src2[0] or pts_src1[1] == pts_src2[1]:205 return None206 # 计算x,y轴的缩放比例:x_scale、y_scale,从middle点扩张出目标区域:(注意整数计算要转成浮点数结果!)207 h, w = self.im_search.shape[:2]208 h_s, w_s = self.im_source.shape[:2]209 x_scale = abs(1.0 * (pts_src2[0] - pts_src1[0]) / (pts_sch2[0] - pts_sch1[0]))210 y_scale = abs(1.0 * (pts_src2[1] - pts_src1[1]) / (pts_sch2[1] - pts_sch1[1]))211 # 得到scale后需要对middle_point进行校正,并非特征点中点,而是映射矩阵的中点。212 sch_middle_point = int((pts_sch1[0] + pts_sch2[0]) / 2), int((pts_sch1[1] + pts_sch2[1]) / 2)...

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:

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