ADB操作手机,OPENCV检测异常

之前发布过adb操作手机。总是出现一些异常退出。于是粗略研究了一下opencv,用opencv“掌眼”,确保每一步都正常以后再操作,防止异常退出。

上代码。

import os, sys
import time
import datetime as dt
import random as rd

# linux 下sudo dnf install python3-opencv
# win 下 pip install python3-opencv

import cv2

import numpy as np

#执行程序前先清空图片
#os.remove("/home/olxxxxx/screencap*.png") 
os.system("adb shell rm /sdcard/screencap*.png")
time.sleep(3)

def input_tap(a, b):
    #十六进制转换为10进制。因为adbgetevent得到的坐标就是十六进制的
    #print(a, b)
    x = int(str(a), 16)    
    y = int(str(b), 16)
    #print(x, y)
    os.system("adb shell input tap {} {}".format(x, y))

def input_text(a):
    os.system("adb shell input text {}".format(a))

def input_back():
    os.system("adb shell input keyevent BACK")

def back_ward():
    input_back()
    time.sleep(1)
    input_back()
    time.sleep(1)
    input_back()
    time.sleep(1)
    input_back()

def input_enter():
    os.system("adb shell input keyevent 28")

def compare_img(std, ocr):  #比较图片
    std_img1 = cv2.imread(std, 0)    #模板图片
    ocr_img1= cv2.imread(ocr, 0)   #截图

    std_img2 = np.float32(std_img1)
    ocr_img2 = np.float32(ocr_img1)

    std_img3 = np.ndarray.flatten(std_img2)
    ocr_img3 = np.ndarray.flatten(ocr_img2)

    img_ocr = np.corrcoef(std_img3, ocr_img3)
    print(img_ocr[0, 1])
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
            f.write(str(img_ocr[0, 1]))            
            f.write("\n")
    m = img_ocr[0, 1]
    m = round(m, 4)
    return m
    
def scr_cap_md(i):    #截取模板图片
    os.system("adb shell screencap -p /sdcard/screencap_model_{}.png".format(str(i)))
    os.system("adb pull /sdcard/screencap_model_{}.png /home/olxxxxx".format(str(i)))
    
def judge_cap_back(j):  #这个用不上了
    tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    m = scr_cap_score(j, tm1)  
    if m >0.9:
        #删除电脑中的截图
        os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
        #删除手机中的截图
        os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))

    else:
        print("出现异常!", k, v)
        with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
            f.write("出现异常! ")
            f.write(k)
            f.write(v)
            f.write("\n")
        input_tap("05d", "092")  #退回主界面
        #break   #结束该次循环

def judge_cap_pause(j, i, r, t):   #截图不符,推迟几秒
    #print(tm1)
    tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    m = scr_cap_score(j, tm1)   

    if m <r and t-1>0:
        #print("第{}次失败".format(p))
        try:
            print("剩{}次".format(t-1))  #这个t是用来控制递归次数的
            judge_cap_pause(j, i, r, t-1)      
        except:
            pass
    
    else:        
        pass
    try:
        os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
    except:
        pass
    try:
        os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
    except:
        pass

        

   

def judge_cap_exit(i):
    #print(tm1)
    tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    m = scr_cap_score(i, tm1)
    if m <0.9:
        sys.exit() 
    else:
        os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
        os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
        pass


def scr_cap_score(i, tm1):  #截图并与模板图片对比
    #global tm1
    
    print("第{}张截图".format(str(i)))
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write("第{}张截图".format(str(i)) + "\n")
    os.system("adb shell screencap -p /sdcard/screencap{}.png".format(tm1))
    os.system("adb pull /sdcard/screencap{}.png /home/olxxxxx".format(tm1))
        
    std = "/home/olxxxxx/screencap_model_{}.png".format(str(i))
    ocr = "/home/olxxxxx/screencap{}.png".format(tm1)
    #print(std, ocr)
    #with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        #f.write(std + "" + ocr + "\n")

    #compare_img(std, ocr)

    m = compare_img(std, ocr)
    #print(m) 
    return m 
    
    

    


ygh_dict = {"3XXXXXXXX":"Dxxxxxxxx",
            "3xxxxxxxx":"yxxxxxxxx",            
            "3xxxxxxxx":"wxxxxxxxx"}


td = dt.datetime.now().strftime("%Y-%m-%d")
with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
    f.write("szyyllrz:" + td + "\n")

def browse():    
    j = rd.randint(0, 10)
    print(100+j)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(str(100+j) + "-\n")
    for i in range(1, 101 + j):
        print(str(i) + "-")
        with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
            f.write(str(i) + "-\n")

        judge_cap_pause(1, 1, 0.9, 6)

        judge_cap_exit(1)

        input_tap("28d", "89a")
        time.sleep(2)

        #scr_cap_score(2)
        judge_cap_pause(2, 1, 0.85, 3) #等2秒


        input_tap("2e3", "5b7")
        time.sleep(2)

        #scr_cap_score(3)  #通过截图对比,监控一下异常
        judge_cap_pause(3, 1, 0.9, 3) #等3秒
        
        input_tap("3ab", "5d6")
        time.sleep(2)

        judge_cap_pause(4, 1, 0.85, 3)

        '''

        tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        m = scr_cap_score(4, tm1)
        if m <0.85:
                
            input_tap("295", "565")
            input_tap("290", "565")
            input_tap("285", "565")
            input_tap("295", "565")
            input_tap("05d", "092")
            time.sleep(1)
            continue #这里用break程序就退出了
        else:
            os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
            os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
            pass    #此处不对则说明用户名密码不对,退回
        '''

        input_tap("0d3", "782")
        time.sleep(2)

        #scr_cap_score(5)
        judge_cap_pause(5, 3, 0.9, 3)
        

        tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        m = scr_cap_score(5, tm1)
        if m <0.9:
            judge_cap_pause(5, 3, 0.9, 3)           
            input_tap("295", "565")
            input_tap("290", "565")
            input_tap("285", "565")
            input_tap("295", "565")
            input_tap("05d", "092")
            time.sleep(1)
            continue 
        else:
            os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
            os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
            pass    #此处不对则说明用户名密码不对,退回

        input_tap("05d", "092")
            
        time.sleep(1)
        
        print("-" + str(i))
        tm = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        print(tm)
        with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
            f.write("-" + str(i) + "\n")
            f.write(tm  + "\n")

        i = i + 1

        

