基于OCR的小猿口算比大小脚本

基于OCR的小猿口算比大小脚本_第1张图片

刚做完OCR,赛季就更新到网络信息安全攻防战了。。

视觉已死,抓包当道,

你要问我怎么抓包?嘿嘿,不会

环境

IntelliJ IDEA Community Edition 2024.2.1

安装python插件:新建项目---通过插件获取更多语言---python

Pycharm也是可以的,只是我没装

安装一个模拟器,没有要求,能运行手机程序就行

在模拟器里运行小猿口算

安装OCR

这里是链接tesseract-ocr · GitHub,如果进不去,挂VPN

需要引入的库

在python环境下运行以下代码,安装需要的软件包

pip install pytesseract
pip install opencv-python
pip install numpy
pip install pyautogui

在py文件开始,引入需要的库

from operator import ifloordiv
from time import sleep
import pytesseract
from PIL import ImageGrab
import cv2
import numpy as np
import pyautogui

代码

from operator import ifloordiv
from time import sleep
import pytesseract
from PIL import ImageGrab
import cv2
import numpy as np
import pyautogui

start_x, start_y = 275, 659
end_x, end_y = 275, 705
left_x, left_y = 258, 682
right_x, right_y = 291, 682

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def capture_and_recognize(x1, y1, x2, y2):
    screen = ImageGrab.grab(bbox=(x1, y1, x2, y2))
    img = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2GRAY)
    _, img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
    custom_config = r'--oem 3 --psm 6 outputbase digits'  # 只提取数字
    digits = pytesseract.image_to_string(img, config=custom_config)
    return digits.strip()

if __name__ == "__main__":
    while True:
        i = 10
        while i != 0:
            i = i - 1
            left = capture_and_recognize(200, 280, 270, 330)
            right = capture_and_recognize(330, 280, 400, 320)
            print("识别出的数字:", left + '  ' + right)
            x = int(left)
            y = int(right)
            if x > y:
                pyautogui.moveTo(start_x, start_y)
                pyautogui.mouseDown()
                pyautogui.moveTo(right_x, right_y)
                pyautogui.moveTo(end_x, end_y)
                pyautogui.mouseUp()
            else:
                pyautogui.moveTo(start_x, start_y)
                pyautogui.mouseDown()
                pyautogui.moveTo(left_x, left_y)
                pyautogui.moveTo(end_x, end_y)
                pyautogui.mouseUp()
            sleep(0.31)

        sleep(5)
        pyautogui.moveTo(292, 554)
        pyautogui.mouseDown()
        pyautogui.mouseUp()

        sleep(2)
        pyautogui.moveTo(426, 1035)
        pyautogui.mouseDown()
        pyautogui.mouseUp()

        sleep(2)
        pyautogui.moveTo(290, 904)
        pyautogui.mouseDown()
        pyautogui.mouseUp()

        sleep(13)


代码详解

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

这里将第一步安装OCR的文件路径放进来,注意是tesseract.exe文件的路径

start_x, start_y = 275, 659
end_x, end_y = 275, 705
left_x, left_y = 258, 682
right_x, right_y = 291, 682

控制鼠标绘制大于小于号,x,y代表鼠标位于屏幕上像素点的位置,这个根据自己的需要更改

def capture_and_recognize(x1, y1, x2, y2):
    screen = ImageGrab.grab(bbox=(x1, y1, x2, y2))
    img = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2GRAY)
    _, img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
    custom_config = r'--oem 3 --psm 6 outputbase digits'  # 只提取数字
    digits = pytesseract.image_to_string(img, config=custom_config)
    return digits.strip()

定义了一个函数,函数功能为:

取一个矩形,x1,y1是矩形左上角坐标,x2,y2是矩形右下角坐标,

图像锐化

图像二值化,最小灰度值200,最大255

只提取数字

返回一个字符串

left = capture_and_recognize(200, 280, 270, 330)
right = capture_and_recognize(330, 280, 400, 320)
        print("识别出的数字:", left + '  ' + right)
        x = int(left)
        y = int(right)

识别左边的数字及右边的数字,并转换成整型数据,选取的识别区域根据自己的需求更改

pyautogui.moveTo(start_x, start_y)

将鼠标移动至指定位置

pyautogui.mouseDown()
pyautogui.mouseUp()

鼠标左键按下与释放

后面都是很简单的判断加等待,就不解释了

运行结果

自此,这个简单的OCR识别程序就可以运行了

目前速度大概1秒1题,受限于博主的能力(还有懒),不会优化了

由于OCR是神经网路算法,并不是很稳定,有时候识别不出来是正常情况,重开就好

基于OCR的小猿口算比大小脚本_第2张图片

你可能感兴趣的:(ocr)