Files
2025-10-20 12:32:18 +08:00

13 KiB

In [2]:
from airtest.core import api as airtest
from airtest.aircv import *
import time
import datetime
import  logging
import re 
from orc import orc  
import os
logger = logging.getLogger("airtest")
logger2 = logging.getLogger("ppocr")
logger.setLevel(logging.WARNING)
logger2.setLevel(logging.WARNING)
times = time.localtime(time.time())
week=datetime.datetime(times.tm_year,times.tm_mon,times.tm_mday).weekday()
day = times.tm_mday
In [3]:
info = True

os.system("adb devices")
Out [3]:
0
In [14]:
def c():
    airtest.connect_device("Android://127.0.0.1:5037/192.168.0.218:5555")
c()
[19:08:42][WARNING]<airtest.core.api> Device:192.168.0.218:5555 updated <airtest.core.android.android.Android object at 0x000002327665F440> -> <airtest.core.android.android.Android object at 0x0000023276A82150>
In [6]:
threshold = 0.92
threshold1= 0.75
def t(path=False):
    if not path:
        airtest.snapshot(filename="demo.png",quality=90)
    else:
        airtest.snapshot(filename=f"./test/imgs/{path}.png",quality=90)
def l(flag):
    if flag: logger.setLevel(logging.DEBUG)
    else:logger.setLevel(logging.WARNING)
def log(information):
    info and print(information)
def local(v1,v2,paddle=True):
    local = aircv.crop_image(airtest.G.DEVICE.snapshot(),(v1[0],v1[1],v2[0],v2[1]))
    pil_image = cv2_2_pil(local)
    pil_image.save("./test/imgs/demo.png", quality=99, optimize=True)
    if paddle:
        ocr=PaddleOCR(use_angle_cls = True,use_gpu= False) 
        text=ocr.ocr("./test/imgs/demo.png",cls=True)
        text = np.array(text)[0]
        for i in text:
            for k in i[0]:
                k[0]+=v1[0]
                k[1]+=v1[1]
    else:
        text = orc(img='./test/imgs/demo.png')
    return text
def axis(e):
    e = str(e)
    x = int(re.search(r'pos: \((.*), (.*)\), message:',e).group(1))
    y = int(re.search(r'pos: \((.*), (.*)\), message:',e).group(2))
    return (x,y)
def click(path,threshold=threshold,rgb=False,load=0):
    if isinstance(path,bool):return False
    if isinstance(path,str) :
        if info:
            start_time = time.time()
            log(f"检测{path}")
        airtest.touch(airtest.Template(filename=f"./test/imgs/{path}.png",threshold=threshold,rgb=rgb))
        if info:
            end_time = time.time()
            log(f"点击{path},耗时{int((end_time-start_time)*1000)}毫秒")
    else:
        if path[0]<1:
            path = (1080*path[0],2310*path[1])
        airtest.touch(path)
        log(f"点击坐标{path}")
    time.sleep(load)
    return True
def offShow(count=1,load=1.5):
    for i in range(count):
        airtest.touch((258,90))
        log("关闭显示")
        time.sleep(load)
def back(count=1,load=0):
    for i in range(count):
        airtest.touch((72,139))
        log("返回")
        if load==0:
            pass
        else:
            time.sleep(load)
def wait(path,threshold=threshold,rgb=False,timeout=60,interval=1):
    if info:
        start_time = time.time()
        log(f"等待{path}出现")
    loc = airtest.wait(airtest.Template(filename=f"./test/imgs/{path}.png",threshold=threshold,rgb=rgb),timeout=timeout,interval=interval,)
    if info:
        end_time = time.time()
        log(f"已识别{path},耗时{int((end_time-start_time)*1000)}毫秒")
    return loc
def exists(path,threshold=threshold,rgb=False,all=False):
    if info:
        start_time = time.time()
        log(f"检测是否存在{path}")
    if all:
        vs = airtest.find_all(airtest.Template(filename=f"./test/imgs/{path}.png",threshold=threshold,rgb=rgb))
        if vs:
            result = [i["result"] for i in vs]
            if info:
                end_time = time.time()
                log(f"存在{path},坐标{result},耗时{int((end_time-start_time)*1000)}毫秒")
            return  result
        else:
            if info:
                end_time = time.time()
                log(f"不存在{path},耗时{int((end_time-start_time)*1000)}毫秒")
            return False
    if info:
        start_time = time.time()
    loc = airtest.exists(airtest.Template(filename=f"./test/imgs/{path}.png",threshold=threshold,rgb=rgb))
    if info:
        end_time = time.time()
        if loc :log(f"存在{path},坐标{loc},耗时{int((end_time-start_time)*1000)}毫秒")
        else:log(f"不存在{path},耗时{int((end_time-start_time)*1000)}毫秒")
    return loc
