ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

词达人模拟点击完成答题代码开源(基本都可以自动)

2020-04-18 19:03:03  阅读:3068  来源: 互联网

标签:答题 ## 开源 replace 词达人 cv print data options


本方法采用ocr文字识别获取坐标,然后与答案进行对比,点击屏幕

在这里插入图片描述

1.申请某度api

具体方法参考我之前的博客

2.fiddler配置参考我这个博客

3.可以去下方链接看效果

https://www.bilibili.com/video/BV1AQ4y1T7qx/

<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="m2I7tMjR-1587044549565" src="https://player.bilibili.com/player.html?aid=710343582"></iframe>

词达人利用python自动答题

4.对代码的解析(拒绝白嫖,从我做起)

## 2020.04.16
## jun_军
## qq:604803231
## blog主页:https://blog.csdn.net/weixin_44868057
## qq交流群:635032789 (进群密码:词达人)
##方便小白观看,我没有定义函数

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

标签:答题,##,开源,replace,词达人,cv,print,data,options
来源: https://blog.csdn.net/weixin_44868057/article/details/105567571

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有