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

兼容多个adb设备的情况 #136

Open
wants to merge 6 commits 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
67 changes: 67 additions & 0 deletions GetQuestionAndroid.old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-

# @Author : Skye
# @Time : 2018/1/8 20:38
# @desc : 答题闯关辅助,截屏 ,OCR 识别,百度搜索


from PIL import Image
from common import screenshot, ocr, methods
from threading import Thread
import time
import configparser

# 读取配置文件
config = configparser.ConfigParser()
config.read('./config/configure.conf', encoding='utf-8')


while True:
# 截图
t = time.clock()
# screenshot.check_screenshot()

#end_time = time.clock()
#print(end_time - t)

img = Image.open("./screenshot.png")

# 文字识别,可选 Tesseract 和 Baidu ,请在 config/configure.conf 中进行相应配置

#ocr_img: 需要分别截取题目和选项区域,使用 Tesseract
#ocr_img_tess: 题目和选项一起截,使用 Tesseract
#ocr_img_baidu: 题目和选项一起截,使用 baidu ocr,需配置 key

# question, choices = ocr.ocr_img(img, config)
# question, choices = ocr.ocr_img_tess(img, config)
question, choices = ocr.ocr_img_baidu(img, config)

#end_time2 = time.clock()
#print(end_time2 - end_time)

# 用不同方法输出结果,取消某个方法在前面加上#

# # 打开浏览器方法搜索问题
# methods.run_algorithm(0, question, choices)
# # 将问题与选项一起搜索方法,并获取搜索到的结果数目
# methods.run_algorithm(1, question, choices)
# # 用选项在问题页面中计数出现词频方法
# methods.run_algorithm(2, question, choices)

# 多线程
if question and 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.start()
m2.start()
m3.start()

end_time3 = time.clock()
print('用时: {0}'.format(end_time3 - t))

go = input('输入回车继续运行,输入 n 回车结束运行: ')
if go == 'n':
break

print('------------------------')
69 changes: 36 additions & 33 deletions GetQuestionAndroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,37 @@
# @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
import configparser

# 读取配置文件
config = configparser.ConfigParser()
config.read('./config/configure.conf', encoding='utf-8')


while True:
# 截图
t = time.clock()
screenshot.check_screenshot()

#end_time = time.clock()
#print(end_time - t)

img = Image.open("./screenshot.png")

# 文字识别,可选 Tesseract 和 Baidu ,请在 config/configure.conf 中进行相应配置

#ocr_img: 需要分别截取题目和选项区域,使用 Tesseract
#ocr_img_tess: 题目和选项一起截,使用 Tesseract
#ocr_img_baidu: 题目和选项一起截,使用 baidu ocr,需配置 key

# question, choices = ocr.ocr_img(img, config)
question, choices = ocr.ocr_img_tess(img, config)
# question, choices = ocr.ocr_img_baidu(img, config)

#end_time2 = time.clock()
#print(end_time2 - end_time)

t = time.clock()
# 截图
bScreenshot = screenshot.check_screenshot()
# bScreenshot.save(io.BytesIO(),'PNG')
# bScreenshot.seek(0)
# bScreenImg = bScreenshot.read()
try:
image_file = io.BytesIO(bScreenshot)
img = Image.open(image_file)
# 文字识别
question, choices = ocr.ocr_img_baidu(img, config)
except Exception:
print('识别失败', traceback.format_exc())
continue

# t = time.clock()
# 用不同方法输出结果,取消某个方法在前面加上#

# # 打开浏览器方法搜索问题
Expand All @@ -49,18 +44,26 @@
# 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.start()
m2.start()
m3.start()

end_time3 = time.clock()
print('用时: {0}'.format(end_time3 - t))
# if question and 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()
if question and 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.start()
m2.start()
m3.start()

end_time = time.clock()
print('用时: {0}'.format(end_time - t))

go = input('输入回车继续运行,输入 n 回车结束运行: ')
if go == 'n':
break

print('------------------------')
print('------------------------')
34 changes: 25 additions & 9 deletions common/methods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-

# @Author : Skye
# @Time : 2018/1/9 10:39
Expand All @@ -7,9 +7,10 @@
import requests
import webbrowser
import urllib.parse
from pyquery import PyQuery as pq

# # 颜色兼容Win 10
from colorama import init,Fore
from colorama import init,Fore,Back
init()

def open_webbrowser(question):
Expand Down Expand Up @@ -37,18 +38,31 @@ def open_webbrowser_count(question,choices):
def count_base(question,choices):
print('\n-- 方法3: 题目搜索结果包含选项词频计数法 --\n')
# 请求
question = '寒食节是为了纪念谁'
choices = ['屈原','介之推','鲁迅']
req = requests.get(url='http://www.baidu.com/s', params={'wd':question})
content = req.text
#print(content)
doc = pq(content)
content = doc.find('#content_left').html()
baike_content = doc.find('.op_exactqa_main').html()
counts = []
baike_recommend = ''
print('Question: '+question)
if '不是' in question:
print('**请注意此题为否定题,选计数最少的**')
for i in range(len(choices)):
counts.append(content.count(choices[i]))
if baike_content and baike_content.count(choices[i]):
baike_recommend = choices[i]
#print(choices[i] + " : " + str(counts[i]))
output(choices, counts)

