def build_recognition_db(binary_threshold, window=None, strings=None, show=False, neural=False, *args, **kwargs): if not window: window = game_window() elif type(window) == str: window = graphics.open_image(window) # window.show() if not strings: strings = input("Enter the strings in the game window in the following order: " "pot, your stack, starting from the player to your left, clockwise, " "player 1 stack, player 1 bet, player 2 stack, etc, then call value " "and bet/raise value, separated by spaces. If no bet, call or raise, " "just press spacebar again.\nAlternatively, you can input the " "filename where all that info is stored.\nOr just press enter and " "input each image text separately when prompted.\n").split(" ") elif type(strings) == str: strings = strings.split(" ") if len(strings) == 1: if strings[0] == "": show = True else: with open(strings[0], 'r') as f: strings = f.read().strip("\n").split(" ") def gen(): if strings: yield strings.pop(0) else: yield None def next_string(): return strings.pop(0) if strings else None # return gen().__next__() print(strings) pot_digits, pot_img = next_string(), money.pot_image(window) if graphics.colour_ratio(pot_img, "White", w_thres=500) > 0.01: graphics.update_recognition_db(pot_img, binary_threshold, show=show, ics=pot_digits, neural=neural) graphics.update_recognition_db(money.own_stack_image(window), binary_threshold, show=show, ics=next_string(), neural=neural) plrs = players.create_players() for player in plrs: graphics.update_recognition_db(player.get_stack(window, image=True), binary_threshold, show=show, ics=next_string(), neural=neural) bet_image = player.get_bet(window, image=True) bet = next_string() if bet_image: graphics.update_recognition_db(bet_image, binary_threshold, show=show, ics=bet, neural=neural) if actions.is_my_turn(window): _, call = actions.get_cc_images(window) call_digits = next_string() if call: graphics.update_recognition_db(call, binary_threshold, show=show, ics=call_digits, neural=neural) _, br, _ = actions.get_br_images(window) graphics.update_recognition_db(br, binary_threshold, show=show, ics=next_string(), neural=neural)
# if x[j] < threshold and any(np.array([y[j-1], y[j], y[j+1]]) < threshold): # break # else: # box = [start, 0, i, arr.shape[0]] # boxes.append(box) # start = i if not space: box = [start, 0, arr.shape[1], arr.shape[0]] boxes.append(box) return [image.crop(bbox) for bbox in boxes] w = graphics.open_image('images/game/75814.png') im = money.pot_image(w) im = im.convert(mode="L") im = graphics.invert(im) im = pil_to_np(im, mode="L") white = np.max(im) black = np.min(im) binary_threshold = black + (white - black) * (120 / 255) im = graphics.binarize(im, binary_threshold) # text.show() def f(im): # chars = None for i in range(50): chars = split_characters(im) # for char in chars: # char.show()
def build_recognition_db(binary_threshold, window=None, strings=None, show=False, neural=False, *args, **kwargs): if not window: window = game_window() elif type(window) == str: window = graphics.open_image(window) # window.show() if not strings: strings = input( "Enter the strings in the game window in the following order: " "pot, your stack, starting from the player to your left, clockwise, " "player 1 stack, player 1 bet, player 2 stack, etc, then call value " "and bet/raise value, separated by spaces. If no bet, call or raise, " "just press spacebar again.\nAlternatively, you can input the " "filename where all that info is stored.\nOr just press enter and " "input each image text separately when prompted.\n").split(" ") elif type(strings) == str: strings = strings.split(" ") if len(strings) == 1: if strings[0] == "": show = True else: with open(strings[0], 'r') as f: strings = f.read().strip("\n").split(" ") def gen(): if strings: yield strings.pop(0) else: yield None def next_string(): return strings.pop(0) if strings else None # return gen().__next__() print(strings) pot_digits, pot_img = next_string(), money.pot_image(window) if graphics.colour_ratio(pot_img, "White", w_thres=500) > 0.01: graphics.update_recognition_db(pot_img, binary_threshold, show=show, ics=pot_digits, neural=neural) graphics.update_recognition_db(money.own_stack_image(window), binary_threshold, show=show, ics=next_string(), neural=neural) plrs = players.create_players() for player in plrs: graphics.update_recognition_db(player.get_stack(window, image=True), binary_threshold, show=show, ics=next_string(), neural=neural) bet_image = player.get_bet(window, image=True) bet = next_string() if bet_image: graphics.update_recognition_db(bet_image, binary_threshold, show=show, ics=bet, neural=neural) if actions.is_my_turn(window): _, call = actions.get_cc_images(window) call_digits = next_string() if call: graphics.update_recognition_db(call, binary_threshold, show=show, ics=call_digits, neural=neural) _, br, _ = actions.get_br_images(window) graphics.update_recognition_db(br, binary_threshold, show=show, ics=next_string(), neural=neural)
def _create_image_with_info(window): threshold = 100 font_size = 17 pot = money.pot_image(window) if graphics.colour_ratio(pot, "White") >= 0.01: pot = recognition.recognize_digits(pot, threshold) else: pot = "NO POT!" draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) draw.text((330, 270), "POT SIZE: %s" % pot, (255, 0, 0), font=font) del draw own_stack = money.own_stack_image(window) own_stack = recognition.recognize_digits(own_stack, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) draw.text((330, 435), "MY STACK: %s" % own_stack, (255, 0, 0), font=font) del draw seat = dealer.find_dealer_seat(window) loc = dealer.DEALER_OFFSETS[seat] loc = (loc[0] - 60, loc[1] + 20) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) draw.text(loc, "DEALER HERE (player %d)!" % (seat + 1), (255, 0, 0), font=font) del draw for j in range(5): p = players.Player(j) if p.is_in_game(window): text = "PLAYER IS ACTIVE" else: text = "PLAYER INACTIVE" draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) loc = (p.card_box.left - 20, p.card_box.top - 20) draw.text(loc, text, (255, 0, 0), font=font) del draw stack = window.crop(p.stack_box) stack = recognition.recognize_digits(stack, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) loc = (p.stack_box.left - 20, p.stack_box.top + 20) draw.text(loc, "STACK: %s" % stack, (255, 0, 0), font=font) del draw bet = window.crop(p.bet_box) bet_np = graphics.pil_to_np(bet) white = graphics.colour_ratio(bet_np, "White") if white < 0.01: text = "NO BETS HERE!" else: for i in range(bet_np.shape[1] // 2, bet_np.shape[1]): col = bet_np[:, i:i + 1] max_red_ratio = graphics.max_colour_ratio(col, "Red") max_green_ratio = graphics.max_colour_ratio(col, "Red") if max_red_ratio > 0.5: # print("Max red ratio in each pixel in column %d is: %.3f" % (i, max_red_ratio)) bet = bet.crop((0, 0, i, bet.height)) break if max_green_ratio > 0.334: # print("Max green ratio in column %d is: %.3f" % (i, max_green_ratio)) bet = bet.crop((0, 0, i - 1, bet.height)) break for i in range(bet_np.shape[1] // 2, -1, -1): col = bet_np[:, i:i + 1] max_red_ratio = graphics.max_colour_ratio(col, "Red") if max_red_ratio > 0.5: # print("Max red ratio in each pixel in colum %d is : %.3f" % (i, max_red_ratio)) bet = bet.crop((i + 1, 0, bet.width, bet.height)) break text = "PLACED A BET: %s" % recognition.recognize_digits( bet, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) loc = (p.bet_box.left - 40, p.bet_box.top - 20 + (int(j == 2) * 40)) draw.text(loc, text, (255, 0, 0), font=font) cc, val_img = actions.get_cc_images(window) call = recognition.recognize_digits(val_img) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", int(font_size * 1.5)) loc = (540, 490) draw.text(loc, call, (0, 255, 0), font=font) br, val_img, _ = actions.get_br_images(window) bet = recognition.recognize_digits(val_img) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", int(font_size * 1.5)) loc = (660, 490) draw.text(loc, bet, (0, 255, 0), font=font) own_cards_image = cards.own_cards_image(window) own_cards = cards.own_cards(own_cards_image) flop = [cards.table_card(window, i) for i in range(3)] turn = cards.table_card(window, 3) river = cards.table_card(window, 4) # my_card = cards. # window.show() return window
def _create_image_with_info(window): threshold = 100 font_size = 17 pot = money.pot_image(window) if graphics.colour_ratio(pot, "White") >= 0.01: pot = recognition.recognize_digits(pot, threshold) else: pot = "NO POT!" draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) draw.text((330, 270),"POT SIZE: %s" % pot,(255,0,0), font=font) del draw own_stack = money.own_stack_image(window) own_stack = recognition.recognize_digits(own_stack, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) draw.text((330, 435),"MY STACK: %s" % own_stack,(255,0,0), font=font) del draw seat = dealer.find_dealer_seat(window) loc = dealer.DEALER_OFFSETS[seat] loc = (loc[0]-60, loc[1] + 20) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) draw.text(loc,"DEALER HERE (player %d)!" % (seat + 1),(255,0,0), font=font) del draw for j in range(5): p = players.Player(j) if p.is_in_game(window): text = "PLAYER IS ACTIVE" else: text = "PLAYER INACTIVE" draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) loc = (p.card_box.left-20, p.card_box.top-20) draw.text(loc, text, (255, 0, 0), font=font) del draw stack = window.crop(p.stack_box) stack = recognition.recognize_digits(stack, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) loc = (p.stack_box.left-20, p.stack_box.top+20) draw.text(loc, "STACK: %s" % stack, (255, 0, 0), font=font) del draw bet = window.crop(p.bet_box) bet_np = graphics.pil_to_np(bet) white = graphics.colour_ratio(bet_np, "White") if white < 0.01: text = "NO BETS HERE!" else: for i in range(bet_np.shape[1] // 2, bet_np.shape[1]): col = bet_np[:,i:i+1] max_red_ratio = graphics.max_colour_ratio(col, "Red") max_green_ratio = graphics.max_colour_ratio(col, "Red") if max_red_ratio > 0.5: # print("Max red ratio in each pixel in column %d is: %.3f" % (i, max_red_ratio)) bet = bet.crop((0, 0, i, bet.height)) break if max_green_ratio > 0.334: # print("Max green ratio in column %d is: %.3f" % (i, max_green_ratio)) bet = bet.crop((0, 0, i-1, bet.height)) break for i in range(bet_np.shape[1] // 2, -1, -1): col = bet_np[:,i:i+1] max_red_ratio = graphics.max_colour_ratio(col, "Red") if max_red_ratio > 0.5: # print("Max red ratio in each pixel in colum %d is : %.3f" % (i, max_red_ratio)) bet = bet.crop((i+1, 0, bet.width, bet.height)) break text = "PLACED A BET: %s" % recognition.recognize_digits(bet, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", font_size) loc = (p.bet_box.left-40, p.bet_box.top-20 + (int(j == 2) * 40)) draw.text(loc, text, (255, 0, 0), font=font) cc, val_img = actions.get_cc_images(window) call = recognition.recognize_digits(val_img) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", int(font_size*1.5)) loc = (540, 490) draw.text(loc, call, (0, 255, 0), font=font) br, val_img, _ = actions.get_br_images(window) bet = recognition.recognize_digits(val_img) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("data/font.ttf", int(font_size*1.5)) loc = (660, 490) draw.text(loc, bet, (0, 255, 0), font=font) own_cards_image = cards.own_cards_image(window) own_cards = cards.own_cards(own_cards_image) flop = [cards.table_card(window, i) for i in range(3)] turn = cards.table_card(window, 3) river = cards.table_card(window, 4) # my_card = cards. # window.show() return window
def create_image_with_info(): threshold = 115 font_size = 17 # window = game_window() window = game_window(path="images/game/s7.png") window.show() pot = money.pot_image(window) if graphics.colour_ratio(pot, "White") >= 0.01: pot = graphics.recognize_digits(pot, threshold) else: pot = "NO POT!" draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("font.ttf", font_size) draw.text((330, 270), "POT SIZE: %s" % pot, (255, 0, 0), font=font) del draw own_stack = money.own_stack_image(window) own_stack = graphics.recognize_digits(own_stack, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("font.ttf", font_size) draw.text((330, 435), "MY STACK: %s" % own_stack, (255, 0, 0), font=font) del draw seat = dealer.find_dealer_seat(window) loc = dealer.DEALER_OFFSETS[seat] loc = (loc[0] - 60, loc[1] + 20) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("font.ttf", font_size) draw.text(loc, "DEALER HERE (player %d)!" % (seat + 1), (255, 0, 0), font=font) del draw for j in range(5): p = players.Player(j) if p.is_in_game(window): text = "PLAYER IS ACTIVE" else: text = "PLAYER INACTIVE" draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("font.ttf", font_size) loc = (p.card_box.left - 20, p.card_box.top - 20) draw.text(loc, text, (255, 0, 0), font=font) del draw stack = window.crop(p.stack_box) stack = graphics.recognize_digits(stack, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("font.ttf", font_size) loc = (p.stack_box.left - 20, p.stack_box.top + 20) draw.text(loc, "STACK: %s" % stack, (255, 0, 0), font=font) del draw bet = window.crop(p.bet_box) bet_np = graphics.pil_to_np(bet) white = graphics.colour_ratio(bet_np, "White") if white < 0.01: text = "NO BETS HERE!" else: for i in range(bet_np.shape[1] // 2, bet_np.shape[1]): col = bet_np[:, i:i + 1] max_red_ratio = graphics.max_colour_ratio(col, "Red") max_green_ratio = graphics.max_colour_ratio(col, "Red") if max_red_ratio > 0.5: print("Max red ratio in each pixel in column %d is: %.3f" % (i, max_red_ratio)) bet = bet.crop((0, 0, i, bet.height)) break if max_green_ratio > 0.334: print("Max green ratio in column %d is: %.3f" % (i, max_green_ratio)) bet = bet.crop((0, 0, i - 1, bet.height)) break for i in range(bet_np.shape[1] // 2, -1, -1): col = bet_np[:, i:i + 1] max_red_ratio = graphics.max_colour_ratio(col, "Red") if max_red_ratio > 0.5: print("Max red ratio in each pixel in colum %d is : %.3f" % (i, max_red_ratio)) bet = bet.crop((i + 1, 0, bet.width, bet.height)) break text = "PLACED A BET: %s" % graphics.recognize_digits( bet, threshold) draw = PIL.ImageDraw.Draw(window) font = PIL.ImageFont.truetype("font.ttf", font_size) loc = (p.bet_box.left - 40, p.bet_box.top - 20 + (int(j == 2) * 40)) draw.text(loc, text, (255, 0, 0), font=font) # my_card = cards. window.show()
neural=True) build_recognition_db(110, "images/game/s4.png", strings="s4.txt", show=False, neural=True) build_recognition_db(110, "images/game/s3.png", strings="s3.txt", show=False, neural=True) build_recognition_db(110, "images/game/s2.png", strings="s2.txt", show=False, neural=True) recognition.train(100) with open(recognition.NET_PATH, 'wb') as f: pickle.dump(recognition.NETWORK, f) for img in ["s7.png", "8120.png", "75814.png"]: w = game_window(path="images/game/%s" % img) p = money.pot_image(w) print("Digits recognized: ", recognition.recognize_digits(p)) w = game_window(path="images/game/s2.png") s = money.own_stack_image(w) print("Digits recognized: ", recognition.recognize_digits(s))