Ejemplo n.º 1
0
def find_dealer_seat(window):
# 	t = time.time()
	for seat, offset in enumerate(DEALER_OFFSETS):
		box = (offset[0], offset[1], offset[0]+DEALER_SIZE[0], offset[1]+DEALER_SIZE[1])
		img = window.crop(box)
# 		img.show()
# 		input()
# 		red = graphics.colour_ratio(img, "Red")
		green = graphics.colour_ratio(img, "Green")
# 		blue = graphics.colour_ratio(img, "Blue")
		white = graphics.colour_ratio(img, "White", w_thres=500)
# 		print("types: ", type(red), type(green), type(blue), type(white))
# 		print("Red: %.3f, Green: %.3f, Blue: %.3f, White: %.3f" % (red, green, blue, white))
# 		if white > 0.3 and (red / green > 0.8) and (red / blue) > 0.8:
		if white > 0.3 and green < 0.4:
# 		if white > 0.3:
# 			print("Found a dealer in %.3f seconds!" % (time.time() - t))
			return seat
# 		input()
Ejemplo n.º 2
0
def pot(window):
	pot_img = pot_image(window)
# 	pot_img.show()
	
	white = graphics.colour_ratio(pot_img, "White", w_thres=500)
	
	if white < 0.01:
		return

	pot = graphics.recognize_digits(pot_img)

	return pot
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
def find_dealer_seat(window):
    # 	t = time.time()
    for seat, offset in enumerate(DEALER_OFFSETS):
        box = (offset[0], offset[1], offset[0] + DEALER_SIZE[0],
               offset[1] + DEALER_SIZE[1])
        img = window.crop(box)
        # 		img.show()
        # 		input()
        # 		red = graphics.colour_ratio(img, "Red")
        green = graphics.colour_ratio(img, "Green")
        # 		blue = graphics.colour_ratio(img, "Blue")
        white = graphics.colour_ratio(img, "White", w_thres=500)
        # 		print("types: ", type(red), type(green), type(blue), type(white))
        # 		print("Red: %.3f, Green: %.3f, Blue: %.3f, White: %.3f" % (red, green, blue, white))
        # 		if white > 0.3 and (red / green > 0.8) and (red / blue) > 0.8:
        if white > 0.3 and green < 0.4:
            # 		if white > 0.3:
            # 			print("Found a dealer in %.3f seconds!" % (time.time() - t))
            return seat


# 		input()
Ejemplo n.º 5
0
    def get_bet(self, window, threshold=None, image=False):
        bet = window.crop(self.bet_box)
        # 		if image: return bet

        if not threshold: threshold = self.ocr_threshold

        bet_np = graphics.pil_to_np(bet)
        white = graphics.colour_ratio(bet_np, "White")
        if white < 0.01:
            return None
        else:
            ## THIS PART IS BUGGED AS F**K, but seems to work for now
            ## No idea yet how to detect green poker chips to crop the image.
            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")  # green?!?!
                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")
                max_green_ratio = graphics.max_colour_ratio(col,
                                                            "Red")  # green?!?!
                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
                if max_green_ratio > 0.334:
                    # 					print("Max green ratio in column %d is: %.3f" % (i, max_green_ratio))
                    bet = bet.crop((i - 1, 0, bet.width, bet.height))
                    break


# 			bet.show()
            if image: return bet
            bet = graphics.recognize_digits(bet, threshold)
            return bet
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
 def is_in_game(self, window):
     img = window.crop(self.card_box)
     return graphics.colour_ratio(img, "Red") > 0.42
Ejemplo n.º 9
0
def fast_fold(window):
    ff = window.crop(FOLD_BOX)
    return graphics.colour_ratio(ff, "Red") > 0.5
Ejemplo n.º 10
0
def is_my_turn(window):
    box = window.crop(TURN_BOX)
    # 	for col in ["Red", "Green", "Blue", "Black", "White", "Colour"]:
    # 		ratio = graphics.colour_ratio(box, col)
    # 		print("%s ratio: %.3f" % (col, ratio))
    return graphics.colour_ratio(box, "Red") > 0.5
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
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()