Best Python code snippet using Airtest
sift.py
Source:sift.py
...134 """ç¹å¾ç¹å¹é
ç¹å¯¹æ°ç®>=4个ï¼å¯ä½¿ç¨åç©éµæ å°,æ±åºè¯å«çç®æ åºå."""135 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in good]).reshape(136 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)137 # Mæ¯è½¬åç©éµ138 M, mask = _find_homography(sch_pts, img_pts)139 matches_mask = mask.ravel().tolist()140 # ä»goodä¸é´çéåºæ´ç²¾ç¡®çç¹(å设goodä¸å¤§é¨åç¹ä¸ºæ£ç¡®çï¼ç±ratio=0.7ä¿é)141 selected = [v for k, v in enumerate(good) if matches_mask[k]]142 # é对ææçselectedç¹å次计ç®åºæ´ç²¾ç¡®ç转åç©éµMæ¥143 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in selected]).reshape(144 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in selected]).reshape(-1, 1, 2)145 M, mask = _find_homography(sch_pts, img_pts)146 # 计ç®å个è§ç©éµåæ¢åçåæ ï¼ä¹å°±æ¯å¨å¤§å¾ä¸çç®æ åºåç顶ç¹åæ :147 h, w = im_search.shape[:2]148 h_s, w_s = im_source.shape[:2]149 pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)150 dst = cv2.perspectiveTransform(pts, M)151 # trans numpy arrary to python list: [(a, b), (a1, b1), ...]152 def cal_rect_pts(dst):153 return [tuple(npt[0]) for npt in dst.astype(int).tolist()]154 pypts = cal_rect_pts(dst)155 # 注æï¼è½ç¶4个è§ç¹æå¯è½è¶åºsourceå¾è¾¹çï¼ä½æ¯(æ ¹æ®ç²¾ç¡®åæ å°åæ å°ç©éµM线æ§æºå¶)ä¸ç¹ä¸ä¼è¶åºè¾¹ç156 lt, br = pypts[0], pypts[2]157 middle_point = int((lt[0] + br[0]) / 2), int((lt[1] + br[1]) / 2)158 # èèå°ç®åºçç®æ ç©éµæå¯è½æ¯ç¿»è½¬çæ
åµï¼å¿
é¡»è¿è¡ä¸æ¬¡å¤çï¼ç¡®ä¿æ å°åçâå·¦ä¸è§âå¨å¾çä¸ä¹æ¯å·¦ä¸è§ç¹ï¼159 x_min, x_max = min(lt[0], br[0]), max(lt[0], br[0])160 y_min, y_max = min(lt[1], br[1]), max(lt[1], br[1])161 # æéåºç®æ ç©å½¢åºåå¯è½ä¼æè¶çæ
åµï¼è¶çæ¶ç´æ¥å°å
¶ç½®ä¸ºè¾¹çï¼162 # è¶
åºå·¦è¾¹çå0ï¼è¶
åºå³è¾¹çåw_s-1ï¼è¶
åºä¸è¾¹çå0ï¼è¶
åºä¸è¾¹çåh_s-1163 # å½x_minå°äº0æ¶ï¼å0ã x_maxå°äº0æ¶ï¼å0ã164 x_min, x_max = int(max(x_min, 0)), int(max(x_max, 0))165 # å½x_min大äºw_sæ¶ï¼åå¼w_s-1ã x_max大äºw_s-1æ¶ï¼åw_s-1ã166 x_min, x_max = int(min(x_min, w_s - 1)), int(min(x_max, w_s - 1))167 # å½y_minå°äº0æ¶ï¼å0ã y_maxå°äº0æ¶ï¼å0ã168 y_min, y_max = int(max(y_min, 0)), int(max(y_max, 0))169 # å½y_min大äºh_sæ¶ï¼åå¼h_s-1ã y_max大äºh_s-1æ¶ï¼åh_s-1ã170 y_min, y_max = int(min(y_min, h_s - 1)), int(min(y_max, h_s - 1))171 # ç®æ åºåçè§ç¹ï¼æå·¦ä¸ãå·¦ä¸ãå³ä¸ãå³ä¸ç¹åºï¼(x_min,y_min)(x_min,y_max)(x_max,y_max)(x_max,y_min)172 pts = np.float32([[x_min, y_min], [x_min, y_max], [173 x_max, y_max], [x_max, y_min]]).reshape(-1, 1, 2)174 pypts = cal_rect_pts(pts)175 return middle_point, pypts, [x_min, x_max, y_min, y_max, w, h]176def _two_good_points(pts_sch1, pts_sch2, pts_src1, pts_src2, im_search, im_source):177 """è¿å两对å¹é
ç¹å¾ç¹æ
å½¢ä¸çè¯å«ç»æ."""178 # å
ç®åºä¸å¿ç¹(å¨im_sourceä¸çåæ )ï¼179 middle_point = [int((pts_src1[0] + pts_src2[0]) / 2), int((pts_src1[1] + pts_src2[1]) / 2)]180 pypts = []181 # å¦æç¹å¾ç¹åxè½´æåyè½´(æ 论srcè¿æ¯schä¸)ï¼åä¸è½è®¡ç®åºç®æ ç©å½¢åºåæ¥ï¼æ¤æ¶è¿åå¼ågood=1æ
å½¢182 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]:183 confidence = ONE_POINT_CONFI184 one_match = generate_result(middle_point, pypts, confidence)185 return one_match186 # 计ç®x,yè½´ç缩æ¾æ¯ä¾ï¼x_scaleãy_scaleï¼ä»middleç¹æ©å¼ åºç®æ åºå:(注ææ´æ°è®¡ç®è¦è½¬ææµ®ç¹æ°ç»æ!)187 h, w = im_search.shape[:2]188 h_s, w_s = im_source.shape[:2]189 x_scale = abs(1.0 * (pts_src2[0] - pts_src1[0]) / (pts_sch2[0] - pts_sch1[0]))190 y_scale = abs(1.0 * (pts_src2[1] - pts_src1[1]) / (pts_sch2[1] - pts_sch1[1]))191 # å¾å°scaleåéè¦å¯¹middle_pointè¿è¡æ ¡æ£ï¼å¹¶éç¹å¾ç¹ä¸ç¹ï¼èæ¯æ å°ç©éµçä¸ç¹ã192 sch_middle_point = int((pts_sch1[0] + pts_sch2[0]) / 2), int((pts_sch1[1] + pts_sch2[1]) / 2)193 middle_point[0] = middle_point[0] - int((sch_middle_point[0] - w / 2) * x_scale)194 middle_point[1] = middle_point[1] - int((sch_middle_point[1] - h / 2) * y_scale)195 middle_point[0] = max(middle_point[0], 0) # è¶
åºå·¦è¾¹çå0 (å¾åå·¦ä¸è§åæ 为0,0)196 middle_point[0] = min(middle_point[0], w_s - 1) # è¶
åºå³è¾¹çåw_s-1197 middle_point[1] = max(middle_point[1], 0) # è¶
åºä¸è¾¹çå0198 middle_point[1] = min(middle_point[1], h_s - 1) # è¶
åºä¸è¾¹çåh_s-1199 # 计ç®åºæ¥rectangleè§ç¹ç顺åºï¼å·¦ä¸è§->å·¦ä¸è§->å³ä¸è§->å³ä¸è§ï¼ 注æï¼æä¸èèå¾ç转å¨200 # è¶
åºå·¦è¾¹çå0, è¶
åºå³è¾¹çåw_s-1, è¶
åºä¸è¾¹çå0, è¶
åºä¸è¾¹çåh_s-1201 x_min, x_max = int(max(middle_point[0] - (w * x_scale) / 2, 0)), int(202 min(middle_point[0] + (w * x_scale) / 2, w_s - 1))203 y_min, y_max = int(max(middle_point[1] - (h * y_scale) / 2, 0)), int(204 min(middle_point[1] + (h * y_scale) / 2, h_s - 1))205 # ç®æ ç©å½¢çè§ç¹æå·¦ä¸ãå·¦ä¸ãå³ä¸ãå³ä¸çç¹åºï¼(x_min,y_min)(x_min,y_max)(x_max,y_max)(x_max,y_min)206 pts = np.float32([[x_min, y_min], [x_min, y_max], [x_max, y_max], [x_max, y_min]]).reshape(-1, 1, 2)207 for npt in pts.astype(int).tolist():208 pypts.append(tuple(npt[0]))209 return middle_point, pypts, [x_min, x_max, y_min, y_max, w, h]210def _find_homography(sch_pts, src_pts):211 """å¤ç»ç¹å¾ç¹å¯¹æ¶ï¼æ±åååæ§ç©éµ."""212 try:213 M, mask = cv2.findHomography(sch_pts, src_pts, cv2.RANSAC, 5.0)214 except Exception:215 import traceback216 traceback.print_exc()217 raise HomographyError("OpenCV error in _find_homography()...")218 else:219 if mask is None:220 raise HomographyError("In _find_homography(), find no mask...")221 else:222 return M, mask223def _target_error_check(w_h_range):224 """æ ¡éªè¯å«ç»æåºåæ¯å¦ç¬¦å常ç."""225 x_min, x_max, y_min, y_max, w, h = w_h_range226 tar_width, tar_height = x_max - x_min, y_max - y_min227 # å¦æsrc_imgä¸çç©å½¢è¯å«åºåç宽åé«çåç´ æ°ï¼5ï¼åå¤å®è¯å«å¤±æã认为æååºåå¾
ä¸å¯è½å°äº5个åç´ ã(æªå¾ä¸è¬ä¸å¯è½å°äº5åç´ )228 if tar_width < 5 or tar_height < 5:229 raise SiftResultCheckError("In src_image, Taget area: width or height < 5 pixel.")230 # å¦æç©å½¢è¯å«åºåç宽åé«ï¼ä¸sch_imgç宽é«å·®è·è¶
è¿5å(å±å¹åç´ å·®ä¸å¯è½æ5å)ï¼è®¤å®ä¸ºè¯å«é误ã231 if tar_width < 0.2 * w or tar_width > 5 * w or tar_height < 0.2 * h or tar_height > 5 * h:232 raise SiftResultCheckError("Target area is 5 times bigger or 0.2 times smaller than sch_img.")233def _cal_sift_confidence(im_search, resize_img, rgb=False):234 if rgb:...
sift_test.py
Source:sift_test.py
...44good = good_diff45# print(good)46# for i in good:47# print(i.distance)48def _find_homography(sch_pts, src_pts):49 """å¤ç»ç¹å¾ç¹å¯¹æ¶ï¼æ±åååæ§ç©éµ."""50 try:51 M, mask = cv2.findHomography(sch_pts, src_pts, cv2.RANSAC, 5.0)52 except Exception:53 import traceback54 traceback.print_exc()55 raise Exception("OpenCV error in _find_homography()...")56 else:57 if mask is None:58 raise Exception("In _find_homography(), find no mask...")59 else:60 return M, mask61def _many_good_pts(im_source, im_search, kp_sch, kp_src, good):62 """ç¹å¾ç¹å¹é
ç¹å¯¹æ°ç®>=4个ï¼å¯ä½¿ç¨åç©éµæ å°,æ±åºè¯å«çç®æ åºå."""63 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in good]).reshape(64 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)65 # Mæ¯è½¬åç©éµ66 M, mask = _find_homography(sch_pts, img_pts)67 matches_mask = mask.ravel().tolist()68 # ä»goodä¸é´çéåºæ´ç²¾ç¡®çç¹(å设goodä¸å¤§é¨åç¹ä¸ºæ£ç¡®çï¼ç±ratio=0.7ä¿é)69 selected = [v for k, v in enumerate(good) if matches_mask[k]]70 # é对ææçselectedç¹å次计ç®åºæ´ç²¾ç¡®ç转åç©éµMæ¥71 sch_pts, img_pts = np.float32([kp_sch[m.queryIdx].pt for m in selected]).reshape(72 -1, 1, 2), np.float32([kp_src[m.trainIdx].pt for m in selected]).reshape(-1, 1, 2)73 M, mask = _find_homography(sch_pts, img_pts)74 # print(M, mask)75 # 计ç®å个è§ç©éµåæ¢åçåæ ï¼ä¹å°±æ¯å¨å¤§å¾ä¸çç®æ åºåç顶ç¹åæ :76 h, w = im_search.shape[:2]77 h_s, w_s = im_source.shape[:2]78 pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)79 dst = cv2.perspectiveTransform(pts, M)80 # trans numpy arrary to python list: [(a, b), (a1, b1), ...]81 def cal_rect_pts(dst):82 return [tuple(npt[0]) for npt in dst.astype(int).tolist()]83 pypts = cal_rect_pts(dst)84 # 注æï¼è½ç¶4个è§ç¹æå¯è½è¶åºsourceå¾è¾¹çï¼ä½æ¯(æ ¹æ®ç²¾ç¡®åæ å°åæ å°ç©éµM线æ§æºå¶)ä¸ç¹ä¸ä¼è¶åºè¾¹ç85 lt, br = pypts[0], pypts[2]86 middle_point = int((lt[0] + br[0]) / 2), int((lt[1] + br[1]) / 2)87 # èèå°ç®åºçç®æ ç©éµæå¯è½æ¯ç¿»è½¬çæ
åµï¼å¿
é¡»è¿è¡ä¸æ¬¡å¤çï¼ç¡®ä¿æ å°åçâå·¦ä¸è§âå¨å¾çä¸ä¹æ¯å·¦ä¸è§ç¹ï¼...
homography.py
Source:homography.py
...6 def __init__(self, matcher: cv2.DescriptorMatcher, neighbours: int = 3):7 self.matcher = matcher8 self.matches: List[cv2.DMatch] = list()9 self.neighbours = neighbours10 def _find_homography(self,11 kp_frame: List[cv2.KeyPoint],12 kp_marker: List[cv2.KeyPoint],13 matches: List[cv2.DMatch],14 threshold: float = 5.001,15 maxiters: int = 10000000) -> Array[float]:16 src_pts = np.float32([kp_marker[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)17 dst_pts = np.float32([kp_frame[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)18 M, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,19 ransacReprojThreshold=threshold, maxIters=maxiters)20 return M21 def __call__(self, 22 kp_frame: List[cv2.KeyPoint],23 kp_marker: List[cv2.KeyPoint],24 des_frame: List[cv2.DMatch],25 des_marker: List[cv2.DMatch],26 n_best: int = 100,27 threshold: float = 5.0) -> Array[float]:28 29 if isinstance(self.matcher, cv2.FlannBasedMatcher):30 self.matches = self.matcher.knnMatch(des_marker, des_frame, k=self.neighbours)31 else:32 self.matches = self.matcher.match(des_marker, des_frame)33 self.matches = sorted(self.matches, key=lambda x: x.distance)[:n_best]...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!