欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python腳本實現(xiàn)小猿口算

 更新時間:2024年10月22日 15:13:26   作者:枕流y  
最近小猿口算已經(jīng)被不少大學(xué)生攻占,一個好好的給小學(xué)生的口算題已經(jīng)變成了大學(xué)生的計算機(jī)大戰(zhàn),下面我們就來看看如何使用Python腳本就行吧小猿口算

代碼如下:

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

# 下面這行的路徑改成自己安裝的tesseract的路徑
pytesseract.pytesseract.tesseract_cmd = r'D:\Tesseract-OCR\tesseract.exe'

not_found_count = 0
last_not_found_time = 0
last_numbers = None  
skip_count = 0  

def capture_area():
    region = (1614, 312, 800, 298)  #這里改成自己屏幕里題目的位置(x,y,width,height)
    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, last_numbers, skip_count

    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 >= 25:
            pyautogui.click(280, 840)  # 點擊“開心收下”按鈕
            time.sleep(0.3)
            pyautogui.click(410, 990)  # 點擊“繼續(xù)”按鈕
            time.sleep(0.3)
            pyautogui.click(280, 910)  # 點擊“繼續(xù)PK”按鈕
            time.sleep(13)
            print("準(zhǔn)備重新開始程序...")
            time.sleep(0.3)
            main()
        return

    if last_numbers is not None and last_numbers == numbers:
        skip_count += 1
        print(f"當(dāng)前結(jié)果與上次相同,跳過此次執(zhí)行 (次數(shù): {skip_count})")

        if skip_count > 5:  # 超過5次則強(qiáng)制執(zhí)行一次
            skip_count = 0  # 重置計數(shù)器
            print("跳過次數(shù)超過5次,強(qiáng)制執(zhí)行一次")
            # 在這里可以直接執(zhí)行繪制邏輯,或根據(jù)需要處理
            first, second = numbers[0], numbers[1]
            origin_x, origin_y = 2015, 1125  # 繪制區(qū)域坐標(biāo)
            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)
        return

    first, second = numbers[0], numbers[1]
    origin_x, origin_y = 250, 650  # 繪制區(qū)域坐標(biāo)
    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
    last_numbers = numbers  # 更新 last_numbers 為當(dāng)前數(shù)字
    skip_count = 0  # 重置跳過次數(shù)


def draw_greater_than(origin_x, origin_y, size):
    pyautogui.press(".")  # 腳本快捷鍵,用于BlueStacks腳本管理器,這個是大于號的


def draw_less_than(origin_x, origin_y, size):
    pyautogui.press(",")  # 腳本快捷鍵,用于BlueStacks腳本管理器,這個是小于號的


def main():
    keyboard.add_hotkey('=', lambda: sys.exit("進(jìn)程已結(jié)束"))  # 默認(rèn)退出快捷鍵是 "="

    try:
        while True:
            image = capture_area()
            numbers = recognize_numbers(image)
            draw_comparison(numbers)
            time.sleep(0.7)  # 每次繪制及識別的延遲
    except SystemExit as e:
        print(e)


if __name__ == "__main__":
    main()

部署教程:

1.安裝 BlueStacks 5模擬器:

2.打開模擬器:繪制大于號小于號的腳本并綁定按鍵(大于號是’.’ 小于號是’,')

3.安裝tesseract(不會安裝的話csdn搜安裝教程),安裝完成后第十行代碼改成安裝好的tesseract的路徑

4.pycharm終端安裝所需要的庫:

pip install opencv-python pyautogui pytesseract keyboard numpy

5.用截圖工具查找坐標(biāo)并替換代碼中的坐標(biāo)

6. 運行程序

以上就是Python腳本實現(xiàn)小猿口算的詳細(xì)內(nèi)容,更多關(guān)于Python小猿口算的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論