1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| import cv2 as cv import numpy as np import dlib, os, mss import pyautogui import time
import tkinter from tkinter import Tk,Entry,Button,Label,Text,StringVar,PhotoImage,ttk from PIL import Image, ImageTk import time import threading
window_name = 'Test' screnn_width = 1920 screnn_height = 1080 window_size = 3 num_left, num_top, num_width, num_height = screnn_width // 3,\ screnn_height // 3, screnn_width // 3, screnn_height // 3 num_res_width, num_res_height = screnn_width // window_size,\ screnn_height // window_size
monitor = { 'left': 0, 'top': 0, 'width': 1000, 'height': 576, }
def detection(dets, img): for k, d in enumerate(dets): cv.rectangle(img, (d.left(), d.top()), (d.left() + d.width(), d.top() + d.height()), (0, 0, 255), 2)
def monit(): with mss.mss() as sct: while True: try: img = sct.grab(monitor) img = np.array(img) gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) cv.namedWindow(window_name, cv.WINDOW_NORMAL) cv.resizeWindow(window_name, num_res_width, num_res_height) detector = dlib.simple_object_detector("data.svm") dets = detector(gray) detection(dets, img) cv.imshow(window_name, img) k = cv.waitKey(1) if k % 256 == 27: cv.destroyAllWindows() os._exit(0) detected_windows = [] for k, d in enumerate(dets): detected_windows.append((d.left(), d.top(), d.width(), d.height()))
if len(detected_windows) != 0: upper_left = (detected_windows[0][0], detected_windows[0][1]) yield upper_left
except Exception as e: print(e) os._exit(0)
def auto_click(var_avg): pyautogui.click(var_avg[0], var_avg[1], button="left") time.sleep(1)
def thread_it(func, *args): myThread = threading.Thread(target=func, *args) myThread.setDaemon(True) myThread.start()
def routine(): global going going = True while going == True: for i in range(50): pass avg = monit() print(next(avg)) print("正在点击") auto_click(next(avg))
def end(): global going going = False
def end_program(): os._exit(0)
def GUI(): fm_main=Tk() fm_main.resizable(False, False) fm_main.geometry("250x320+500+200") fm_main.title("阴阳师小助手") fm_main.iconbitmap(r'.\image\程序图标.ico') btn1 = Button(fm_main, text='开始运行', command= lambda:thread_it(routine), font=("华文行楷", 12)) btn2 = Button(fm_main, text='结束运行', command=lambda: end(), font=("华文行楷", 12)) btn1.grid(row=1, column=0, columnspan=1,sticky="w", padx=40, pady=9) btn2.grid(row=1, column=0, columnspan=1,sticky="e", padx=40, pady=9)
image = Image.open(r"./image/背景.png") image = image.resize((240, 240), Image.LANCZOS) photo = ImageTk.PhotoImage(image) label = Label(image=photo) label.image = photo label.grid(row=2, column=0, padx=2, pady=0)
author = tkinter.Label(fm_main, text="By GanSer", font=("华文行楷", 16)) author.place(x=155, y=295)
fm_main.mainloop()
going = False GUI()
|