Best Python code snippet using Airtest
multiscale_template_matching.py
Source:multiscale_template_matching.py
...90 tr = (w*ratio)/tw91 templ = cv2.resize(templ, (max(int(tw*tr), 1), max(int(th*tr), 1)))92 return src, templ, tr, sr93 @staticmethod94 def _org_size(max_loc, w, h, tr, sr):95 """è·ååå§æ¯ä¾çæ¡"""96 max_loc = (int((max_loc[0]/sr)), int((max_loc[1]/sr)))97 w, h = int((w/sr)), int((h/sr))98 return max_loc, w, h99 def multi_scale_search(self, org_src, org_templ, templ_min=10, src_max=800, ratio_min=0.01, ratio_max=0.99, step=0.01, threshold=0.8):100 """å¤å°ºåº¦æ¨¡æ¿å¹é
"""101 mmax_val = 0102 max_info = None103 r = ratio_min104 while r <= ratio_max:105 src, templ, tr, sr = self._resize_by_ratio(106 org_src.copy(), org_templ.copy(), r, src_max=src_max)107 if min(templ.shape) > templ_min:108 src[0,0] = templ[0,0] = 0109 src[0,1] = templ[0,1] = 255110 result = cv2.matchTemplate(src, templ, cv2.TM_CCOEFF_NORMED)111 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)112 h, w = templ.shape113 if mmax_val < max_val:114 mmax_val = max_val115 max_info = (r, max_val, max_loc, w, h, tr, sr)116 # print((r, max_val, max_loc, w, h, tr, sr))117 if max_val >= threshold:118 omax_loc, ow, oh = self._org_size(max_loc, w, h, tr, sr)119 confidence = self._get_confidence_from_matrix(omax_loc, ow, oh)120 if confidence >= threshold:121 return confidence, omax_loc, ow, oh, r122 r += step123 if max_info is None:124 return 0, (0, 0), 0, 0, 0125 max_r, max_val, max_loc, w, h, tr, sr = max_info126 omax_loc, ow, oh = self._org_size(max_loc, w, h, tr, sr)127 confidence = self._get_confidence_from_matrix(omax_loc, ow, oh)128 return confidence, omax_loc, ow, oh, max_r129class MultiScaleTemplateMatchingPre(MultiScaleTemplateMatching):130 """åºäºæªå¾é¢è®¾æ¡ä»¶çå¤å°ºåº¦æ¨¡æ¿å¹é
."""131 METHOD_NAME = "MSTemplatePre"132 DEVIATION = 150133 @print_run_time134 def find_best_result(self):135 """å½æ°åè½ï¼æ¾å°æä¼ç»æ."""136 if self.resolution!=():137 # 第ä¸æ¥ï¼æ ¡éªå¾åè¾å
¥138 check_source_larger_than_search(self.im_source, self.im_search)139 if self.resolution[0]<self.im_search.shape[1] or self.resolution[1]<self.im_search.shape[0]:140 raise TemplateInputError("error: resolution is too small.")...
croppergui_support.py
Source:croppergui_support.py
1#! /usr/bin/env python2# -*- coding: utf-8 -*-3#4# Support module generated by PAGE version 6.05# in conjunction with Tcl version 8.66# Jan 14, 2021 11:47:32 AM EET platform: Linux7# Jan 16, 2021 05:07:26 PM EET platform: Linux8import sys9import os10import glob11from PIL import Image, ImageTk12from cropperhandler import CropperHandler13try:14 import Tkinter as tk15except ImportError:16 import tkinter as tk17try:18 import ttk19 py3 = False20except ImportError:21 import tkinter.ttk as ttk22 py3 = True23# picture class and original crop coordinates24_crop_handler: CropperHandler25# canvas rectangle handle26_rect = None27# files to process28_pic_files = None29def init(top, gui, *args, **kwargs):30 global w, top_level, root31 global _pic_files32 w = gui33 top_level = top34 root = top35 # own inits36 _folder = os.path.abspath(".")37 w.btn_working_folder.configure(text=_folder)38 _pic_files = get_pic_files(_folder)39def get_pic_files(_path):40 # get all pic files41 _infiles = glob.glob(_path + "/*.jpg")42 for i, val in enumerate(_infiles):43 _infiles[i] = os.path.basename(_infiles[i])44 return _infiles45def a_btnCrop(p1):46 print("croppergui_support.a_btnCrop")47 _crop_handler.save_crop(_crop_handler._limits)48 # take next pic49 a_btnNext(p1)50 sys.stdout.flush()51# scale cropping to canvas52def _calc_crop_lines(cropper: CropperHandler, org_coords):53 _clines = [0] * 454 _org_size = cropper.get_image().size55 _x_scale = w.canvPhoto.winfo_width() / _org_size[0]56 _y_scale = w.canvPhoto.winfo_height() / _org_size[1]57 _clines[0] = org_coords[0] * _x_scale58 _clines[1] = org_coords[2] * _y_scale59 _clines[2] = org_coords[1] * _x_scale60 _clines[3] = org_coords[3] * _y_scale61 return _clines62def a_btnNext(p1):63 global _rect64 global _crop_handler65 global _pic_files66 print("croppergui_support.a_btnNext")67 sys.stdout.flush()68 if len(_pic_files) == 0:69 w.canvPhoto.delete("all")70 return71 _crop_handler = CropperHandler(_pic_files[0])72 del _pic_files[0]73 _crop_handler_coords = _crop_handler.find_edges()74 # load the image file and fit it to canvas75 ima = _crop_handler.get_image().resize(76 (w.canvPhoto.winfo_width(), w.canvPhoto.winfo_height()), Image.BILINEAR77 )78 photo = ImageTk.PhotoImage(ima)79 # put image on canvas80 w.canvPhoto.imageList = []81 id = w.canvPhoto.create_image(0, 0, image=photo, anchor=tk.NW)82 w.canvPhoto.imageList.append(photo)83 # crop lines to canvas84 _crop_lines = _calc_crop_lines(_crop_handler, _crop_handler_coords)85 _rect = w.canvPhoto.create_rectangle(86 _crop_lines[0],87 _crop_lines[1],88 _crop_lines[2],89 _crop_lines[3],90 outline="white",91 dash="2 4",92 )93def a_canv_mousewheel(p1):94 print("croppergui_support.a_canv_mousewheel")95 sys.stdout.flush()96# define on what section the pointer is on pic (use 20% rule)97def get_side(px):98 _side = "center"99 _add_crop = 1 # change magnitude (+/-) depends by edge100 _xcur = px.x # cursor pos101 _ycur = px.y102 _cw = w.canvPhoto.winfo_width() # canvas size103 _ch = w.canvPhoto.winfo_height()104 # define on which region the pointer is105 if _xcur < _cw * 0.20:106 _side = "left"107 elif _xcur > _cw * 0.80:108 _side = "right"109 _add_crop = -1110 elif _ycur < _ch * 0.20:111 _side = "up"112 elif _ycur > _ch * 0.80:113 _side = "down"114 _add_crop = -1115 return _side, _add_crop116def a_canv_scroll_down(p1):117 global _rect118 global _crop_handler119 # delete prev crop120 w.canvPhoto.delete(_rect)121 # add crop lines122 _side, _add = get_side(p1)123 _clines = _crop_handler.change_edge(_side, -10 * _add)124 _crop_lines = _calc_crop_lines(_crop_handler, _clines)125 _rect = w.canvPhoto.create_rectangle(126 _crop_lines[0],127 _crop_lines[1],128 _crop_lines[2],129 _crop_lines[3],130 outline="white",131 dash="2 4",132 )133 print("croppergui_support.a_canv_scroll_down")134 sys.stdout.flush()135def a_canv_scroll_up(p1):136 global _rect137 global _crop_handler138 # delete prev crop139 w.canvPhoto.delete(_rect)140 # add crop lines141 _side, _add = get_side(p1)142 _clines = _crop_handler.change_edge(_side, +10 * _add)143 _crop_lines = _calc_crop_lines(_crop_handler, _clines)144 _rect = w.canvPhoto.create_rectangle(145 _crop_lines[0],146 _crop_lines[1],147 _crop_lines[2],148 _crop_lines[3],149 outline="white",150 dash="2 4",151 )152 print("croppergui_support.a_canv_scroll_up")153 sys.stdout.flush()154def a_btn_working_folder(p1):155 print("croppergui_support.a_btn_working_folder")156 sys.stdout.flush()157def destroy_window():158 # Function which closes the window.159 global top_level160 top_level.destroy()161 top_level = None162if __name__ == "__main__":163 import croppergui...
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!!