def die(path,second = 30,threshold=threshold1,rgb=False):
    if info:
        start_time = time.time()
        log(f"等待{path}消失")
    end_time = datetime.datetime.now() + datetime.timedelta(seconds=second)
    flag = True
    while airtest.exists(airtest.Template(filename=f"./test/imgs/{path}.png",threshold=threshold,rgb=rgb)):
        if datetime.datetime.now() >end_time:
            flag = False
            log(f"检测超时")
            raise "超时"
            break 
        time.sleep(1.5)
    if info:
        end_time = time.time()
        flag  and log(f"{path}已消失,耗时{int((end_time-start_time)*1000)}毫秒")
    return flag
def text(string:str = "",enter:bool=True):
    if info:
        log(f"输入{string}")
    airtest.text(string,enter=enter)
def sort(arr1,dimension):
    if not arr1:
        return False
    arr = arr1[:]
    n = len(arr)
    for i in range(n):
        flag = True
        for j in range(0, n-i-1):
            if arr[j][dimension] > arr[j+1][dimension] :
                arr[j], arr[j+1] = arr[j+1], arr[j]
                flag = False
        if flag:break
    return arr
# 定义一个函数contain,用于判断一个数组arr中是否包含一个数组v,range参数默认值为30
def contain(arr,v,range=30):
    # 如果v为False,则返回False
    if v == False:
        return False
    # 遍历arr中的每一个元素
    for i in arr:
        # 如果i[0]-range小于v[0]小于i[0]+range,且i[1]-range小于v[1]小于i[1]+range,则返回(v[0]-20,v[1]+65)
        if (i[0]-range)<v[0]<(i[0]+range) and i[1]-range<v[1]<i[1]+range:
            return (v[0]-20,v[1]+65)
    # 如果遍历完arr中的每一个元素,没有找到满足条件的元素,则返回False
    return False
In [19]:
t()
In [ ]:
for i in range(200):
    click(wait("桐行飞讯"),load=3.5)
    click("打电话",load=3.5)
    click("微信打电话")
    wait("通话结束")
等待桐行飞讯出现
已识别桐行飞讯,耗时156毫秒
点击坐标(666, 40)
检测打电话
点击打电话,耗时319毫秒
检测微信打电话
点击微信打电话,耗时319毫秒
等待通话结束出现
已识别通话结束,耗时11918毫秒
等待桐行飞讯出现
已识别桐行飞讯,耗时4507毫秒
点击坐标(666, 40)
检测打电话
点击打电话,耗时319毫秒
检测微信打电话
点击微信打电话,耗时293毫秒
等待通话结束出现
已识别通话结束,耗时5926毫秒
等待桐行飞讯出现
已识别桐行飞讯,耗时4489毫秒
点击坐标(666, 40)
检测打电话
点击打电话,耗时321毫秒
检测微信打电话
点击微信打电话,耗时284毫秒
等待通话结束出现
已识别通话结束,耗时4486毫秒
等待桐行飞讯出现
已识别桐行飞讯,耗时4597毫秒
点击坐标(666, 40)
检测打电话
点击打电话,耗时305毫秒
检测微信打电话
点击微信打电话,耗时289毫秒
等待通话结束出现
已识别通话结束,耗时5935毫秒
等待桐行飞讯出现
已识别桐行飞讯,耗时4310毫秒
点击坐标(666, 40)
检测打电话
点击打电话,耗时305毫秒
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[20], line 3
      1 for i in range(200):
      2     click(wait("桐行飞讯"),load=3.5)
----> 3     click("打电话",load=3.5)
      4     click("微信打电话")
      5     wait("通话结束")

Cell In[6], line 48, in click(path, threshold, rgb, load)
     46     airtest.touch(path)
     47     log(f"点击坐标{path}")
---> 48 time.sleep(load)
     49 return True

KeyboardInterrupt: 
在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。

请查看单元格中的代码,以确定故障的可能原因。

单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。

有关更多详细信息,请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。