1. 优化地主牌识别逻辑

2. 微调参数
This commit is contained in:
ZaneYork 2021-11-29 21:57:20 +08:00
parent ce06486c03
commit d0586b7d2f
5 changed files with 61 additions and 22 deletions

View File

@ -180,8 +180,8 @@ class GameHelper:
def Screenshot(self, region=None): # -> (im, (left, top)) def Screenshot(self, region=None): # -> (im, (left, top))
self.Handle = win32gui.FindWindow("Hlddz", None) self.Handle = win32gui.FindWindow("Hlddz", None)
hwnd = self.Handle hwnd = self.Handle
# im = Image.open(r"C:\Users\q9294\Desktop\Snipaste_2021-09-05_00-52-51.png") # im = Image.open(r"F:\DouZero\DouZero_For_HLDDZ_FullAuto\test.png")
# im = im.resize((1796, 1047)) # im = im.resize((1798, 1047))
# return im, (0,0) # return im, (0,0)
left, top, right, bot = win32gui.GetWindowRect(hwnd) left, top, right, bot = win32gui.GetWindowRect(hwnd)
width = right - left width = right - left
@ -214,7 +214,7 @@ class GameHelper:
else: else:
return None, (0, 0) return None, (0, 0)
def LocateOnScreen(self, templateName, region, confidence=0.9): def LocateOnScreen(self, templateName, region, confidence=0.85):
image, _ = self.Screenshot() image, _ = self.Screenshot()
return pyautogui.locate(needleImage=self.Pics[templateName], return pyautogui.locate(needleImage=self.Pics[templateName],
haystackImage=image, region=region, confidence=confidence) haystackImage=image, region=region, confidence=confidence)

1
debug.bat Normal file
View File

@ -0,0 +1 @@
python3 debug_screenshot.py

View File

@ -2,7 +2,7 @@ import GameHelper as gh
from GameHelper import GameHelper from GameHelper import GameHelper
GameHelper = GameHelper() GameHelper = GameHelper()
GameHelper.ScreenZoomRate = 1.25 GameHelper.ScreenZoomRate = 1.0
img, _ = GameHelper.Screenshot() img, _ = GameHelper.Screenshot()
img = gh.DrawRectWithText(img, (313, 747, 1144, 200), "test") img = gh.DrawRectWithText(img, (313, 747, 1144, 200), "test")
img.save("test.png") img.save("test.png")

73
main.py
View File