if baike_recommend:
print()
print(Fore.YELLOW + '{0}:{1}'.format('百科推荐', baike_recommend) + Fore.RESET)
print()


def output(choices, counts):
counts = list(map(int, counts))
#print(choices, counts)
Expand All @@ -62,22 +76,24 @@ def output(choices, counts):
if index_max == index_min:
print(Fore.RED + "高低计数相等此方法失效!" + Fore.RESET)
return

print(Back.WHITE)
for i in range(len(choices)):
print()
if i == index_max:
# 绿色为计数最高的答案
print(Fore.GREEN + "{0} : {1} ".format(choices[i], counts[i]) + Fore.RESET)
print(Fore.GREEN + "选项 {0} ---- {1} : {2} ".format(str(i + 1), choices[i], counts[i]) + Fore.RESET)
elif i == index_min:
# 红色为计数最低的答案
print(Fore.MAGENTA + "{0} : {1}".format(choices[i], counts[i]) + Fore.RESET)
print(Fore.MAGENTA + "选项 {0} ---- {1} : {2}".format(str(i + 1), choices[i], counts[i]) + Fore.RESET)
else:
print("{0} : {1}".format(choices[i], counts[i]))

print(Fore.BLACK + "选项 {0} ---- {1} : {2}".format(str(i + 1), choices[i], counts[i]) + Fore.RESET)

print(Back.RESET)

def run_algorithm(al_num, question, choices):
if al_num == 0:
open_webbrowser(question)
# open_webbrowser(question)
pass
elif al_num == 1:
open_webbrowser_count(question, choices)
elif al_num == 2:
Expand Down
31 changes: 15 additions & 16 deletions common/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,35 +177,34 @@ def ocr_img_baidu(image, config):
#region_im = region_im.convert('L')

# 把图片变成二值图像
#region_im = binarizing(region_im, 190)
#region_im.show()
# region_im = binarizing(region_im, 190)
# region_im.show()
img_byte_arr = io.BytesIO()
region_im.save(img_byte_arr, format='PNG')
image_data = img_byte_arr.getvalue()
# base64_data = base64.b64encode(image_data)
response = client.basicGeneral(image_data)
#print(response)
words_result = response['words_result']

texts = [x['words'] for x in words_result]
# print(texts)
if len(texts) > 2:
question = texts[0]
choices = texts[1:]
question = ''.join(texts[0:-3])
choices = texts[-3:]
choices = [x.replace(' ', '') for x in choices]
else:
print(Fore.RED + '截图区域设置错误,请重新设置' + Fore.RESET)
exit(0)
return ('','')
# exit(0)

# 处理出现问题为两行或三行
if choices[0].endswith('?'):
question += choices[0]
choices.pop(0)
elif choices[1].endswith('?'):
question += choices[0]
question += choices[1]
choices.pop(0)
choices.pop(0)
# if choices[0].endswith('?'):
# question += choices[0]
# choices.pop(0)
# elif choices[1].endswith('?'):
# question += choices[0]
# question += choices[1]
# choices.pop(0)
# choices.pop(0)

return question, choices

Expand Down
85 changes: 85 additions & 0 deletions common/screenshot.old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
"""
手机屏幕截图的代码 ,用管道或取图片数据比直接传输图片快,来自 https://github.com/wangshub/wechat_jump_game
"""
import subprocess
import os
import sys
from PIL import Image
import configparser

# 读取配置文件
config = configparser.ConfigParser()
config.read('./config/configure.conf', encoding='utf-8')


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


def pull_screenshot():
"""
获取屏幕截图,目前有 0 1 2 3 四种方法,未来添加新的平台监测方法时,
可根据效率及适用性由高到低排序
"""
global SCREENSHOT_WAY

# 兼容多个adb设备的情况
device_cmd = ''
try:
devices = os.popen('adb devices').readlines()[1:-1]
devices_count = len(devices)
if devices_count > 1:
adb_device = config.get('device', 'adb_device')
device_cmd = ' -s ' + adb_device
elif devices_count == 0:
print('当前暂无已连接的adb设备')
exit(0)
except Exception:
pass

if 1 <= SCREENSHOT_WAY <= 3:
process = subprocess.Popen(
'adb' + device_cmd + ' shell screencap -p',
shell=True, stdout=subprocess.PIPE)
binary_screenshot = process.stdout.read()
if SCREENSHOT_WAY == 2:
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)
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()
elif SCREENSHOT_WAY == 0:
os.system('adb' + device_cmd +
' shell screencap -p /sdcard/screenshot.png')
os.system('adb' + device_cmd + ' pull /sdcard/screenshot.png .')


def check_screenshot():
"""
检查获取截图的方式
"""
global SCREENSHOT_WAY
if os.path.isfile('screenshot.png'):
try:
os.remove('screenshot.png')
except Exception:
pass
if SCREENSHOT_WAY < 0:
print('暂不支持当前设备')
sys.exit()
pull_screenshot()
try:
Image.open('./screenshot.png').load()
print('采用方式 {} 获取截图'.format(SCREENSHOT_WAY))
except Exception:
SCREENSHOT_WAY -= 1
check_screenshot()

if __name__ == '__main__':
check_screenshot()
img = Image.open("./screenshot.png")
Loading