import
traceback
import
numpy as np
from
paddleocr
import
PaddleOCR
from
pyautogui
import
*
import
win32gui
import
win32api
import
logging
import
pynput, time
import
keyboard
import
loguru
import
pathlib
import
datetime
import
os
from
concurrent.futures
import
ThreadPoolExecutor, as_completed, ALL_COMPLETED, wait
log_dir
=
pathlib.Path(
'./logs'
)
log_dir.mkdir(exist_ok
=
True
)
if
not
os.path.exists(f
"/logs/"
):
os.mkdir(f
"/logs/"
)
current_date
=
datetime.datetime.now().strftime(
'%Y-%m-%d'
)
if
not
os.path.exists(f
"/logs/{current_date}/"
):
os.mkdir(f
"/logs/{current_date}/"
)
log_file
=
log_dir.joinpath(f
'./logs/{current_date}/'
+
"log_{time: YYYY_MM_DD}.log"
)
log
=
loguru.logger
pool
=
ThreadPoolExecutor(max_workers
=
1
)
logging.disable(logging.DEBUG)
logging.disable(logging.WARNING)
def
ocr_hero_buy(heroname, windowTitle
=
"腾讯手游助手(64位)"
, isClick
=
True
):
time.sleep(
0.25
)
if
type
(heroname)
=
=
str
:
heroname
=
[heroname]
def
get_window_rect(window_title):
hwnd
=
win32gui.FindWindow(
0
, window_title)
if
hwnd:
rect
=
win32gui.GetWindowRect(hwnd)
return
rect
return
None
rect
=
get_window_rect(windowTitle)
if
rect:
StartLeft, StartTop, right, bottom
=
rect
width
=
right
-
StartLeft
height
=
bottom
-
StartTop
else
:
StartLeft, StartTop, right, bottom
=
0
,
0
,
1
,
1
width
=
right
-
StartLeft
height
=
bottom
-
StartTop
image
=
screenshot(region
=
(StartLeft, StartTop, width, height))
heroBoxheight
=
image.height
*
0.95
heroBoxwidth
=
image.width
*
0.2
StartLeft
=
StartLeft
+
heroBoxwidth
StartTop
=
StartTop
+
heroBoxheight
image
=
image.crop((
int
(heroBoxwidth),
int
(heroBoxheight), image.width, image.height))
oimage
=
image
image
=
np.array(image)
result
=
ocr.ocr(image,
cls
=
False
)
point
=
win32api.GetCursorPos()
if
result
is
not
None
:
for
line
in
result:
if
line
is
not
None
:
for
word
in
line:
log.error(word)
for
hero
in
heroname:
if
str
(word).find(hero) >
-
1
or
heroname
=
=
"":
log.success(
f
"发现英雄牌:{hero},坐标位置,x:{word[0][0][0] + StartLeft},y:{word[0][0][1] + StartTop}"
)
if
isClick:
moveTo(word[
0
][
0
][
0
]
+
StartLeft, word[
0
][
0
][
1
]
+
StartTop)
click()
return
""
if
__name__
=
=
'__main__'
:
windowName
=
"腾讯手游助手(64位)"
heros
=
[
'雷克顿'
,
'莎弥拉'
,
'卡西奥佩娅'
,
'崔丝塔娜'
,
'烬'
]
def
keyboardDown():
currentWindow(windowName)
def
on_click(x, y, button, pressed):
if
not
pressed:
currentWindow(windowName)
return
True
def
currentWindow(windowTitle):
point
=
win32api.GetCursorPos()
hwnd
=
win32gui.WindowFromPoint(point)
hwnd
=
win32gui.GetParent(hwnd)
if
hwnd
=
=
0
:
hwnd
=
win32gui.WindowFromPoint(point)
title
=
win32gui.GetWindowText(hwnd)
if
title
=
=
windowTitle:
pool.submit(
lambda
cxp: ocr_hero_buy(
*
cxp),
(heros, windowTitle,
True
))
log.success(
"正在载入OCR金铲铲自动识别选牌工具,正在初始化PaddleOCR模块"
)
ocr
=
PaddleOCR(use_angle_cls
=
False
, lang
=
"ch"
, use_gpu
=
False
, debug
=
False
, show_log
=
False
)
log.success(
"PaddleOCR模块初始化成功,您可以在游戏里按下D键或者鼠标任意键来触发自动选牌"
)
keyboard.add_hotkey(
'd'
, keyboardDown)
with pynput.mouse.Listener(
on_click
=
on_click) as listener:
listener.join()