Python腳本實現(xiàn)小猿口算
代碼如下:
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("準備重新開始程序...")
time.sleep(0.3)
main()
return
if last_numbers is not None and last_numbers == numbers:
skip_count += 1
print(f"當前結(jié)果與上次相同,跳過此次執(zhí)行 (次數(shù): {skip_count})")
if skip_count > 5: # 超過5次則強制執(zhí)行一次
skip_count = 0 # 重置計數(shù)器
print("跳過次數(shù)超過5次,強制執(zhí)行一次")
# 在這里可以直接執(zhí)行繪制邏輯,或根據(jù)需要處理
first, second = numbers[0], numbers[1]
origin_x, origin_y = 2015, 1125 # 繪制區(qū)域坐標
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ū)域坐標
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 為當前數(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("進程已結(jié)束")) # 默認退出快捷鍵是 "="
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.用截圖工具查找坐標并替換代碼中的坐標


6. 運行程序

以上就是Python腳本實現(xiàn)小猿口算的詳細內(nèi)容,更多關(guān)于Python小猿口算的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實現(xiàn)人性化顯示金額數(shù)字實例詳解
在本篇內(nèi)容里小編給大家整理了關(guān)于python實現(xiàn)人性化顯示金額數(shù)字實例內(nèi)容,需要的朋友們可以參考下。2020-09-09
Python實現(xiàn)RLE格式與PNG格式互轉(zhuǎn)
在機器視覺領(lǐng)域的深度學習中,很多數(shù)據(jù)集的標注文件使用RLE的格式。但是神經(jīng)網(wǎng)絡(luò)的輸入一定是一張圖片,為此必須把RLE格式的文件轉(zhuǎn)變?yōu)閳D像格式。本文將利用Python實現(xiàn)RLE格式與PNG格式互轉(zhuǎn),感興趣的可以了解一下2022-08-08
使用Python的數(shù)據(jù)可視化庫Matplotlib實現(xiàn)折線圖
數(shù)據(jù)可視化是數(shù)據(jù)分析和探索中不可或缺的一環(huán),本文將介紹如何使用Python中的數(shù)據(jù)可視化庫Matplotlib,通過示例代碼實現(xiàn)一個簡單的折線圖,感興趣的同學可以參考閱讀下2023-07-07
PyTorch中clone()、detach()及相關(guān)擴展詳解
這篇文章主要給大家介紹了關(guān)于PyTorch中clone()、detach()及相關(guān)擴展的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12

