def get_colors(self, image, card_num, start_loc, width, debug=False): colors = [] for i in range(card_num): total = [0, 0, 0] avg = [0, 0, 0] count = 0 for j in range(width): for k in range(20): pixel = image.getpixel( (start_loc[0] + i * width + j, start_loc[1] + k)) if pixel[0] > 200 or pixel[0] < 20: continue total[0] += pixel[0] total[1] += pixel[1] total[2] += pixel[2] count += 1 avg[0] = total[0] / count avg[1] = total[1] / count avg[2] = total[2] / count if debug: loggers.debug("avg:" + str(avg)) # 0: 灰色 1: 红色 2: 绿色 3: 蓝色 if abs(avg[0] - avg[1]) < 10 and abs(avg[0] - avg[2]) < 10: colors.append(0) elif avg[0] > avg[1] and avg[0] > avg[2]: colors.append(1) elif avg[1] > avg[0] and avg[1] > avg[2]: colors.append(2) elif avg[2] > avg[0] and avg[2] > avg[1]: colors.append(3) return colors
def get_public_card(self, image, debug=False): ls = self.pixel_info["public_left_start"] ts = self.pixel_info["public_top_start"] box = (ls, ts, ls + self.pixel_info["card_width"] * 5, ts + self.pixel_info["card_len"]) image_for_num = image.crop(box).convert('L') if debug: image_for_num.show() content = pytesseract.image_to_string( image_for_num, config=("-c tessedit" "_char_whitelist=AJKQZ123456789" " --psm 10" " -l osd" " ")).strip() if debug: loggers.debug("public picture result: {0}".format(content)) cards = self.str_to_cards(content) if len(cards) < 3: cards = [] start_loc = (self.pixel_info["public_left_start"], self.pixel_info["public_top_start"]) colors = self.get_colors(image, len(cards), start_loc, wpk_pixel_info["card_width"]) return cards, colors
def get_player_left_chip(self, image, loc, debug=False): ls = self.pixel_info["player_left_chip"][str(loc)][0] ts = self.pixel_info["player_left_chip"][str(loc)][1] width = self.pixel_info["player_left_chip"][str(loc)][2] height = self.pixel_info["player_left_chip"][str(loc)][3] box = (ls, ts, ls + width, ts + height) image_for_flod = image.crop(box) image_for_flod = self.image_to_2(image_for_flod, 210) if debug: image_for_flod.show() content = pytesseract.image_to_string(image_for_flod, config="-psm 10000").strip() if debug: loggers.debug("{0} get_player_left_chip raw chip: {1}".format( loc, content)) content = content.replace("I", "1", 10) content = content.replace("l", "1", 10) content = content.replace("Z", "2", 10) content = content.replace("O", "0", 10) num = "".join(filter(lambda ch: ch in '0123456789.', content)) if debug: loggers.debug("{0} get_player_left_chip raw chip: {1}".format( loc, content)) try: result = float(num) return result except: return 0
def get_player_info(self, image, loc, debug=False): if self.isFold(image, loc, debug): if debug: loggers.debug("{0} Flod".format(loc)) return -1 else: chip_num = self.get_player_put_chip(image, loc, debug) / big_blind if debug: loggers.debug("{0} put chip {1}".format(loc, chip_num)) return chip_num
def get_self_card(self, image, debug=False): # 图像识别 ls = self.pixel_info["self_left_start"] ts = self.pixel_info["self_top_start"] box = (ls, ts, ls + self.pixel_info["card_width"] * 2, ts + self.pixel_info["card_len"]) image_for_num = image.crop(box) image_for_num = self.image_to_2(image_for_num, 180) if debug: image_for_num.show() content = pytesseract.image_to_string( image_for_num, config=("-c tessedit" "_char_whitelist=AJKQZ123456789" " --psm 10" " -l osd" " ")).strip() loggers.debug("picture result: {0}".format(content)) # 牌型转换 cards = self.str_to_cards(content) cards.sort(reverse=True) loggers.debug("to cards:{0}".format(cards)) # 颜色识别 start_loc = (self.pixel_info["self_left_start"], self.pixel_info["self_top_start"]) colors = self.get_colors(image, len(cards), start_loc, wpk_pixel_info["card_width"], debug) loggers.debug("colors:{0}".format(colors)) return cards, colors
def isFold(self, image, loc, debug=False): if loc == 0: return False ls = self.pixel_info["players_fold_coor"][str(loc)][0] ts = self.pixel_info["players_fold_coor"][str(loc)][1] width = self.pixel_info["players_fold_coor"][str(loc)][2] hight = self.pixel_info["players_fold_coor"][str(loc)][3] box = (ls, ts, ls + width, ts + hight) image_for_flod = image.crop(box) image_for_flod = self.image_to_2(image_for_flod, 180) if debug: image_for_flod.show() content = pytesseract.image_to_string(image_for_flod, config='-psm 100').strip() if debug: loggers.debug("{0} raw Fold info: {1}".format(loc, content)) if "F" in content and 'd' in content: return True
def do_self_action(self, pre_actions, player_left_chip, cards_type, button_loc, pot_chip): # 判断此时该谁action max_num = max(pre_actions) if max_num <= 0 : loggers.debug("not in preflop") return # straddle 不在玩的范围内时,前面有人open直接丢 if button_loc == 5 and (cards_type not in PreFlopTable.preFlopCardTable) and max_num > GrabTableInfo.big_blind: loggers.debug("fold when straddle meet open") self.wpk_mouse.Flod() return action_loc = -1 # 若自己不是straddle,且自身chip放入最大,则一定当前不是自己的操作顺序 #if max_num == pre_actions[0] and max_num != GrabTableInfo.big_blind: if max_num == pre_actions[0] and button_loc != 5: loggers.info("not my turn") return # 判断当前是谁操作 i = len(pre_actions) - 1 while i >= 0: if max_num == pre_actions[i]: break i -= 1 i += 1 while i <= 7 : if pre_actions[i] == -1: i += 1 continue else: break action_loc = i % 8 loggers.debug("{0} need action".format(action_loc)) if action_loc != 0: return # 判断是否前面全 Flod / Call if self.isPreCall(pre_actions): loggers.debug("pre all Flod or Call") # 前面没有Open, 在 straddle位置,直接call if cards_type not in PreFlopTable.preFlopCardTable: loggers.debug("straddle call") self.wpk_mouse.Call() return # 根据牌型做出操作 status = PreFlopTable.preFlopCardTable[cards_type] if status == PreFlopTable.PreOp.potBet: loggers.debug("1 pot bet") self.doOpen("1" , player_left_chip[0] > pot_chip) elif status < PreFlopTable.PreOp.justSpecailLoc: loggers.debug("2/3 pot bet") self.doOpen("2/3", player_left_chip[0] > (pot_chip * 2 / 3)) elif status == PreFlopTable.PreOp.justSpecailLoc: if button_loc == 0: loggers.debug("2/3 pot bet") self.doOpen("2/3", player_left_chip[0] > (pot_chip * 2 / 3)) elif button_loc == 5: loggers.debug("straddle call") self.wpk_mouse.Call() else: loggers.debug("not in special loc -> Flod") self.wpk_mouse.Flod() elif status == PreFlopTable.PreOp.samllPair: loggers.debug("call") self.wpk_mouse.Call() else: loggers.debug("Never arrive here") # 判断是否是小于5个BB的Open elif self.isOpen(pre_actions, 7): # straddle位置,牌不在玩的范围内, 就Flod掉 if cards_type not in PreFlopTable.preFlopCardTable: loggers.debug("straddle Flod") self.wpk_mouse.Flod() return status = PreFlopTable.preFlopCardTable[cards_type] if status == PreFlopTable.PreOp.potBet: loggers.debug("2/3 pot 3 bet") self.doOpen("2/3", player_left_chip[0] > (pot_chip * 2 / 3)) elif status <= PreFlopTable.PreOp.notCall3Bet: loggers.debug("2/3 pot 3 bet") self.doOpen("2/3", player_left_chip[0] > (pot_chip * 2 / 3)) elif status == PreFlopTable.PreOp.callOpen or status == PreFlopTable.PreOp.samllPair: loggers.debug("call open") self.wpk_mouse.Call() elif status == PreFlopTable.PreOp.justSpecailLoc: # 只在庄和大盲位置玩部分牌 if button_loc == 5 or button_loc == 0: loggers.debug("call open") self.wpk_mouse.Call() else: loggers.debug("not play justSpecailLoc when not in button or blind") self.wpk_mouse.Flod() elif status == PreFlopTable.PreOp.notCallOpen: loggers.debug("not call open Flod") self.wpk_mouse.Flod() else: loggers.debug("never arrive here for deal open") # 3Bet 或者 超大 Open elif self.isOpen(pre_actions, 15): if cards_type not in PreFlopTable.preFlopCardTable: loggers.debug("straddle Flod") self.wpk_mouse.Flod() return status = PreFlopTable.preFlopCardTable[cards_type] if status == PreFlopTable.PreOp.potBet: loggers.debug("2/3 pot 4 bet") self.doOpen("2/3", player_left_chip[0] > (pot_chip * 2 / 3)) elif status == PreFlopTable.PreOp.call3Bet: loggers.debug("call 3 bet ") self.wpk_mouse.Call() else: loggers.debug("do not call 3Bet") self.wpk_mouse.Flod() # 4bet 接近 allin elif self.isOpen(pre_actions, 110): if cards_type not in PreFlopTable.preFlopCardTable: loggers.debug("straddle Flod") self.wpk_mouse.Flod() return status = PreFlopTable.preFlopCardTable[cards_type] if status == PreFlopTable.PreOp.potBet: loggers.debug("2/3 pot 5 bet") self.doOpen("2/3", player_left_chip[0] > (pot_chip * 2 / 3)) else: loggers.debug("do not call 4 bet") self.wpk_mouse.Flod()
try_time = 1 while try_time > 0: if stop: break sleep_time = 2 time.sleep(sleep_time) if True: try_time -= 1 sleep_time = 0 cur_time = time.time() save_file = "images/cur_test_{0}".format(cur_time) # 屏幕截图 pic_loc = {"top": 380, "left": 1560, "width": 720, "height": 1330} loggers.debug("save image: {0}".format(save_file)) screenShot.shot(save_file, pic_loc) if debug: save_file = "images\cur_test_1614353669.7127104" image = Image.open(save_file + ".png") # 获取button位置 button_loc = grabTableInfo.get_button_loc(image) loggers.info("button_loc: {0}".format(button_loc)) # 获取底池 pot = grabTableInfo.get_pot(image) loggers.info("pot is: {0}".format(pot)) # 获取手牌
def PrintCoor(self): loggers.debug(self.mouse.position)