小猿口算python

大家好,好久不见,依旧是你们的霄霄呀。

最近网络上小猿口算非常的热门,今天我们也来参与一下吧。

1. 项目背景

在本项目中,我们使用了几个流行的库:

  • OpenCV:用于图像处理。
  • NumPy:用于数组和矩阵运算。
  • PyAutoGUI:用于自动化鼠标和键盘操作。
  • Pytesseract:用于光学字符识别(OCR),即从图像中提取文本。

这个代码的目的是在指定区域内截取屏幕图像,并识别其中的数字,接着根据识别结果绘制比较符号。

2. 代码分析

以下是代码的主要部分,逐步分析其功能。

2.1. 捕获图像区域

def capture_area():
    region = (207, 450, 550, 500)  # (x, y, width, height)
    screenshot = pyautogui.screenshot(region=region)
    return np.array(screenshot)

capture_area 函数使用 PyAutoGUI 的 screenshot 方法捕获屏幕的特定区域。该区域的坐标为 (x=207, y=450),宽度为550,高度为500。

2.2. 数字识别

def recognize_numbers(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
    text = pytesseract.image_to_string(thresh, config='--psm 6')
    numbers = [int(s) for s in text.split() if s.isdigit()]
    return numbers

recognize_numbers 函数中,首先将图像转换为灰度图像,然后应用二值化阈值处理。之后,使用 Pytesseract 的 image_to_string 方法提取文本,并过滤出数字。这里的 --psm 6 参数指定 Pytesseract 以单个统一文本块的方式进行识别。

2.3. 比较数字并绘制结果

def draw_comparison(numbers):
    global not_found_count, last_not_found_time

    if len(numbers) < 2:
        # 处理未找到足够数字的情况
        ...
        return

    first, second = numbers[0], numbers[1]
    origin_x, origin_y = 378, 994  # 绘制区域坐标
    size = 50

    if first > second:
        draw_greater_than(origin_x, origin_y, size)
    elif first < second:
        draw_less_than(origin_x, origin_y, size)

draw_comparison 函数根据识别到的数字进行比较。如果数字少于两个,则会记录未找到数字的次数。根据数字的大小关系,调用不同的绘制函数绘制“>”或“<”。

2.4 主函数

def main():
    keyboard.add_hotkey('=', lambda: sys.exit("进程已结束"))

    try:
        while True:
            image = capture_area()
            numbers = recognize_numbers(image)
            draw_comparison(numbers)
            time.sleep(0.5)
    except SystemExit as e:
        print(e)

if __name__ == "__main__":
    main()

main 函数中,程序进入一个无限循环,持续捕获图像、识别数字和绘制结果。可以通过按下“=”键退出程序。

3. 原理

该程序结合了多个技术:

  • 图像捕获:使用 PyAutoGUI 截取屏幕指定区域。
  • 图像处理:使用 OpenCV 处理图像,以提高字符识别的准确性。
  • 字符识别:通过 Pytesseract 提取图像中的数字。
  • 用户界面交互:通过 PyAutoGUI 模拟鼠标点击,实现交互反馈

 

4.完整代码 

import cv2
import numpy as np
import pyautogui
import pytesseract
import keyboard
import sys
import time

pytesseract.pytesseract.tesseract_cmd = r'D:\Tesseract-OCR\tesseract.exe'

not_found_count = 0
last_not_found_time = 0

def capture_area():
    region = (207, 450, 550, 500)  # (x, y, width, height) 坐标是由上到下,由左到右的;此坐标是识别区域坐标 左上:(60,260) 右下:(360,320)
    screenshot = pyautogui.screenshot(region=region)
    return np.array(screenshot)

def recognize_numbers(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
    text = pytesseract.image_to_string(thresh, config='--psm 6')
    numbers = [int(s) for s in text.split() if s.isdigit()]
    return numbers

def draw_comparison(numbers):
    global not_found_count, last_not_found_time

    if len(numbers) < 2:
        current_time = time.time()


        if not_found_count == 0 or current_time - last_not_found_time > 1:
            not_found_count = 1
        else:
            not_found_count += 1

        last_not_found_time = current_time
        print("未找到足够的数字进行比较")

        if not_found_count >= 26:

            pyautogui.click(376, 1102) # 此坐标是“开心收下”按钮的坐标
            time.sleep(0.3)
            pyautogui.click(566, 1307) # 此坐标是“继续”按钮的坐标
            time.sleep(0.3)
            pyautogui.click(402, 1203) # 此坐标是“继续PK”按钮的坐标
            time.sleep(13)

            print("准备重新开始程序...")
            time.sleep(1)
            main()
        return

    first, second = numbers[0], numbers[1]
    origin_x, origin_y = 378, 994  # 此坐标是绘制区域是坐标
    size = 50

    if first > second:
        print(f"{first} > {second}")
        draw_greater_than(origin_x, origin_y, size)
    elif first < second:
        print(f"{first} < {second}")
        draw_less_than(origin_x, origin_y, size)

    not_found_count = 0

def draw_greater_than(origin_x, origin_y, size):
    pyautogui.mouseDown(origin_x, origin_y)
    pyautogui.moveTo(origin_x + size, origin_y + size, duration=0.02)
    pyautogui.moveTo(origin_x, origin_y + size, duration=0.02)
    pyautogui.mouseUp()

def draw_less_than(origin_x, origin_y, size):
    pyautogui.mouseDown(origin_x + size, origin_y)
    pyautogui.moveTo(origin_x, origin_y + size, duration=0.02)
    pyautogui.moveTo(origin_x + size, origin_y + size, duration=0.02)
    pyautogui.mouseUp()

def main():
    keyboard.add_hotkey('=', lambda: sys.exit("进程已结束")) #默认退出快捷键是“=”

    try:
        while True:
            image = capture_area()
            numbers = recognize_numbers(image)
            draw_comparison(numbers)
            time.sleep(0.5) # 每次绘画及识别的延迟
    except SystemExit as e:
        print(e)

if __name__ == "__main__":
    main()

最后给大家来一个鼠标定位坐标的小代码进行配合把。

from pynput import mouse


def on_click(x, y, button, pressed):
    if pressed and button == mouse.Button.left:
        print(f'鼠标左键点击坐标: ({x}, {y})')

    # 创建鼠标监听器并设置回调函数


with mouse.Listener(on_click=on_click) as listener:
    # 监听鼠标事件,直到手动停止(例如按 Ctrl+C)
    listener.join()

好啦,拜拜,我们下次见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是萧萧吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值