From 665fe06b7c8faa5d37a88da74e9373fdad6a45df Mon Sep 17 00:00:00 2001 From: xc Date: Sun, 14 Jan 2018 23:06:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化速度 1.不需要保存截图再读取,直接把图片二进制数据给PIL 2.之前的多线程处理有问题,实际上还是单线程,没有正确使用多线程 Signed-off-by: xc --- GetQuestionTessAndroid.py | 31 +++++++++++++++++++------------ common/screenshot.py | 26 ++++++++++++++++---------- 2 files changed, 35 insertions(+), 22 deletions(-) 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")