@ -45,7 +45,7 @@ AllCards = ['rD', 'bX', 'b2', 'r2', 'bA', 'rA', 'bK', 'rK', 'bQ', 'rQ', 'bJ', 'r
'b9', 'r9', 'b8', 'r8', 'b7', 'r7', 'b6', 'r6', 'b5', 'r5', 'b4', 'r4', 'b3', 'r3'] 'b9', 'r9', 'b8', 'r8', 'b7', 'r7', 'b6', 'r6', 'b5', 'r5', 'b4', 'r4', 'b3', 'r3']
helper = GameHelper() helper = GameHelper()
helper.ScreenZoomRate = 1.25 # 请修改屏幕缩放比 helper.ScreenZoomRate = 1.0 # 请修改屏幕缩放比
class MyPyQT_Form(QtWidgets.QWidget, Ui_Form): class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
def __init__(self): def __init__(self):
@ -63,9 +63,9 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
# 参数 # 参数
self.MyConfidence = 0.95 # 我的牌的置信度 self.MyConfidence = 0.95 # 我的牌的置信度
self.OtherConfidence = 0.9 # 别人的牌的置信度 self.OtherConfidence = 0.87 # 别人的牌的置信度
self.WhiteConfidence = 0.95 # 检测白块的置信度 self.WhiteConfidence = 0.95 # 检测白块的置信度
self.LandlordFlagConfidence = 0.9 # # 检测地主标志的置信度 self.LandlordFlagConfidence = 0.85 # # 检测地主标志的置信度
self.ThreeLandlordCardsConfidence = 0.9 # 检测地主底牌的置信度 self.ThreeLandlordCardsConfidence = 0.9 # 检测地主底牌的置信度
self.PassConfidence = 0.85 self.PassConfidence = 0.85
self.WaitTime = 1 # 等待状态稳定延时 self.WaitTime = 1 # 等待状态稳定延时
@ -73,6 +73,7 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
self.OtherFilter = 25 # 别人的牌检测结果过滤参数 self.OtherFilter = 25 # 别人的牌检测结果过滤参数
self.SleepTime = 0.1 # 循环中睡眠时间 self.SleepTime = 0.1 # 循环中睡眠时间
self.RunGame = False self.RunGame = False
print("self.RunGame = False 2")
self.AutoPlay = False self.AutoPlay = False
self.BidThreshold1 = 65 # 叫地主阈值 self.BidThreshold1 = 65 # 叫地主阈值
self.BidThreshold2 = 72 # 抢地主阈值 self.BidThreshold2 = 72 # 抢地主阈值
@ -93,9 +94,9 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
self.shouldExit = 0 # 通知上一轮记牌结束 self.shouldExit = 0 # 通知上一轮记牌结束
self.canRecord = threading.Lock() # 开始记牌 self.canRecord = threading.Lock() # 开始记牌
self.card_play_model_path_dict = { self.card_play_model_path_dict = {
'landlord': "baselines/douzero_ADP/landlord.ckpt", 'landlord': "baselines/resnet/landlord.ckpt",
'landlord_up': "baselines/douzero_ADP/landlord_up.ckpt", 'landlord_up': "baselines/resnet/landlord_up.ckpt",
'landlord_down': "baselines/douzero_ADP/landlord_down.ckpt" 'landlord_down': "baselines/resnet/landlord_down.ckpt"
} }
def init_display(self): def init_display(self):
@ -353,22 +354,27 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
result = helper.LocateOnScreen("finish_round", region=(810,840,200,80), confidence=0.8) result = helper.LocateOnScreen("finish_round", region=(810,840,200,80), confidence=0.8)
if result is not None: if result is not None:
helper.ClickOnImage("finish_round", region=(810,840,200,80), confidence=0.8) helper.ClickOnImage("finish_round", region=(810,840,200,80), confidence=0.8)
print("finish_round")
self.sleep(1000) self.sleep(1000)
result = helper.LocateOnScreen("next_round", region=(958,869,300,100), confidence=0.8) result = helper.LocateOnScreen("next_round", region=(958,869,300,100), confidence=0.8)
if result is not None: if result is not None:
helper.ClickOnImage("next_round", region=(958,869,300,100), confidence=0.8) helper.ClickOnImage("next_round", region=(958,869,300,100), confidence=0.8)
print("next_round")
self.sleep(1000) self.sleep(1000)
self.beforeStart() self.beforeStart()
result = helper.LocateOnScreen("yes_btn", region=(680, 661, 435, 225)) result = helper.LocateOnScreen("yes_btn", region=(680, 661, 435, 225))
if result is not None: if result is not None:
print("yes_btn")
helper.ClickOnImage("yes_btn", region=(680, 661, 435, 225)) helper.ClickOnImage("yes_btn", region=(680, 661, 435, 225))
self.sleep(1000) self.sleep(1000)
result = helper.LocateOnScreen("get_award_btn", region=(680, 661, 435, 225)) result = helper.LocateOnScreen("get_award_btn", region=(680, 661, 435, 225))
if result is not None: if result is not None:
print("get_award_btn")
helper.ClickOnImage("get_award_btn", region=(680, 661, 435, 225)) helper.ClickOnImage("get_award_btn", region=(680, 661, 435, 225))
self.sleep(1000) self.sleep(1000)
result = helper.LocateOnScreen("yes_btn_sm", region=(669, 583, 468, 100)) result = helper.LocateOnScreen("yes_btn_sm", region=(669, 583, 468, 100))
if result is not None: if result is not None:
print("yes_btn_sm")
helper.ClickOnImage("yes_btn_sm", region=(669, 583, 468, 100)) helper.ClickOnImage("yes_btn_sm", region=(669, 583, 468, 100))
self.sleep(200) self.sleep(200)
@ -382,18 +388,40 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
result = pyautogui.locateAll(needleImage=helper.Pics['o' + card], haystackImage=img, result = pyautogui.locateAll(needleImage=helper.Pics['o' + card], haystackImage=img,
confidence=self.ThreeLandlordCardsConfidence) confidence=self.ThreeLandlordCardsConfidence)
three_landlord_cards_real += card[1] * self.cards_filter(list(result), self.OtherFilter) three_landlord_cards_real += card[1] * self.cards_filter(list(result), self.OtherFilter)
if len(three_landlord_cards_real) > 3: delta = 0
three_landlord_cards_real = "" ratio = 0.05
for card in AllCards: direction = 0
result = pyautogui.locateAll(needleImage=helper.Pics['o' + card], haystackImage=img, for i in range(0, 15):
confidence=self.ThreeLandlordCardsConfidence + 0.05) if len(three_landlord_cards_real) > 3:
three_landlord_cards_real += card[1] * self.cards_filter(list(result), self.OtherFilter) if direction == 0:
if len(three_landlord_cards_real) < 3: delta = ratio
three_landlord_cards_real = "" elif direction == -1:
for card in AllCards: ratio /= 2
result = pyautogui.locateAll(needleImage=helper.Pics['o' + card], haystackImage=img, delta += ratio
confidence=self.ThreeLandlordCardsConfidence + 0.1) else:
three_landlord_cards_real += card[1] * self.cards_filter(list(result), self.OtherFilter) delta += ratio
direction = 1
three_landlord_cards_real = ""
for card in AllCards:
result = pyautogui.locateAll(needleImage=helper.Pics['o' + card], haystackImage=img,
confidence=self.ThreeLandlordCardsConfidence + delta)
three_landlord_cards_real += card[1] * self.cards_filter(list(result), self.OtherFilter)
elif len(three_landlord_cards_real) < 3:
if direction == 0:
delta = -ratio
elif direction == 1:
ratio /= 2
delta -= ratio
else:
delta -= ratio
direction = -1
three_landlord_cards_real = ""
for card in AllCards:
result = pyautogui.locateAll(needleImage=helper.Pics['o' + card], haystackImage=img,
confidence=self.ThreeLandlordCardsConfidence + delta)
three_landlord_cards_real += card[1] * self.cards_filter(list(result), self.OtherFilter)
else:
break
return three_landlord_cards_real return three_landlord_cards_real
def find_my_cards(self, pos): def find_my_cards(self, pos):
@ -447,6 +475,7 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
def stop(self): def stop(self):
try: try:
self.RunGame = False self.RunGame = False
print("self.RunGame = False")
self.env.game_over = True self.env.game_over = True
self.env.reset() self.env.reset()
self.init_display() self.init_display()
@ -465,12 +494,14 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
def beforeStart(self): def beforeStart(self):
self.RunGame = True self.RunGame = True
print("self.RunGame = True")
GameHelper.Interrupt = True GameHelper.Interrupt = True
HaveBid = False HaveBid = False
isTaodou = False isTaodou = False
is_stolen = 0 is_stolen = 0
while True: while True:
if not self.RunGame: if not self.RunGame:
print("return")
return return
outterBreak = False outterBreak = False
jiaodizhu_btn = helper.LocateOnScreen("jiaodizhu_btn", region=(765, 663, 116, 50)) jiaodizhu_btn = helper.LocateOnScreen("jiaodizhu_btn", region=(765, 663, 116, 50))
@ -485,6 +516,7 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
qiangdizhu_btn = helper.LocateOnScreen("qiangdizhu_btn", region=(783, 663, 116, 50)) qiangdizhu_btn = helper.LocateOnScreen("qiangdizhu_btn", region=(783, 663, 116, 50))
jiabei_btn = helper.LocateOnScreen("jiabei_btn", region=self.GeneralBtnPos) jiabei_btn = helper.LocateOnScreen("jiabei_btn", region=self.GeneralBtnPos)
if jiabei_btn is None: if jiabei_btn is None:
print("jiabei_btn_1")
img, _ = helper.Screenshot() img, _ = helper.Screenshot()
cards, _ = helper.GetCards(img) cards, _ = helper.GetCards(img)
cards_str = "".join([card[0] for card in cards]) cards_str = "".join([card[0] for card in cards])
@ -517,6 +549,7 @@ class MyPyQT_Form(QtWidgets.QWidget, Ui_Form):
print("淘豆场,跳过加倍") print("淘豆场,跳过加倍")
break break
else: else:
print("find_three_landlord_cards_1")
llcards = self.find_three_landlord_cards(self.ThreeLandlordCardsPos) llcards = self.find_three_landlord_cards(self.ThreeLandlordCardsPos)
print("地主牌:", llcards) print("地主牌:", llcards)
img, _ = helper.Screenshot() img, _ = helper.Screenshot()
@ -617,4 +650,8 @@ if __name__ == '__main__':
""") """)
my_pyqt_form = MyPyQT_Form() my_pyqt_form = MyPyQT_Form()
my_pyqt_form.show() my_pyqt_form.show()
# my_pyqt_form.find_three_landlord_cards((763, 37, 287, 136))
# my_pyqt_form.find_other_cards((463, 355, 380, 250));
# btn = helper.LocateOnScreen("jiabei_btn", region=(616, 631, 576, 117))
# my_pyqt_form.beforeStart()
sys.exit(app.exec_()) sys.exit(app.exec_())

1
start.bat Normal file
View File

@ -0,0 +1 @@
python3 main.py