def clear():
    for j in range(1, 12):
        input_tap("3eb", "7b5")
        j = j +1

#定义截取模板图片方法
def cap_model_scr():
    k = "37xxxxxxx"
    #v = ygh_dict[k]
    v = "Jxxxxxxx"

    tm = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    print(tm)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(tm + "\n")

    scr_cap_md(1)
    
    input_tap("28d", "89a") #第一步,主界面
    time.sleep(5)


    scr_cap_md(2)
    
    input_tap("285", "3e9") #输入用户名界面
    time.sleep(1)

    #scr_cap_md(2)
    
    clear()
    #time.sleep(2)

    os.system("adb shell input text {}".format(k))
    print(k)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(k + "\n")
    time.sleep(1)

    input_tap("2d8", "450")  #输入密码位置
    time.sleep(1)
    
    clear()
    #time.sleep(2)

    os.system("adb shell input text {}".format(v))
    #print(v)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(v + "\n")
    time.sleep(1)

    input_tap("21a", "19e")
    time.sleep(3)    

    input_tap("2e3", "5b7") #登录
    time.sleep(6)

    scr_cap_md(3) 

    input_tap("3ab", "5d6") #点数字运营
    time.sleep(6)

    scr_cap_md(4)

    input_tap("0d3", "782")  #点查询内容
    time.sleep(6)

    scr_cap_md(5)

    input_tap("05d", "092")        
    time.sleep(1)


    

cap_model_scr()  #截取模板图片

for k, v in ygh_dict.items():
    tm = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    print(tm)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(tm + "\n")

    #scr_cap_score(1)
    judge_cap_pause(1, 1, 0.9, 6)
    
    judge_cap_exit(1)

    input_tap("28d", "89a")
    time.sleep(3)

    #scr_cap_score(2)
    judge_cap_pause(2, 1, 0.85, 5) #等2秒

    input_tap("285", "3e9")
    time.sleep(1)
    
    clear()
    #time.sleep(2)

    os.system("adb shell input text {}".format(k))
    print(k)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(k + "\n")
    time.sleep(1)

    input_tap("2d8", "450")
    time.sleep(1)
    
    clear()
    #time.sleep(2)

    os.system("adb shell input text {}".format(v))
    #print(v)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(v + "\n")
    time.sleep(1)

    input_tap("21a", "19e")
    time.sleep(1)

    input_tap("2e3", "5b7")
    time.sleep(3)  

    judge_cap_pause(3, 1, 0.9, 5) #等3秒
    
    tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    m = scr_cap_score(3, tm1)
    if m <0.85:
           
        input_tap("295", "565")
        input_tap("290", "565")
        input_tap("285", "565")
        input_tap("295", "565")
        input_tap("05d", "092")
        time.sleep(1)
        continue #这里用break程序就退出了
    else:
        os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
        os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
        pass    #此处不对则说明用户名密码不对,退回
    
    
    input_tap("3ab", "5d6")
    time.sleep(3)

    #scr_cap_score(4)
    #judge_cap_pause(4, 1, 0.85, 5)
    judge_cap_pause(4, 1, 0.85, 3)


    tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    m = scr_cap_score(4, tm1)
    if m <0.85:
           
        input_tap("295", "565")
        input_tap("290", "565")
        input_tap("285", "565")
        input_tap("295", "565")
        input_tap("05d", "092")
        time.sleep(1)
        continue #这里用break程序就退出了
    else:
        os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
        os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
        pass    #此处不对则说明用户名密码不对,退回



    input_tap("0d3", "782")
    time.sleep(2)

    
    #judge_cap_pause(5, 3, 0.9, 5)
    judge_cap_pause(5, 3, 0.9, 5)

    tm1 = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    m = scr_cap_score(5, tm1)
    if m <0.9:
        
        input_tap("295", "565")
        input_tap("290", "565")
        input_tap("285", "565")
        input_tap("295", "565")
        input_tap("05d", "092")
        time.sleep(1)
        continue 
    else:
        os.remove("/home/olxxxxx/screencap{}.png".format(tm1))
        os.system("adb shell rm /sdcard/screencap{}.png".format(tm1))
        pass    #此处不对则说明用户名密码不对,退回

    #if int(dt.datetime.now().strftime("%H"))>=21 or (int(dt.datetime.now().strftime("%H"))<=16):
    input_tap("05d", "092")

    tm = dt.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
    print(tm)
    with open("/home/olxxxxx/下载/"+ td + "szyyllrz.txt", "a+") as f:
        f.write(tm + "\n")
    
        
    time.sleep(1)

    browse()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值