def split_characters(image, threshold=255): if type(image) == np.ndarray: arr = image image = np_to_pil(image) else: arr = pil_to_np(image, mode="L") start = 0 boxes = [] space = True unconnected = 0 trns = arr.transpose() # whites = trns[np.min(trns) >= threshold] whites = np.min(trns) >= threshold print(whites) # whites = trns # whites[whites >= threshold] = 1 # whites[whites <= threshold] = 0 # print(np.sum(whites)) # print(arr[arr > 200]) # # for i, col in enumerate(arr.transpose()): # min_val = np.min(col) # for i in range(arr.shape[1]): # min_val = min(arr[:,i]) # if min_val >= threshold and not space: # pass # box = [start, 0, i, arr.shape[0]] # boxes.append(box) # space = True # elif min_val < threshold and space: # start = i # space = False # elif i > 0 and (i - start) > 1 and start != 0 and min_val < threshold: # x, y = arr[:,i], arr[:,i-1] # for j in range(1, arr.shape[0]-1): # 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]
def update_training_set(image, digits=None, binary_threshold=110, net=None, new=False): if not net: net = NETWORK if new: training_set = SupervisedDataSet(INPUTS, OUTPUTS) elif not TRAINING_SET: if os.path.isfile(TRAINING_SET_PATH): with open(TRAINING_SET_PATH, 'rb') as f: training_set = pickle.load(f) else: training_set = SupervisedDataSet(INPUTS, OUTPUTS) else: training_set = TRAINING_SET if not digits: image.show() digits = input("What are the characters in this picture? ") logger.debug("Image is supposed to have the following string: %s" % digits) if len(digits) == 1: chars = [image] else: chars = graphics.image_characters(image, binary_threshold) if len(digits) != len(chars): logger.error( ("The number of characters is not the same (" "recognized: %d, supposed to be: %d ('%s'))! Dou suru kana~ " "Skipping this image.") % (len(chars), len(digits), digits)) input() return for i, char in enumerate(chars): d = digits[i] if d not in "0123456789": logger.debug("Not adding char %s to the training set" % d) continue inputs = graphics.pil_to_np(char.resize((5, 5), 1)).flatten() outputs = [0] * 10 outputs[int(d)] = 1 training_set.addSample(inputs, outputs) logger.debug("Added a sample for digits %s. Training set size: %d" % (d, len(training_set))) with open(TRAINING_SET_PATH, 'wb') as f: pickle.dump(training_set, f)
def recognize_digits(image, binary_threshold=110, *args, **kwargs): image.show() chars = graphics.image_characters(image, binary_threshold) digits = "" for char in chars: char = graphics.crop_to_bounding_box(char) # char.show() char = char.resize((5,5), 1) inputs = graphics.pil_to_np(char).flatten() # print(inputs) r = NETWORK.activate(inputs) # print(r) digits += str(r.argmax()) # input("digits so far: %s" % digits) # print("Digits recognized: %s" % digits) return digits
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
def update_training_set(image, digit, net=None, new=False): if not net: net = NETWORK if new or not hasattr(net, 'training_set'): net.training_set = SupervisedDataSet(net.training_set.indim, net.training_set.outdim) image = graphics.np_to_pil(image) image = graphics.crop_to_bounding_box(image) # image.show() image = image.resize((5,5), 1) # image.show() outputs = [0]*10 outputs[int(digit)] = 1 inputs = graphics.pil_to_np(image).flatten() # print("Digit: %s" % digit) # print("inputs: ", inputs) # print("outputs: ", outputs) net.training_set.addSample(inputs, outputs) print("Added a sample for digits %s. Training set size: %d" % (digit, len(net.training_set)))
def recognize_digits(image, binary_threshold=110, *args, **kwargs): image.show() chars = graphics.image_characters(image, binary_threshold) digits = "" for char in chars: char = graphics.crop_to_bounding_box(char) # char.show() char = char.resize((5, 5), 1) inputs = graphics.pil_to_np(char).flatten() # print(inputs) r = NETWORK.activate(inputs) # print(r) digits += str(r.argmax()) # input("digits so far: %s" % digits) # print("Digits recognized: %s" % digits) return digits
def update_training_set(image, digits=None, binary_threshold=110, net=None, new=False): if not net: net = NETWORK if new: training_set = SupervisedDataSet(INPUTS, OUTPUTS) elif not TRAINING_SET: if os.path.isfile(TRAINING_SET_PATH): with open(TRAINING_SET_PATH, 'rb') as f: training_set = pickle.load(f) else: training_set = SupervisedDataSet(INPUTS, OUTPUTS) else: training_set = TRAINING_SET if not digits: image.show() digits = input("What are the characters in this picture? ") logger.debug("Image is supposed to have the following string: %s" % digits) if len(digits) == 1: chars = [image] else: chars = graphics.image_characters(image, binary_threshold) if len(digits) != len(chars): logger.error(("The number of characters is not the same (" "recognized: %d, supposed to be: %d ('%s'))! Dou suru kana~ " "Skipping this image.") % (len(chars), len(digits), digits)) input() return for i, char in enumerate(chars): d = digits[i] if d not in "0123456789": logger.debug("Not adding char %s to the training set" % d) continue inputs = graphics.pil_to_np(char.resize((5,5), 1)).flatten() outputs = [0] * 10 outputs[int(d)] = 1 training_set.addSample(inputs, outputs) logger.debug("Added a sample for digits %s. Training set size: %d" % (d, len(training_set))) with open(TRAINING_SET_PATH, 'wb') as f: pickle.dump(training_set, f)
def update_training_set(image, digit, net=None, new=False): if not net: net = NETWORK if new or not hasattr(net, 'training_set'): net.training_set = SupervisedDataSet(net.training_set.indim, net.training_set.outdim) image = graphics.np_to_pil(image) image = graphics.crop_to_bounding_box(image) # image.show() image = image.resize((5, 5), 1) # image.show() outputs = [0] * 10 outputs[int(digit)] = 1 inputs = graphics.pil_to_np(image).flatten() # print("Digit: %s" % digit) # print("inputs: ", inputs) # print("outputs: ", outputs) net.training_set.addSample(inputs, outputs) print("Added a sample for digits %s. Training set size: %d" % (digit, len(net.training_set)))
def recognize_digits(image, binary_threshold=110, show=False, *args, **kwargs): if show: image.show() chars = graphics.image_characters(image, binary_threshold) digits = "" for char in chars: char = graphics.crop_to_bounding_box(char) if not is_digit(char): digits += recognize_char(char) continue char = char.resize((5, 5), 1) inputs = graphics.pil_to_np(char).flatten() output = NETWORK.activate(inputs) top = output.argmax() digits += str(top) second = output.argpartition(-2)[-2:-1][0] logger.debug("Top two values: %.3f (%s), %.3f (%s). Ratio: %.3f" % (output[top], str(top), output[second], str(second), output[second] / output[top])) return digits
def recognize_digits(image, binary_threshold=110, show=False, *args, **kwargs): if show: image.show() chars = graphics.image_characters(image, binary_threshold) digits = "" for char in chars: char = graphics.crop_to_bounding_box(char) if not is_digit(char): digits += recognize_char(char) continue char = char.resize((5,5), 1) inputs = graphics.pil_to_np(char).flatten() output = NETWORK.activate(inputs) top = output.argmax() digits += str(top) second = output.argpartition(-2)[-2:-1][0] logger.debug("Top two values: %.3f (%s), %.3f (%s). Ratio: %.3f" % ( output[top], str(top), output[second], str(second), output[second]/output[top])) return digits
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
# for pixel in row: # if np.sum(pixel[:3]) > w_thres: # pixel[:3] = np.array([0,0,0]) if w_thres: slice = slice[np.sum(slice, axis=2) <= w_thres] colour = np.sum(slice[:,col]) # total = np.sum(np.sum(slice[:,:], axis=2) <= w_thres) # colour = np.sum(np.sum(slice[:,:], axis=2) <= w_thres) else: colour = np.sum(slice[:,:,col]) total = np.sum(slice) # print(slice[:,:,:3] > w_thres) # for i in range(height): # for j in range(width): # t = sum(array[i+offset[0]][j+offset[1]][:3]) # if w_thres and t > w_thres: continue # ignore "white" pixels # total += t # colour += array[i+offset[0]][j+offset[1]][col] print("Total: %d, colour: %d" % (total, colour)) return colour / max(total, 0.00001) im = graphics.open_image("images/cards/2d.png") im.show() im_np = graphics.pil_to_np(im) print(colour_ratio_rgb(im_np, "Red", offset=(5,7), size=(4,4), w_thres=None)) print(colour_ratio_rgb_opt(im_np, "Red", offset=(5,7), size=(4,4), w_thres=None))
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
# 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() import cProfile # cProfile.run('f(im)', sort='tottime')
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()
# 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()