本文的主要目的是学习Python-OpenCV的用法
本人不对使用本程序造成的任何后果负责!
该程序主要用于写题太认真没注意到签到按钮的情况不推荐用于其他目的
基本思路:每10秒进行一次屏幕截图,和签到按键的图片进行匹配,若符合则计算出签到按钮的中心坐标并点击目标位置,完成签到。
为便于学习,把注释写的很全。
使用方法
c盘新建文件夹pic,将签到按钮截图导入,并命名为button.png。(可以自己改别的路径)
首先电脑要装好python 然后win+R输入cmd回车,cd进入程序目录,输入python xxx.py即可(xxx为程序的名字)
如果提示缺什么模块直接网上搜安装xxx即可,或者直接pip install xxx
exe文件下载,可以直接使用
腾讯课堂签到
链接:https://pan.baidu.com/s/1gbJcwZtQR4pzXwSq-FkkzA
提取码:t110
高校邦(务必先阅读使用说明)
链接https://www.aliyundrive.com/s/75mc7ypZZwL(提取码:py08)
程序功能:当遇到选择题时随机选择答案并点击下一步(课堂中的选择题不计入课程成绩),推荐在虚拟机中运行,并将虚拟机静音
源代码:见本文下方
使用说明:c盘新建文件夹pic 将以下三个图片放入C:\pic 图片名必须一致,建议用自己电脑截图效果最佳,务必确保照片的中心点位于按钮上),运行程序必须以管理员身份运行
定时自动签到程序
该版本在原自动签到程序中又加入了在指定时间启动腾讯课堂并根据选择自动进入相应科目,同时自动完成签到,仅需要签到功能则使用第二个程序 自动签到程序 即可。
'''
特别说明:本人不对使用该程序造成的任何后果负责,建议合理使用本程序
该程序签到功能仅在我的电脑实验成功
请务必用管理员身份运行,否则无法签到!!!
XiaoBai Apr 24,2022
'''
import subprocess
import datetime
import cv2
import pyautogui #自动GUI操作
import pyscreeze #屏幕截图
from time import sleep
def check(path):
target_button=cv2.imread(path, cv2.IMREAD_GRAYSCALE)
screenshot=pyscreeze.screenshot('C:/pic/screenshot.png')#截图
target_screenshot=cv2.imread('C:/pic/screenshot.png', cv2.IMREAD_GRAYSCALE)#cv2读入截屏
####获取按钮的宽高####
sp=target_button.shape
button_height=sp[0]
button_width=sp[1]
####图片匹配#####
result = cv2.matchTemplate(target_screenshot, target_button, cv2.TM_CCOEFF_NORMED)
mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= 0.8:
return max_loc
else:
return 0
def click(max_loc,X,Y):
####计算签到按钮位置####
taget_X=max_loc[0]+int(X/2)
taget_Y=max_loc[1]+int(Y/2)
###鼠标左键点击所计算的目标点####
pyautogui.click(taget_X, taget_Y, button='left')
print("请选择要签到的课程:1=信息论与编码 2=数字信号处理 3=人工智能")
Lesson=input()
if Lesson=='1':
Lesson_choice='C:/pic/L1.png'
target_button=cv2.imread('C:/pic/L1.png', cv2.IMREAD_GRAYSCALE) #信息论
sp=target_button.shape
L1_height=sp[0]
L1_width=sp[1]
elif Lesson=='2':
Lesson_choice='C:/pic/L2.png'
target_button=cv2.imread('C:/pic/L2.png', cv2.IMREAD_GRAYSCALE) #数字信号
sp=target_button.shape
L1_height=sp[0]
L1_width=sp[1]
elif Lesson=='3':
Lesson_choice='C:/pic/L3.png'
target_button=cv2.imread('C:/pic/L3.png', cv2.IMREAD_GRAYSCALE) #人工智能
sp=target_button.shape
sp=target_button.shape
L1_height=sp[0]
L1_width=sp[1]
else:
print("没有找到课程!")
print("设置上课时间,输入hour,min空格隔开:")
hour_input,min_input =map(int,input().split())
count=0
target_button=cv2.imread('C:/pic/in_class.png', cv2.IMREAD_GRAYSCALE) #进入课堂按钮
sp=target_button.shape
in_height=sp[0]
in_width=sp[1]
target_button=cv2.imread('C:/pic/button.png', cv2.IMREAD_GRAYSCALE) #签到按钮
sp=target_button.shape
sign_height=sp[0]
sign_width=sp[1]
target_button=cv2.imread('C:/pic/log.png', cv2.IMREAD_GRAYSCALE) #登录
sp=target_button.shape
log_height=sp[0]
log_width=sp[1]
target_button=cv2.imread('C:/pic/right.png', cv2.IMREAD_GRAYSCALE) #右箭头
sp=target_button.shape
right_height=sp[0]
right_width=sp[1]
while 1:
hour = datetime.datetime.now().hour
min = datetime.datetime.now().minute
if (min>=min_input)&(hour==hour_input):
subprocess.Popen('C:\\Program Files (x86)\\Tencent\\TXEDU\\2.0.1.54\\bin\\TXEDU.exe')#启动腾讯课堂
sleep(10)
res=check('C:/pic/log.png')
if res:
click(res,log_width,log_height)#登录
print("已经登录")
sleep(10)
while 1:
res=check(Lesson_choice)#查找选择的课程
if res:
click(res,L1_width,L1_height)#点击打开课程
res=check('C:/pic/in_class.png')
click(res,in_width,in_height)#点击进入直播
print("已经打开课程,接下来将为您自动签到")
while 1:
res=check('C:/pic/button.png')#查找签到按钮
if res:
click(res,sign_width,sign_height)#点击签到
sleep(3)
res=check('C:/pic/button.png')#再次检查签到按钮是否存在
if res:
print("警告:签到可能失败,请及时确认!")
else:
count+=1
print("已经完成",count,"次签到")
sleep(5)
else :
res=check('C:/pic/right.png')
click(res,right_width,right_height)#下一页
sleep(5)
执行效果
自动签到程序
'''
特别说明:本人不对使用该程序造成的任何后果负责,建议合理使用本程序
该程序签到功能仅在我的电脑实验成功
请务必用管理员身份运行,否则无法签到!!!
如果之后有时间了话写个UI界面
我的博客:https://xiaobai1103.cn/2022/04/22/tengxun_sign/
XiaoBai Apr 22,2022
'''
import cv2
import pyautogui #自动GUI操作
import pyscreeze #屏幕截图
import winsound #语音警报
from time import sleep
count=0 #签到次数
def sign():
####目标图片读取&截屏,路径根据自己需要更改####
target_button=cv2.imread('C:/pic/button.png', cv2.IMREAD_GRAYSCALE) #设置签到按钮图片,最好用自己电脑截图
screenshot=pyscreeze.screenshot('C:/pic/screenshot.png')#截图
target_screenshot=cv2.imread('C:/pic/screenshot.png', cv2.IMREAD_GRAYSCALE)#cv2读入截屏
####获取签到按钮照片的宽高####
sp=target_button.shape
button_height=sp[0]
button_width=sp[1]
####图片匹配#####
result = cv2.matchTemplate(target_screenshot, target_button, cv2.TM_CCOEFF_NORMED)
mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
#min_val、max_val都是输入的矩阵中的最小值和最大值
#min_loc、 max_loc都是最小值 最大值所对应的坐标元组
return max_val,max_loc,button_width,button_height
while 1:
max_val,max_loc,button_width,button_height=sign()
if max_val >= 0.8:
####计算签到按钮位置####
taget_X=max_loc[0]+int(button_width/2)
taget_Y=max_loc[1]+int(button_height/2)
###鼠标左键点击所计算的目标点####
pyautogui.click(taget_X, taget_Y, button='left')
####3秒后再次确认签到按钮是否消失####
sleep(3)
max_val,max_loc,button_width,button_height=sign()
if max_val >= 0.8:
print("警告:签到可能失败,请及时确认!")
winsound.Beep(440, 3000)
else:
####结果输出#####
count+=1
print("已经完成",count,"次签到")
sleep(10) #暂停10秒继续执行 可以根据老师签到时间把这个值改高点
执行效果
高校邦
'''
特别说明:本人不对使用该程序造成的任何后果负责,建议合理使用本程序
该程序签到功能仅在我的电脑实验成功
请务必用管理员身份运行!
XiaoBai May 11,2022
'''
import cv2
import pyautogui #自动GUI操作
import pyscreeze #屏幕截图
from time import sleep
count=0 #签到次数
checkbox_path='C:/pic/checkbox.png'
submit_button_path='C:/pic/submit.png'
continue_button_path='C:/pic/continue.png'
def check(path):
target_button=cv2.imread(path, cv2.IMREAD_GRAYSCALE)
screenshot=pyscreeze.screenshot('C:/pic/screenshot.png')#截图
target_screenshot=cv2.imread('C:/pic/screenshot.png', cv2.IMREAD_GRAYSCALE)#cv2读入截屏
####获取按钮的宽高####
sp=target_button.shape
button_height=sp[0]
button_width=sp[1]
####图片匹配#####
result = cv2.matchTemplate(target_screenshot, target_button, cv2.TM_CCOEFF_NORMED)
mn_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val >= 0.8:
return max_loc
else:
return 0
def click(max_loc,X,Y):
####计算签到按钮位置####
taget_X=max_loc[0]+int(X/2)
taget_Y=max_loc[1]+int(Y/2)
###鼠标左键点击所计算的目标点####
pyautogui.click(taget_X, taget_Y, button='left')
###选择框####
target_button=cv2.imread(checkbox_path, cv2.IMREAD_GRAYSCALE)
sp=target_button.shape
checkbox_height=sp[0]
checkbox_width=sp[1]
###提交按钮####
target_button=cv2.imread(submit_button_path, cv2.IMREAD_GRAYSCALE)
sp=target_button.shape
submit_button_height=sp[0]
submit_button_width=sp[1]
###继续观看按钮####
target_button=cv2.imread(continue_button_path, cv2.IMREAD_GRAYSCALE)
sp=target_button.shape
continue_button_height=sp[0]
continue_button_width=sp[1]
while 1:
res=check(checkbox_path)#选择框
if res:
click(res,checkbox_width,checkbox_height)
sleep(2)
res=check(submit_button_path)#提交按钮
if res:
click(res,submit_button_width,submit_button_height)
sleep(2)
res=check(continue_button_path)#继续按钮
if res:
click(res,continue_button_width,continue_button_height)
sleep(2)
res=check(continue_button_path)#再次查找继续按钮
if res:
print("出现异常")
else:
print("完成课堂测试")
sleep(3)
升级记录
Apr 22,2022:增加判断是否签到失败的功能,当签到3秒后签到按钮仍然存在时,会直接报错。
Apr 24,2022:增加了定时启动,并自动进入相应课程的功能
Apr 25,2022:增加签到失败声音报警功能
May 11,2022:增加高校邦网课程序
问题记录
收到反馈无法签到,解决办法:通过录屏、截屏等方法获得自己电脑的签到按钮图片
原理:模板匹配
模板匹配,就是在给定的图片中查找和模板最相似的区域,该算法的输入包括模板和图片,整个任务的思路就是按照滑窗的思路不断的移动模板图片,计算其与图像中对应区域的匹配度,最终将匹配度最高的区域选择为最终的结果。
#模板匹配
result = cv2.matchTemplate(img,template,method)
#返回图像结果 val:值 loc:坐标
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
- img: 要进行模板匹配的图像
- Template :模板
- method:实现模板匹配的算法,主要有:
cv2.TM_CCOEFF (系数匹配法)匹配数值越高表示匹配效果越好
cv2.TM_CCOEFF_NORMED(相关系数匹配法)匹配数值越高表示匹配效果越好 结果0~1
cv2.TM_CCORR (相关匹配法)匹配数值越高表示匹配效果越好
cv2.TM_CCORR_NORMED (归一化相关匹配法)匹配数值越高表示匹配效果越好 结果0~1
cv2.TM_SQDIFF (平方差匹配法)匹配数值越低表示匹配效果越好
cv2.TM_SQDIFF_NORMED (归一化平方差匹配法)匹配数值越低表示匹配效果越好 结果0~1
返回结果(如果不需要,则使用NULL)
- min_val参数表示返回的最小值。
- max_val参数表示返回的最大值。
- min_loc参数表示返回的最小位置的坐标。
- max_loc参数表示返回的最大位置的坐标。
参考文献:
Comments | 4 条评论
博客作者 春
早上8点自动打开腾讯课堂并自动进入相应课程的软件,同时具有签到功能,这软件啥时候上,等不及了。好兄弟
博客作者 小白
@春 已经做出了个基础版本 之后有时间再改进一下
博客作者 740555801
这是一条私密评论
博客作者 小白
@740555801 QQ 1920227256