diff --git a/GetQuestionTessAndroid.py b/GetQuestionTessAndroid.py index 76a50ad..8930170 100644 --- a/GetQuestionTessAndroid.py +++ b/GetQuestionTessAndroid.py @@ -3,21 +3,31 @@ # @Author : Skye # @Time : 2018/1/8 20:38 # @desc : 答题闯关辅助,截屏 ,OCR 识别,百度搜索 - - +import io from PIL import Image from common import screenshot, ocr, methods from threading import Thread +import traceback import time while True: + + go = input('输入回车继续运行,输入 n 回车结束运行: ') + if go == 'n': + break + # 截图 - screenshot.check_screenshot() + bScreenshot = screenshot.check_screenshot() - img = Image.open("./screenshot.png") + try: + image_file = io.BytesIO(bScreenshot) + img = Image.open(image_file) + # 文字识别 + question, choices = ocr.ocr_img(img) + except Exception: + print('识别失败', traceback.format_exc()) + continue - # 文字识别 - question, choices = ocr.ocr_img(img) # t = time.clock() # 用不同方法输出结果,取消某个方法在前面加上# @@ -29,17 +39,14 @@ # methods.run_algorithm(2, question, choices) # 多线程 - m1 = Thread(methods.run_algorithm(0, question, choices)) - m2 = Thread(methods.run_algorithm(1, question, choices)) - m3 = Thread(methods.run_algorithm(2, question, choices)) + m1 = Thread(target=methods.run_algorithm, args=(0, question, choices)) + m2 = Thread(target=methods.run_algorithm, args=(1, question, choices)) + m3 = Thread(target=methods.run_algorithm, args=(2, question, choices)) m1.start() m2.start() m3.start() # end_time = time.clock() # print(end_time - t) - go = input('输入回车继续运行,输入 n 回车结束运行: ') - if go == 'n': - break print('------------------------') diff --git a/common/screenshot.py b/common/screenshot.py index 66a69c0..a7aeeb9 100644 --- a/common/screenshot.py +++ b/common/screenshot.py @@ -7,7 +7,6 @@ import sys from PIL import Image - # SCREENSHOT_WAY 是截图方法,经过 check_screenshot 后,会自动递减,不需手动修改 SCREENSHOT_WAY = 3 @@ -27,15 +26,21 @@ def pull_screenshot(): binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n') # binary_screenshot = binary_screenshot.split(b' ') # binary_screenshot = binary_screenshot[len(binary_screenshot) - 1] - #print(binary_screenshot) + # print(binary_screenshot) elif SCREENSHOT_WAY == 1: binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n') - f = open('screenshot.png', 'wb') - f.write(binary_screenshot) - f.close() + return binary_screenshot + # f = open('screenshot.png', 'wb') + # f.write(binary_screenshot) + # f.close() elif SCREENSHOT_WAY == 0: os.system('adb shell screencap -p /sdcard/screenshot.png') os.system('adb pull /sdcard/screenshot.png .') + file = open('./screenshot.png', 'b') + try: + return file.read() + finally: + file.close() def check_screenshot(): @@ -51,14 +56,15 @@ def check_screenshot(): if SCREENSHOT_WAY < 0: print('暂不支持当前设备') sys.exit() - pull_screenshot() - try: - Image.open('./screenshot.png').load() + screenshot = pull_screenshot() + if screenshot is not None: print('采用方式 {} 获取截图'.format(SCREENSHOT_WAY)) - except Exception: + return screenshot + else: SCREENSHOT_WAY -= 1 check_screenshot() + if __name__ == '__main__': check_screenshot() - img = Image.open("./screenshot.png") \ No newline at end of file + img = Image.open("./screenshot.png")