基于图片比对获取坐标,aircv
###
IMGS_PATH这个文件夹用于存放appium获取到的页面截图
PAET_PATH 提前准备好的小截图,按照格式存放,格式如:huawei_meat40_仅使用期间允许.png
import aircv as ac import os, re import shutil import time IMGS_PATH = "/Users/****/tool/python-tool/works/julive_xpt/imgs/" PAET_PATH = "/Users/***/tool/python-tool/works/julive_xpt/part_img/" class Service: def __init__(self, log, driver): self.log = log self._driver = driver def getImgCoordinate(self, part_name, confidencevalue=0.5) -> list: """ 根据关键字获取在页面上的坐标 :param part_name: 关键字名字 :param confidencevalue: 图片相似度,默认0.5,相似度越大,匹配的越精确,需要手动调节 :return: 如果获取到了,返回xy坐标,否则返回空列表 """ self.is_file_existence() img_path = self.setImgName() self._driver.get_screenshot_as_file(img_path) phone_model = self.get_device_frim() find_img = self.find_part_file(part_name, phone_model) # 查找三次,每次相似度会-0.1 num = 0 while num < 3: imsrc = ac.imread(img_path) imobj = ac.imread(find_img) match_result = ac.find_template(imsrc, imobj, confidencevalue) if match_result != None: x1, y1 = match_result['result'] return [(x1, y1)] if confidencevalue > 0.1: confidencevalue -= 0.1 else: num = 4 num += 1 return [] def setImgName(self): """ 设置照片名字 :return: """ x = self._driver.get_window_size()['height'] y = self._driver.get_window_size()['width'] file_name = IMG_COORDINATE_PATH + str(time.time()) + "-" + str(x) + "_" + str(y) + ".png" return file_name def is_file_existence(self): """ 判断文件夹内是否有内容并删除 :return: """ self.log.info("检查是否存在imgs文件夹") if os.path.exists(IMGS_PATH): self.log.info("开始删除文件") shutil.rmtree(IMGS_PATH) self.log.info("创建文件") os.mkdir(IMGS_PATH) else: self.log.info("没找到imgs文件夹,开始创建") os.mkdir(IMGS_PATH) self.log.info("文件夹初始化成功") def find_part_file(self, part_name, phone_model): """ 根据关键字查找图片库里的素材,如果分辨率匹配到,则使用,否则,用最小的 :param part_name: 关键字名字 :param phone_model: 厂商 :return: print(find_part_file("同意并继续", "apple")) : os.walk root 文件本身的地址,dirs当前目录下的所有文件夹,files当前目录下的所有文件名 """ for root, dirs, files in os.walk(PART_PATH): for i in files: # 如果厂商名字在文件名里 self.log.info("11111111", phone_model, i) if phone_model in i: # 如果关键字在文件夹里 self.log.info("22222222", part_name, i) if part_name in i: regular_temp = re.compile("{}_.*_{}.*".format(phone_model, part_name)) regular_result = regular_temp.search(i) # 如果没匹配到,只匹配关键字,取最小的 if regular_result != None: return IMG_PART_PATH + regular_result.group() else: regular_temp = re.compile(".*_{}.*".format(part_name)) regular_result = regular_temp.search(i) if regular_result != None: return IMG_PART_PATH + regular_result.group() else: # 排序,用最小的 if part_name in i: regular_temp = re.compile(".*_{}.*".format(part_name)) regular_result = regular_temp.search(i) if regular_result != None: return IMG_PART_PATH + regular_result.group() def get_device_frim(self): """获取手机厂商""" """ ro.product.manufacturer ro.product.vendor.brand ro.product.system.brand """ info = os.popen("adb shell getprop ro.product.manufacturer").read() print(info) if len(info) == 1: info = os.popen("adb shell getprop ro.product.vendor.brand").read() return str(info).replace(" ", "").lower()
学习最大的乐趣在于分享,我是绝世老中医,欢迎来诊
个人qq:1978529954