学弟抱怨说期末选修网课太多,于是我教他用python如何实现自动刷课,简直不要太爽!
声明:只是用来学习,请不要使用非法用途,责任自负
工具
使用python 3.6版本,安装如下库:
安装win32api
pip3 install pywin32
安装PIL
pip install Pillow
安装pyautogui
pip install pyautogui
安装numpy
pip install numpy
安装cv2
pip install opencv-python
安装matplotlib
pip install matplotlib
使用SPY查看相关窗口标题, 类名。此标题唯一, 故可以以此来查找相关窗口
得到窗口句柄
window_title = '课件学习 - Google Chrome'screen_width = win32api.GetSystemMetrics(0)screen_height = win32api.GetSystemMetrics(1) hwnd = win32gui.FindWindow(win32con.NULL,window_title) if hwnd == 0 :error_exit('%s not found' % window_title)exit()else:print('hwnd = %x'%(hwnd))window_left,window_top,window_right,window_bottom = win32gui.GetWindowRect(hwnd)
主循环
原理:主要通信截图屏幕的图片,然后通过模板图像与之比较,如果出现我们需要的场景,那么得到对应的位置坐标,然后自动调用点击功能,从而实现自动化操作。那么这里主要使用opencv的两个算法,一个是图像相似度打分算法,另一个是图像搜索算法。
while True:grab_image = snapshot.grab_screen(deal_left,deal_top,deal_right,deal_bottom)#grab_image.show()grab_image.save(r'.\tmp_output\full_screen.png')#big pic size = 1936x1056full_screen_w = 1936full_screen_h = 1056pixel_core_x = 877.0pixel_core_y = 25.0deal_left = window_left #window_left + kejian_x / full_screen_w * window_width - 100deal_top = window_top + pixel_core_y / full_screen_h * window_height - 20deal_right = window_left + window_width#window_left + kejian_x / full_screen_w * window_width + 150 deal_bottom = window_top + pixel_core_y / full_screen_h * window_height + 20grab_image = snapshot.grab_screen(deal_left,deal_top,deal_right,deal_bottom)search_pic = r'.\tmp_output\search_kejianxuexi.png' grab_image.save(search_pic)#find kejian_temtemplate_pic = r'.\template\kejian_tem.png'num, w, h, pos_list = match.lookup_pos(template_pic, search_pic)left = 0top = 0find_kejian_flag = 0no_voice_flag = 0if num == 1:left = pos_list[0][0]top = pos_list[0][1]find_kejian_flag = 1else:print('==========warning search_kejianxuexi = ' + str(num))find_kejian_flag = 0if find_kejian_flag:img_rgb = cv2.imread(search_pic)img_rgb = img_rgb[top:top + h, left:left + w + 80, :] # h, w, ccompare_pic = r'.\tmp_output\kejianxuexi_compare.png'cv2.imwrite(compare_pic, img_rgb)temp_voice = r'.\template\kejianhua_tem_voice.png'temp_no_voice = r'.\template\kejianhua_tem_no_voice.png'no_voice_flag = match.score_pic(compare_pic, temp_voice, temp_no_voice)if no_voice_flag:print('===============find no_voice_flag')find_question_flag = find_question()if find_question_flag:#secondtime.sleep(5)find_daan()time.sleep(5)find_quding()find_chongbo_flag = find_chong_bo()if find_question_flag and find_chongbo_flag:print('========>find_chongbo_flag and find_chongbo_flag')exit()if find_chongbo_flag:weikaishi() else:print('===============every thing is ok')time.sleep(2) #exit(0)
图像相似度打分算法
那么如何判断一张被PS过的图片是否与另一张图片本质上相同呢?比较简单、易用的解决方案是采用感知哈希算法(Perceptual Hash Algorithm)。
感知哈希算法是一类算法的总称,包括aHash、pHash、dHash。顾名思义,感知哈希不是以严格的方式计算Hash值,而是以更加相对的方式计算哈希值,因为“相似”与否,就是一种相对的判定。
aHash:平均值哈希。速度比较快,但是常常不太精确。
pHash:感知哈希。精确度比较高,但是速度方面较差一些。
dHash:差异值哈希。Amazing!精确度较高,且速度也非常快。因此我就选择了dHash作为我图片判重的def
pHash(imgfile):"""get image pHash value"""#加载并调整图片为32x32灰度图片img=cv2.imread(imgfile, 0) img=cv2.resize(img,(64,64),interpolation=cv2.INTER_CUBIC)#创建二维列表h, w = img.shape[:2]vis0 = np.zeros((h,w), np.float32)vis0[:h,:w] = img #填充数据#二维Dct变换vis1 = cv2.dct(cv2.dct(vis0))#cv.SaveImage('a.jpg',cv.fromarray(vis0)) #保存图片vis1.resize(32,32)#把二维list变成一维listimg_list=(vis1.tolist())print('----------')sum(img_list)#计算均值avg = sum(img_list)/(len(img_list)*1.0)print('----------')avg_list = ['0' if i<avg else '1' for i in img_list]#得到哈希值return ''.join(['%x' % int(''.join(avg_list[x:x+4]),2) for x in range(0,32*32,4)])def hammingDist(s1, s2):assert len(s1) == len(s2)return sum([ch1 != ch2 for ch1, ch2 in zip(s1, s2)])def aHash(img):#缩放为8*8img=cv2.resize(img,(8,8),interpolation=cv2.INTER_CUBIC)#转换为灰度图gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#s为像素和初值为0,hash_str为hash值初值为''s=0hash_str=''#遍历累加求像素和for i in range(8):for j in range(8):s=s+gray[i,j]#求平均灰度avg=s/64#灰度大于平均值为1相反为0生成图片的hash值for i in range(8):for j in range(8):if gray[i,j]>avg:hash_str=hash_str+'1'else:hash_str=hash_str+'0'return hash_strdef cmpHash(hash1,hash2):n=0#hash长度不同则返回-1代表传参出错if len(hash1)!=len(hash2):return -1#遍历判断for i in range(len(hash1)):#不相等则n计数+1,n最终为相似度if hash1[i]!=hash2[i]:n=n+1return 1 - n / 64def score_pic(compare_pic, temp_voice, temp_no_voice):#HASH1=pHash(compare_pic)#HASH2=pHash(temp_voice)#out_score = 1 - hammingDist(HASH1,HASH2)*1. / (32*32/4)img1 = cv2.imread(compare_pic)img2 = cv2.imread(temp_voice)img3 = cv2.imread(temp_no_voice)#time1 = time.time()hash1 = aHash(img1)hash2 = aHash(img2)voice_score = cmpHash(hash1, hash2)hash1 = aHash(img1)hash3 = aHash(img3)no_voice_score = cmpHash(hash1, hash3)no_voice_flag = 0#print(str(voice_score) + '=>' + str(no_voice_score))if no_voice_score >= voice_score:no_voice_flag = 1else:no_voice_flag = 0return no_voice_flag
图像搜索算法
使用res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
def lookup_pos(template_pic, search_pic):img_rgb = cv2.imread(search_pic)img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)img = img_gray#print(img.shape)template = cv2.imread(template_pic,0)w, h = template.shape[::-1]res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)threshold = 0.95loc = np.where( res >= threshold)num = 0left = 0top = 0pos_list = []for pt in zip(*loc[::-1]):cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)left = pt[0]top = pt[1]pos_list.append(pt)num = num + 1res = res*256cv2.imwrite(r'.\tmp_output\out.png', img_rgb)cv2.imwrite(r'.\tmp_output\res.png', res)return num, w, h, pos_list
学弟抱怨说期末选修网课太多,于是我教他用python如何实现自动刷课,简直不要太爽!相关推荐
- 自动排课系统的设计与实现 开题报告 java_高校自动排课APP的设计与实现毕业论文+开题报告+app源码(Hbuilder+Android+sqlite+H5)+操作步骤...
摘 要 传统的人工排课使用手工操作存在许多弊端,如重复劳动严重.效率低下.且不能达到资源的最优化配置等,将产生大量的文件和数据,这对于查找.更新和维护都带来了不少的困难.因此设计一个高校自动排课AP ...
- java+python课程教室自动排课系统Vue+ElemenUI
使用自动排课系统分为管理员和学生.教师三个角色的权限子模块. 管理员所能使用的功能主要有:首页.个人中心.学生管理.教师管理.班级信息管理.专业信息管理.教室信息管理.课程信息管理.排课信息管理.系统 ...
- python+selenium+优学院刷课
from selenium import webdriver from selenium.webdriver import ActionChains import time#收获: #try...ex ...
- 小几届的学弟问我,软件测试岗是选11k的华为还是20k的小公司,我直呼受不了,太凡尔赛了
这位小学弟得到了两个工作机会,一个是名气大的华为,一个是名气小点的普通互联网企业,但是名气小的企业薪资开的高.这下他就不知道怎么选了. 从交谈中我可以明显的感觉到他是想去后面那家名气小点但是薪资开的更 ...
- 表弟背着我帮同学刷课,一个月赚了1500,这是亲表弟吗?
今天想给大家做一个测试,扎心预警 以下三种情况,哪个让你最绝望: ❶ 好不容易找到一个资源齐全的网站,不是要收费就是要翻墙 ❷ 每次想看的音乐会.想抢的假期车票,永远抢不到 ❸ 工资4500/生活费1 ...
- Python小白的数学建模课-10.微分方程边值问题
小白往往听到微分方程就觉得害怕,其实数学建模中的微分方程模型不仅没那么复杂,而且很容易写出高水平的数模论文. 本文介绍微分方程模型边值问题的建模与求解,不涉及算法推导和编程,只探讨如何使用 Pytho ...
- python培训 线上VIP课费用
前几天看到学妹小希发了个朋友圈,我心态立马炸了: 好家伙!竟然有钱入手五位数的新款苹果13pro. 可是小希不是刚毕业吗,哪来这么多钱?我的内心充满了疑惑. 于是我就问了一下她,结果答案让我大吃一惊. ...
- Python小白的数学建模课-18.最小生成树问题
Python小白的数学建模课-18.最小生成树问题 最小生成树(MST)是图论中的基本问题,具有广泛的实际应用,在数学建模中也经常出现. 路线设计.道路规划.官网布局.公交路线.网络设计,都可以转化为 ...
- Python小白的数学建模课-15.图论的基本概念
图论中所说的图,不是图形图像或地图,而是指由顶点和边所构成的图形结构. 图论不仅与拓扑学.计算机数据结构和算法密切相关,而且正在成为机器学习的关键技术. 本系列结合数学建模的应用需求,来介绍 Netw ...
最新文章
- 解决Android 7.0 App内切换语言不生效的问题
- Hello World!团队第四次会议
- bashrcprofile
- ASP.NET Core 缓存技术 及 Nginx 缓存配置
- php gd png透明,调整PNG大小并将其放在PHP / GD中较大的透明背景上?
- 你让,勋爵? 使用Jenkins声明性管道的Docker中的Docker
- Android编程获取手机型号,本机电话号码,sdk版本及firmware版本号(即系统版本号)...
- java mail outlook_已启用Outlook API邮件与邮箱用户
- Tomcat启动报Error listenerStart错误
- ios 微信分享重新编码链接_微信民众号IOS端复制链接失足,安卓端分享链接翻开只能进入首页等题目的处理...
- DIV 粘贴插入文本或者其他元素后,移动光标到最新处
- 苹果mac应用程序卸载软件:App Uninstaller
- 鹏芯U盘(UDK2008)意外断电后修复
- Ubuntu 18.04安装c++版OpenCV4
- 适合小白的 Asyncio 教程!
- GTX1050和GTX1050Ti哪个值得买?
- Object.setPrototypeOf 与 Object.create() 的区别
- mysql oder by 注入_Order by排序注入方法小总结
- 河北大学秋季计算机课,MOOC与大学计算机课程建设研讨会暨主题教学沙龙活动在河北大学召开...
- 2021年全球与中国全自动免疫分析分析仪行业市场规模及发展前景分析