阴阳师七周年,有个活动需要刷999次。活动前三天,我十分本分的手动刷了999,但是太累了,于是写了脚本用于自动识别点击(脚本比较拙劣,依赖前端鼠标控制,demo没有做防检测凑合着点吧)。

一、简单训练

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
import dlib
import cv2 as cv

def Train():
options = dlib.simple_object_detector_training_options()
options.add_left_right_image_flips = True
options.C = 5
options.num_threads = 2
options.be_verbose = True
dlib.train_simple_object_detector('data.xml', 'data.svm', options)

def deteTest():
imgpath = '12.png'
image = cv.imread(imgpath)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
detector = dlib.simple_object_detector("data.svm")
dets = detector(gray)
detected_windows = []
for (k, d) in enumerate(dets):
detected_windows.append((d.left(), d.top(), d.width(), d.height()))
cv.rectangle(image, (d.left(), d.top()), (d.left() + d.width(),
d.top() + d.height()), (0, 255, 0), 1)
cv.imshow("Output", image)
cv.waitKey(0)
print(detected_windows)


if __name__ == '__main__':
while True:
print('''
| 1.训练模型 | 2.查看效果 |
'''.strip())
var = int(input(">>"))
if var == 1:
Train()
if var == 2:
deteTest()

二、mss+pyautogui点击

简单写了一个GUI界面丑陋无比

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

# sct = mss.mss()

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:
# thread_it(monit)
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)
# #插入进度条
# label = Label(fm_main,text = "进度条:",font=("华文行楷",12))
# label.grid(row=3,column=0,sticky="w")
#
# pbar = ttk.Progressbar(fm_main)
# pbar.grid(row=3,column=0,sticky="e",padx=80)
# pbar['maximum'] = 60
# pbar['value'] = 0

fm_main.mainloop()



going = False
GUI()