Best Python code snippet using Airtest
sift_test.py
Source:sift_test.py
...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 # èèå°ç®åºçç®æ ç©éµæå¯è½æ¯ç¿»è½¬çæ
åµï¼å¿
é¡»è¿è¡ä¸æ¬¡å¤çï¼ç¡®ä¿æ å°åçâå·¦ä¸è§âå¨å¾çä¸ä¹æ¯å·¦ä¸è§ç¹ï¼88 x_min, x_max = min(lt[0], br[0]), max(lt[0], br[0])89 y_min, y_max = min(lt[1], br[1]), max(lt[1], br[1])90 # æéåºç®æ ç©å½¢åºåå¯è½ä¼æè¶çæ
åµï¼è¶çæ¶ç´æ¥å°å
¶ç½®ä¸ºè¾¹çï¼91 # è¶
åºå·¦è¾¹çå0ï¼è¶
åºå³è¾¹çåw_s-1ï¼è¶
åºä¸è¾¹çå0ï¼è¶
åºä¸è¾¹çåh_s-192 # å½x_minå°äº0æ¶ï¼å0ã x_maxå°äº0æ¶ï¼å0ã93 x_min, x_max = int(max(x_min, 0)), int(max(x_max, 0))94 # å½x_min大äºw_sæ¶ï¼åå¼w_s-1ã x_max大äºw_s-1æ¶ï¼åw_s-1ã95 x_min, x_max = int(min(x_min, w_s - 1)), int(min(x_max, w_s - 1))96 # å½y_minå°äº0æ¶ï¼å0ã y_maxå°äº0æ¶ï¼å0ã97 y_min, y_max = int(max(y_min, 0)), int(max(y_max, 0))98 # å½y_min大äºh_sæ¶ï¼åå¼h_s-1ã y_max大äºh_s-1æ¶ï¼åh_s-1ã99 y_min, y_max = int(min(y_min, h_s - 1)), int(min(y_max, h_s - 1))100 # ç®æ åºåçè§ç¹ï¼æå·¦ä¸ãå·¦ä¸ãå³ä¸ãå³ä¸ç¹åºï¼(x_min,y_min)(x_min,y_max)(x_max,y_max)(x_max,y_min)101 pts = np.float32([[x_min, y_min], [x_min, y_max], [102 x_max, y_max], [x_max, y_min]]).reshape(-1, 1, 2)103 pypts = cal_rect_pts(pts)104 return middle_point, pypts, [x_min, x_max, y_min, y_max, w, h]105# å¹é
ç¹å¯¹ >= 4个ï¼ä½¿ç¨åç©éµæ å°æ±åºç®æ åºåï¼æ®æ¤ç®åºå¯ä¿¡åº¦ï¼106middle_point, pypts, w_h_range = _many_good_pts(im_source, im_search, kp_sch, kp_src, good)107print(middle_point)108print(pypts)109print(w_h_range)110# best_match = generate_result(middle_point, pypts, confidence)111#112# print("[sift] result=%s" % (best_match))113# matchesMask = [[0, 0] for i in range(len(matches))]114# coff = 0.2115# for i,(m,n) in enumerate(matches):116# if m.distance < coff * n.distance:117# matchesMask[i]=[1,0]118#119# print(matchesMask)120# draw_params = dict(matchColor = (0,255,0),...
sift.py
Source:sift.py
...30 else:31 middle_point, pypts, w_h_range = _handle_three_good_points(im_source, im_search, kp_src, kp_sch, good)32 else:33 # å¹é
ç¹å¯¹ >= 4个ï¼ä½¿ç¨åç©éµæ å°æ±åºç®æ åºåï¼æ®æ¤ç®åºå¯ä¿¡åº¦ï¼34 middle_point, pypts, w_h_range = _many_good_pts(im_source, im_search, kp_sch, kp_src, good)35 # 第åæ¥ï¼æ ¹æ®è¯å«åºåï¼æ±åºç»æå¯ä¿¡åº¦ï¼å¹¶å°ç»æè¿è¡è¿å:36 # 对è¯å«ç»æè¿è¡åçæ§æ ¡éª: å°äº5个åç´ çï¼æè
缩æ¾è¶
è¿5åçï¼ä¸å¾è§ä¸ºä¸åæ³ç´æ¥raise.37 _target_error_check(w_h_range)38 # å°æªå¾åè¯å«ç»æ缩æ¾å°å¤§å°ä¸è´,åå¤è®¡ç®å¯ä¿¡åº¦39 x_min, x_max, y_min, y_max, w, h = w_h_range40 target_img = im_source[y_min:y_max, x_min:x_max]41 resize_img = cv2.resize(target_img, (w, h))42 confidence = _cal_sift_confidence(im_search, resize_img, rgb=rgb)43 best_match = generate_result(middle_point, pypts, confidence)44 print("[aircv][sift] threshold=%s, result=%s" % (threshold, best_match))45 return best_match if confidence >= threshold else None46def _get_key_points(im_source, im_search, good_ratio):47 """æ ¹æ®ä¼ å
¥å¾å,计ç®å¾åææçç¹å¾ç¹,并å¾å°å¹é
ç¹å¾ç¹å¯¹."""48 # åå¤å·¥ä½: åå§åsiftç®å...
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!!