(未经许可,禁止转载)
具体方法参考我之前的博客
## 2020.04.16
## jun_军
## qq:604803231
## blog主页:https://blog.csdn.net/weixin_44868057
## qq交流群 719352964 (进群密码:bug)
##方便小白观看,我没有定义函数
from aip import AipOcr
import json
import pyautogui
import numpy as np
import cv2 as cv
import time
import os
try:
while True:
##1截图以及图片处理
img = pyautogui.screenshot(region=[0,0,592,1040]) # x,y,w,h
img.save('1/answer.jpg')
## 2图片灰度处理
def access_pixels(image):
height, width, channels = image.shape
print("width:%s,height:%s,channels:%s" % (width, height, channels))
for row in range(height):
for list in range(width):
for c in range(channels):
pv = image[row, list, c]
image[row, list, c] = 255 - pv
# cv.imshow("AfterDeal", image)
src = cv.imread('1/answer.jpg')
# cv.imshow('OriginalImage', src)
# 获取当前系统以运行的周期数
t1 = cv.getTickCount()
# 运行函数
access_pixels(src)
# 再次获取当前系统以运行的周期数
t2 = cv.getTickCount()
# 计时周期数差值除以每秒周期数,获得处理时间
print("time cost:%s s" % ((t2 - t1) / cv.getTickFrequency()))
cv.imwrite('2/answer.jpg',src)
## 3图片放大10倍
# 已知数据集位置
indir = "2/"
# 保存的数据集位置
outdir = "3/"
cout = 0
for fileName in os.listdir(indir):
if fileName == '.DS_Store':
continue
cout = cout + 1
# print(cout)
# print(fileName)
img_dir = os.path.join(indir, fileName) # 获取图片的名字
# print(img_dir)
src = cv.imread(img_dir) # 读取图片
h1, w1 = src.shape[:2]
# print(h1, w1) # 打印长和宽
r = 1 / 10 # 缩小倍数
# 进行缩放
dst = cv.resize(src, (int(w1 / r), int(h1 / r)), interpolation=cv.INTER_CUBIC) # 使用双立方插值
h2, w2 = dst.shape[:2]
# print(h2, w2) # 打印长和宽
# print(outdir + fileName)
cv.imwrite(outdir + fileName, dst)
## 4图片显示增强
I = cv.imread('3/answer.jpg', flags=0)
aa = 2
Oa = I * float(aa)
Oa[Oa > 255] = 255
Oa = np.round(Oa)
Oa = Oa.astype(np.uint8)
cv.imwrite('4/answer.jpg', Oa)
##5.解析本地json文件答案
type = 0
lodec = 'responseBody.txt'
# def getAnswer():
# print('搜寻答案....')
#hhh=[]
answers = []
data = json.loads(open(lodec, "r", encoding='utf-8').read())
# 多选
if data['data']['topic_mode'] in [31]:
options = data['data']['options']
for option in options:
if option['answer'] == True:
answers.append(option['content'].replace('adj','').replace('adv','').replace('noun','').replace(';','').replace(',','').replace('verb',''))
# 单选
if data['data']['topic_mode'] in [11, 22, 42]:
options = data['data']['options']
for option in options:
if option['answer'] == True:
answers.append(
option['content'].replace(' ', '').replace(';', ';').replace(',', ',').replace('.', '').replace('…',
'').replace('adj','').replace('adv','').replace('noun','').replace('verb','').replace(';','').replace(',','').replace('写',''))
# 选词
if data['data']['topic_mode'] in [32]:
options = data['data']['answer_content']['answer_arr']
for option in options:
# print(option)
answers.append(option.replace('\'','').replace('j','').replace('l','').replace('J',''))
# 填空
if data['data']['topic_mode'] in [51]:
type = 1
answers.append(data['data']['answer_content'])
for a in answers:
print(a)
# return answers
print("答案:",answers)
##6.ocr识别
""" 你的 APPID AK SK 图2的内容"""
APP_ID = '*********'
API_KEY = '************************'
SECRET_KEY = '************************'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
fname = './4/answer.jpg'
# fname = 'screenshot.png'
# """ 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#
image = get_file_content(fname)
""" 调用通用文字识别, 图片参数为本地图片 """
client.general(image)
# options = {}
# options["recognize_granularity"] = "big"
# options["language_type"] = "CHN_ENG"
# options["detect_direction"] = "true"
# options["detect_language"] = "true"
# options["vertexes_location"] = "true"
# options["probability"] = "true"
results = client.general(image)["words_result"] # 还可以使用身份证驾驶证模板,直接得到字典对应所需字段
h = []
t = []
for result in results:
text = result["words"].replace(' ','').replace('adj','').replace('adv','').replace('I','').replace('gan√','gain').replace("I","").replace('j','').replace('J','').replace('i','')\
.replace('noun','').replace('verb','').replace('ver','').replace('ad','').replace(';','').replace(',','').replace('写','')
b= (result["location"]['left']/int('10')+int('10'),result["location"]['top']/int('10')+int('10'))
h.append(text)
t.append(b)
# e = (text)
# text = print(result["words"].replace(';', '')) # 只有中文英文
#
# # f = print(a.replace('None','')) #
# location = result["location"]
# # a= [result["location"]['left'],result["location"]['top']]
# c= list(text)+list(b)
# text1=list(text)+list(b)
#
# # # print(re.findall(r"\d+\.?\d*",text))
# # content = list("".join(a)) + list(b)
# t=[]
# print('识别到的坐标:',t)
print("识别的:",h)
if len(answers)>200:
break
#
# if len(list(h)) <100:
# tmp = [val for val in h if val in answers] # 列表推导式求的两个列表的交集
# pass
## 已知的答案与识别到的进行对比,然后将坐标赋值给我们要的答案
else:
i=0
q=0
zuobiao =[]
while i < len(answers):
p= h.index(answers[i])
zuobiao = list(t[p])
# i +=1
# print(zuobiao)
x=int(zuobiao[0])
y=(zuobiao[1])
# print(x)
# print(y)
# PyMouse.click(x,y)
# pyautogui.moveTo(x, y, duration=1)
pyautogui.click(x, y)
i += 1
time.sleep(1)
time.sleep(1)
k = 0
while k<2:
pyautogui.click(523, 1000)
k +=1
# time.sleep(1)
time.sleep(3)
except:
print("情况1:仔细看我的博客,和前面的1-4点内容创建文件夹,\n情况2::当前的页面不在答题界面 \n情况3:如果出现不能自动答题,一定要联系群主")
我已经程序打包,如若需要可前往下载,感谢支持
链接:https://pan.baidu.com/s/1ZTbo9-aJuM887YfzqLtKXw
提取码:3el3