Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化速度 #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions GetQuestionTessAndroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
# 用不同方法输出结果,取消某个方法在前面加上#

Expand All @@ -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('------------------------')
26 changes: 16 additions & 10 deletions common/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
from PIL import Image


# SCREENSHOT_WAY 是截图方法,经过 check_screenshot 后,会自动递减,不需手动修改
SCREENSHOT_WAY = 3

Expand All @@ -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():
Expand All @@ -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")
img = Image.open("./screenshot.png")