python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
Python  /  管理员 发布于 7年前   191
最近的答题赢钱很火爆,我也参与了几次,有些题目确实很难答,但是10秒钟的时间根本不够百度的,所以写了个辅助挂,这样可以出现题目时自动百度,这个时间也就花掉2秒钟,剩下的7、8秒钟可以进行分析和作答,提升了赢钱概率。
源码可以见我的github:点击链接
原理分析下:使用adb命令,抓取手机视频播放的界面,然后通过python的截取和ocr,获得到题目和答案, 然后百度得到结果。这个环境怎么搭建,有需要的童鞋可以联系我,因为使用本地的ocr所以解析不花钱,也没有使用的限制。
github上的代码中
ocr_bw.py,这个是自动根据题目去百度,然后打开浏览器,展示检索结果
# -*- coding: utf-8 -*-import pytesseractimport timeimport webbrowserimport subprocessfrom PIL import Imagedef main(): """ 主函数 """ op = yes_or_no('请确保手机打开了 ADB 并连接了电脑,' '然后打开西瓜视频后再用本程序,确定开始?') if not op: print('bye') return #核心递归 ocr_subject_parent() # for root, sub_dirs, files in os.walk('E:/临时接收的文件/知乎答题/百万/'): # for file in files: # print('发现图片:' + file) # img = Image.open('E:/临时接收的文件/知乎答题/百万/'+file) # ocr_subject(img)def yes_or_no(prompt, true_value='y', false_value='n', default=True): """ 检查是否已经为启动程序做好了准备 """ default_value = true_value if default else false_value prompt = '{} {}/{} [{}]: '.format(prompt, true_value, false_value, default_value) i = input(prompt) if not i: return default while True: if i == true_value: return True elif i == false_value: return False prompt = 'Please input {} or {}: '.format(true_value, false_value) i = input(prompt)def screenImg(true_value='', default=True): prompt = '当出现题目时,请按下回车进行识别 ' i = input(prompt) if not i: return default while True: if i == true_value: return True else: return False i = input(prompt)def ocr_subject(p): # 截取 距离上530开始 940结束 # 截取 距离上260 570结束 p = cut_img(p) pytesseract.pytesseract.tesseract_cmd = 'E:/Program Files (x86)/Tesseract-OCR/tesseract' subject = pytesseract.image_to_string(p, lang='chi_sim') subject = "".join(subject.split()) subject = subject.split('.')[1] print(subject) openPage(subject) ocr_subject_parent()def ocr_subject_parent(): result = screenImg() if result: start = time.time() # screenshot.check_screenshot() process = subprocess.Popen( 'adb shell screencap -p', shell=True, stdout=subprocess.PIPE) binary_screenshot = process.stdout.read() binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n') f = open('autojump.png', 'wb') f.write(binary_screenshot) f.close() # screenshot.pull_screenshot() img = Image.open('autojump.png') print("耗时:" + str(time.time() - start)) ocr_subject(img)def openPage(subject): url = 'https://www.baidu.com/s?wd={}'.format( subject) webbrowser.open(url) webbrowser.get()def cut_img(img): region = img.crop((70, 260, 1025, 570)) #region.save("temp/cut_first.png") return regionif __name__ == '__main__': main()
ocr_bw2.py,这个是根据题目+答案,去百度检索,通过爬虫抓取百度的收录数,然后在控制台打印结果
__author__ = 'zjy'# -*- coding:utf-8 -*-import pytesseractimport timeimport webbrowserimport subprocessfrom PIL import Imageimport urllibimport urllib.requestimport threadingfrom urllib.parse import quotedef main(): """ 主函数 """ op = yes_or_no('请确保手机打开了 ADB 并连接了电脑,' '然后打开西瓜视频后再用本程序,确定开始?') if not op: print('bye') return # 核心递归 ocr_subject_parent() # for root, sub_dirs, files in os.walk('E:/临时接收的文件/知乎答题/百万/'): # for file in files: # print('发现图片:' + file) # img = Image.open('E:/临时接收的文件/知乎答题/百万/'+file) # ocr_subject(img)def yes_or_no(prompt, true_value='y', false_value='n', default=True): """ 检查是否已经为启动程序做好了准备 """ default_value = true_value if default else false_value prompt = '{} {}/{} [{}]: '.format(prompt, true_value, false_value, default_value) i = input(prompt) if not i: return default while True: if i == true_value: return True elif i == false_value: return False prompt = 'Please input {} or {}: '.format(true_value, false_value) i = input(prompt)def screenImg(true_value='', default=True): prompt = '当出现题目时,请按下回车进行识别 \n' i = input(prompt) if not i: return default while True: if i == true_value: return True else: return False i = input(prompt)def ocr_subject(p): # 截取 距离上530开始 940结束 # 截取 距离上260 570结束 subImg = cut_img(p) pytesseract.pytesseract.tesseract_cmd = 'E:/Program Files (x86)/Tesseract-OCR/tesseract' subject = pytesseract.image_to_string(subImg, lang='chi_sim') subject = "".join(subject.split()) subject = subject.split('.')[1].replace("\"", "") print(subject) ocr_answer(p, subject) # openPage(subject) # print("结束:" + str(time.time())) ocr_subject_parent()def getSearchNum(key): key = quote(key) # print(key) url = 'http://www.baidu.com/s?wd={}'.format(key) # print(url) response = urllib.request.urlopen(url) page = response.read().decode("utf-8") i = int(page.index('百度为您找到相关结果约')) start = i + 10 end = i + 25 page = page[start: end] return pagedef ocr_answer(p, subject): list = cut_question(p) pytesseract.pytesseract.tesseract_cmd = 'E:/Program Files (x86)/Tesseract-OCR/tesseract' for p in list: t = threading.Thread(target=ocr_answer_thread, args=(p, subject)) t.start()def ocr_answer_thread(p, subject): answer = pytesseract.image_to_string(p, lang='chi_sim') answer = "".join(answer.split()) v = getSearchNum(subject + ' ' + answer) print(answer + ' ' + v) # print(time.time())def ocr_subject_parent(): result = screenImg() if result: start = time.time() # print("开始:" + str(start)) # screenshot.check_screenshot() process = subprocess.Popen( 'adb shell screencap -p', shell=True, stdout=subprocess.PIPE) binary_screenshot = process.stdout.read() binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n') f = open('autojump.png', 'wb') f.write(binary_screenshot) f.close() # screenshot.pull_screenshot() img = Image.open('autojump.png') ocr_subject(img)def openPage(subject): url = 'https://www.baidu.com/s?wd={}'.format( subject) webbrowser.open(url) webbrowser.get()def cut_img(img): region = img.crop((70, 260, 1025, 570)) # region.save("temp/cut_first.png") return regiondef cut_question(img): list = [] question1 = img.crop((70, 590, 1025, 768)) question2 = img.crop((70, 769, 1025, 947)) question3 = img.crop((70, 948, 1025, 1130)) list.append(question1) list.append(question2) list.append(question3) # question1.save("temp/cut_1.png") # question2.save("temp/cut_2.png") # question3.save("temp/cut_3.png") return listif __name__ == '__main__': main()
由于很多题目是下列哪个不是,所以我更喜欢用第一个方式,基本上识别时间在0.5-0.6秒之间。
最后里面的ocr_zh.py是可以用来抓取头脑王者的辅助。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号