模拟鼠标键盘操作,依托opencv实现程序ui自动操作

上代码:

from pykeyboard import *
from pymouse import *
'''import win32pi'''

m=PyMouse()
k=PyKeyboard()
'''m.click(50,50,1,2)'''
x,y=m.screen_size()
print("x,y",x,y)
'''
环境安装好了,晚点编代码调试
'''
'''测试opencv'''
import cv2
import numpy as np
import pyautogui
import time

'''capture desktop'''
def saveScreen():
    x,y=m.screen_size()
    img=pyautogui.screenshot(region=[0,0,x,y])
    img.save('c:/python/imgs/screenshot.png')
    
def find_button(target,template):
    theight,twidth=target.shape[:2]
    result=cv2.matchTemplate(target,template,cv2.TM_SQDIFF_NORMED)
    min_val,max_val,min_loc,max_loc=cv2.minMaxLoc(result)
    print(str(min_val))
    if min_val>0.01:
        return None
    
    x=min_loc[0]+twidth//3
    y=min_loc[1]+theight//3

    return (x,y)

def check(target):
    saveScreen()
    #target=cv2.imread('c:/python/imgs/is.PNG')
    template=cv2.imread('c:/python/imgs/screenshot.png')
    
    return find_button(target,template)

def find_and_click(target,times):
    #target=cv2.imread('c:/python/imgs/scroll_up.PNG')
    result=check(target)
    time.sleep(1)
    if result is not None:
        m.click(result[0],result[1],1,times)
    else:
        print('not found scrollup')
        
def forcuseMainApp():
    target=cv2.imread('c:/python/imgs/return_to_chia.PNG')
    find_and_click(target,1)
    
def checkFarm():
    target=cv2.imread('c:/python/imgs/is.PNG')
    return check(target)

def scrollUP():
    target=cv2.imread('c:/python/imgs/scroll_up.PNG')
    result=check(target)
    if result is not None:
        m.click(result[0],result[1],1,20)
        time.sleep(1)
    else:
        print('not found scrollup')
        
def clickAddFarmBtn():
    target=cv2.imread('c:/python/imgs/add_farm.PNG')
    find_and_click(target,1)

def clickFarmSize():
    target=cv2.imread('c:/python/imgs/disk_label.PNG')
    find_and_click(target,1)
    
def chooseDefaultDiskSize():
    target=cv2.imread('c:/python/imgs/disk_default.PNG')
    find_and_click(target,1)

def clickFarmNum():
    print('click farm num')
    target=cv2.imread('c:/python/imgs/farm_num_label.PNG')
    find_and_click(target,1)
    
def chooseDefaultFarmNum():
    print('choose farm num')
    target=cv2.imread('c:/python/imgs/farm_num_default.PNG')
    find_and_click(target,2)

def clickParaFarm():
    print('click para farm radio')
    target=cv2.imread('c:/python/imgs/para_farm_radio.PNG')
    find_and_click(target,2)

def inputFarmLat():
    print('click farm lat')
    target=cv2.imread('c:/python/imgs/time_lat.PNG')
    find_and_click(target,2)
    k.tap_key(k.delete_key)
    time.sleep(1)
    k.tap_key(k.delete_key)
    time.sleep(1)
    k.type_string('3')

def clickAdvanceOption():
    print('click ad option')
    target=cv2.imread('c:/python/imgs/show_ad.PNG')
    find_and_click(target,1)

def inputmemsize():
    print('click mem option')
    target=cv2.imread('c:/python/imgs/max_mem_sett.PNG')
    find_and_click(target,2)
    k.tap_key(k.delete_key)
    time.sleep(1)
    k.tap_key(k.delete_key)
    time.sleep(1)
    k.tap_key(k.delete_key)
    time.sleep(1)
    k.tap_key(k.delete_key)
    time.sleep(1)
    k.type_string('3000')
    
def chooseTmpFolder():
    print('choose tmp folder')
    target=cv2.imread('c:/python/imgs/tmp_folder.PNG')
    find_and_click(target,1)

def scroll_to_fast_access():
    print('scoll  to')
    target=cv2.imread('c:/python/imgs/choose_file_up.PNG')
    find_and_click(target,15)

def choosePfolder():
    print('choose p')
    target=cv2.imread('c:/python/imgs/tmp_p.PNG')
    find_and_click(target,2)

def clickChooseFileBtn():
    print('click choose file btn')
    target=cv2.imread('c:/python/imgs/choose_file_btn.PNG')
    find_and_click(target,2)

def chooseFinalFolder():
    print('click choose final folder')
    target=cv2.imread('c:/python/imgs/final_foler.PNG')
    find_and_click(target,1)

def chooseFinalfolderDisk():
    print('choose p')
    target=cv2.imread('c:/python/imgs/final_foler_disk.PNG')
    find_and_click(target,2) 

def createFarm():
    #1,scroll up
    #
    #2,click add farm btn
    clickAddFarmBtn()
    #3,click farm disk size
    time.sleep(2)
    clickFarmSize()
    time.sleep(2)
    chooseDefaultDiskSize()
    time.sleep(4)
    #4,click farm num
    clickFarmNum()
    time.sleep(2)
    chooseDefaultFarmNum()
    #5,choose para farm
    time.sleep(2)
    clickParaFarm()
    time.sleep(2)
    #6,input farm lat
    inputFarmLat()
    time.sleep(2)
    
    forcuseMainApp()
    #7,scroll down 1015 415
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    
    time.sleep(1)
    #m.click(1015,416,1,2)
    #8,click advance iioption
    clickAdvanceOption()
    #9,imput mem size
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    
    time.sleep(1)
    inputmemsize()
    time.sleep(2)
    forcuseMainApp()
    # click is not work
    #m.click(1015,260,1,2)
    #time.sleep(3)
    #m.press(1015,260)
    #time.sleep(3)
    #time.sleep(1)
    #m.release(1015,400)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key)
    time.sleep(1)
    k.tap_key(k.down_key,15)
    time.sleep(1)
    
def chooseTmpFolderAndFinished():
    chooseTmpFolder()
    time.sleep(2)
    scroll_to_fast_access()
    time.sleep(2)
    choosePfolder()
    time.sleep(2)
    clickChooseFileBtn()
    
    return True

def chooseFinalFolderAndFinished():
    chooseFinalFolder()
    time.sleep(2)
    scroll_to_fast_access()
    time.sleep(2)
    chooseFinalfolderDisk()
    time.sleep(2)
    clickChooseFileBtn()
    return True

def clickStartFarm():
    print('start happy farm')
    target=cv2.imread('c:/python/imgs/begin_farm.PNG')
    find_and_click(target,1)
    
runtimes=0
run_times_max=1
# if farm stoped create new farm
# 1,初始状态为 chia钱包运行并且处在耕种界面
print('begin run')
while True:
    print('recheck begin')
    forcuseMainApp()
    time.sleep(2)
    scrollUP()
    re=checkFarm()
    if re:
        print('farm is on going!!')
        time.sleep(100)
        continue
    time.sleep(50)
    runtimes+=1
    '''
    #4,click farm num debug
    clickFarmNum()
    time.sleep(2)
    chooseDefaultFarmNum()
    '''
    
    
    print('create farm')
    createFarm()
    chooseTmpFolderAndFinished()
    time.sleep(3)
    chooseFinalFolderAndFinished()
    time.sleep(3)
    clickStartFarm()
    
    #saveScreen()
    #print(checkFarmOver())
    
    #if runtimes>=run_times_max:
    #    break
    

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值