您好,登錄后才能下訂單哦!
這篇文章主要介紹了python3.5+tesseract+adb如何實(shí)現(xiàn)西瓜視頻或頭腦王者輔助答題,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
原理分析下:使用adb命令,抓取手機(jī)視頻播放的界面,然后通過(guò)python的截取和ocr,獲得到題目和答案, 然后百度得到結(jié)果。這個(gè)環(huán)境怎么搭建,有需要的童鞋可以聯(lián)系我,因?yàn)槭褂帽镜氐膐cr所以解析不花錢,也沒(méi)有使用的限制。
github上的代碼中
ocr_bw.py,這個(gè)是自動(dòng)根據(jù)題目去百度,然后打開(kāi)瀏覽器,展示檢索結(jié)果
# -*- coding: utf-8 -*- import pytesseract import time import webbrowser import subprocess from PIL import Image def main(): """ 主函數(shù) """ op = yes_or_no('請(qǐng)確保手機(jī)打開(kāi)了 ADB 并連接了電腦,' '然后打開(kāi)西瓜視頻后再用本程序,確定開(kāi)始?') if not op: print('bye') return #核心遞歸 ocr_subject_parent() # for root, sub_dirs, files in os.walk('E:/臨時(shí)接收的文件/知乎答題/百萬(wàn)/'): # for file in files: # print('發(fā)現(xiàn)圖片:' + file) # img = Image.open('E:/臨時(shí)接收的文件/知乎答題/百萬(wàn)/'+file) # ocr_subject(img) def yes_or_no(prompt, true_value='y', false_value='n', default=True): """ 檢查是否已經(jīng)為啟動(dòng)程序做好了準(zhǔn)備 """ 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 = '當(dāng)出現(xiàn)題目時(shí),請(qǐng)按下回車進(jìn)行識(shí)別 ' 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開(kāi)始 940結(jié)束 # 截取 距離上260 570結(jié)束 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("耗時(shí):" + 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 region if __name__ == '__main__': main()
ocr_bw2.py,這個(gè)是根據(jù)題目+答案,去百度檢索,通過(guò)爬蟲(chóng)抓取百度的收錄數(shù),然后在控制臺(tái)打印結(jié)果
__author__ = 'zjy' # -*- coding:utf-8 -*- import pytesseract import time import webbrowser import subprocess from PIL import Image import urllib import urllib.request import threading from urllib.parse import quote def main(): """ 主函數(shù) """ op = yes_or_no('請(qǐng)確保手機(jī)打開(kāi)了 ADB 并連接了電腦,' '然后打開(kāi)西瓜視頻后再用本程序,確定開(kāi)始?') if not op: print('bye') return # 核心遞歸 ocr_subject_parent() # for root, sub_dirs, files in os.walk('E:/臨時(shí)接收的文件/知乎答題/百萬(wàn)/'): # for file in files: # print('發(fā)現(xiàn)圖片:' + file) # img = Image.open('E:/臨時(shí)接收的文件/知乎答題/百萬(wàn)/'+file) # ocr_subject(img) def yes_or_no(prompt, true_value='y', false_value='n', default=True): """ 檢查是否已經(jīng)為啟動(dòng)程序做好了準(zhǔn)備 """ 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 = '當(dāng)出現(xiàn)題目時(shí),請(qǐng)按下回車進(jìn)行識(shí)別 \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開(kāi)始 940結(jié)束 # 截取 距離上260 570結(jié)束 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("結(jié)束:" + 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('百度為您找到相關(guān)結(jié)果約')) start = i + 10 end = i + 25 page = page[start: end] return page def 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("開(kāi)始:" + 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 region def 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 list if __name__ == '__main__': main()
由于很多題目是下列哪個(gè)不是,所以我更喜歡用第一個(gè)方式,基本上識(shí)別時(shí)間在0.5-0.6秒之間。
最后里面的ocr_zh.py是可以用來(lái)抓取頭腦王者的輔助。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“python3.5+tesseract+adb如何實(shí)現(xiàn)西瓜視頻或頭腦王者輔助答題”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。