Best Python code snippet using Airtest
template.py
Source:template.py
...19 h, w = im_search.shape[:2]20 # æ±åå¯ä¿¡åº¦:21 confidence = _get_confidence_from_matrix(im_source, im_search, max_loc, max_val, w, h, rgb)22 # æ±åè¯å«ä½ç½®: ç®æ ä¸å¿ + ç®æ åºå:23 # middle_point, rectangle = _get_target_rectangle(max_loc, w, h)24 # best_match = generate_result(middle_point, rectangle, confidence)25 # LOGGING.debug("threshold=%s, result=%s" % (threshold, best_match))26 # return best_match if confidence >= threshold else None27 print confidence28 return True if confidence >= threshold else None29def find_all_template(im_source, im_search, threshold=0.8, rgb=False, max_count=10):30 """æ ¹æ®è¾å
¥å¾çååæ°è®¾ç½®,è¿åææçå¾åè¯å«ç»æ."""31 # 第ä¸æ¥ï¼æ ¡éªå¾åè¾å
¥32 check_source_larger_than_search(im_source, im_search)33 # 第äºæ¥ï¼è®¡ç®æ¨¡æ¿å¹é
çç»æç©éµres34 res = _get_template_result_matrix(im_source, im_search)35 # 第ä¸æ¥ï¼ä¾æ¬¡è·åå¹é
ç»æ36 result = []37 h, w = im_search.shape[:2]38 while True:39 # æ¬æ¬¡å¾ªç¯ä¸,ååºå½åç»æç©éµä¸çæä¼å¼40 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)41 # æ±åå¯ä¿¡åº¦:42 confidence = _get_confidence_from_matrix(im_source, im_search, max_loc, max_val, w, h, rgb)43 if confidence < threshold or len(result) > max_count:44 break45 # æ±åè¯å«ä½ç½®: ç®æ ä¸å¿ + ç®æ åºå:46 middle_point, rectangle = _get_target_rectangle(max_loc, w, h)47 one_good_match = generate_result(middle_point, rectangle, confidence)48 result.append(one_good_match)49 # å±è½å·²ç»ååºçæä¼ç»æ,è¿å
¥ä¸è½®å¾ªç¯ç»§ç»å¯»æ¾:50 cv2.floodFill(res, None, max_loc, (-1000,), max(max_val, 0), flags=cv2.FLOODFILL_FIXED_RANGE)51 return result if result else None52def _get_confidence_from_matrix(im_source, im_search, max_loc, max_val, w, h, rgb):53 """æ ¹æ®ç»æç©éµæ±åºconfidence."""54 # æ±åå¯ä¿¡åº¦:55 if rgb:56 # å¦ææé¢è²æ ¡éª,对ç®æ åºåè¿è¡BGRä¸ééæ ¡éª:57 img_crop = im_source[max_loc[1]:max_loc[1] + h, max_loc[0]: max_loc[0] + w]58 confidence = cal_rgb_confidence(img_crop, im_search)59 else:60 confidence = max_val61 return confidence62def _get_template_result_matrix(im_source, im_search):63 """æ±å模æ¿å¹é
çç»æç©éµ."""64 # ç°åº¦è¯å«: cv2.matchTemplate( )åªè½å¤çç°åº¦å¾çåæ°65 s_gray, i_gray = img_mat_rgb_2_gray(im_search), img_mat_rgb_2_gray(im_source)66 return cv2.matchTemplate(i_gray, s_gray, cv2.TM_CCOEFF_NORMED)67def _get_target_rectangle(left_top_pos, w, h):68 """æ ¹æ®å·¦ä¸è§ç¹å宽é«æ±åºç®æ åºå."""69 x_min, y_min = left_top_pos70 # ä¸å¿ä½ç½®çåæ :71 x_middle, y_middle = int(x_min + w / 2), int(y_min + h / 2)72 # å·¦ä¸(min,max)->å³ä¸(max,max)->å³ä¸(max,min)73 left_bottom_pos, right_bottom_pos = (x_min, y_min + h), (x_min + w, y_min + h)74 right_top_pos = (x_min + w, y_min)75 # ç¹å»ä½ç½®:76 middle_point = (x_middle, y_middle)77 # è¯å«ç®æ åºå: ç¹åº:å·¦ä¸->å·¦ä¸->å³ä¸->å³ä¸, å·¦ä¸(min,min)å³ä¸(max,max)78 rectangle = (left_top_pos, left_bottom_pos, right_bottom_pos, right_top_pos)...
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!!