Skip to content

Commit

Permalink
Implement new ocr for swc player pots
Browse files Browse the repository at this point in the history
also make changes so current_round_pot is correct
  • Loading branch information
timeyyy committed Aug 4, 2023
1 parent 100c9c5 commit dcc8c84
Show file tree
Hide file tree
Showing 31 changed files with 350 additions and 24 deletions.
6 changes: 3 additions & 3 deletions poker/main.py
Expand Up @@ -185,11 +185,11 @@ def run(self):
table.get_round_number(history) and \
table.check_for_checkbutton() and \
table.init_get_other_players_info() and \
table.get_other_player_status(strategy, history) and \
table.get_other_player_status(strategy, history, table_dict['table_name']) and \
table.get_other_player_names(strategy) and \
table.get_other_player_funds(strategy) and \
table.get_total_pot_value(history) and \
table.get_round_pot_value(history) and \
table.get_round_pot_value(history, table_dict['table_name']) and \
table.check_for_call() and \
table.check_for_betbutton() and \
table.check_for_allincall() and \
Expand Down Expand Up @@ -242,7 +242,7 @@ def run(self):
log.debug("Saving screenshot: " + filename)
pil_image = table.crop_image(table.entireScreenPIL, table.tlc[0], table.tlc[1], table.tlc[0] + 1500,
table.tlc[1] + 1100)
pil_image.save("log/screenshots/" + filename)
# pil_image.save("log/screenshots/" + filename)

self.gui_signals.signal_status.emit("Logging data")

Expand Down
9 changes: 6 additions & 3 deletions poker/scraper/table_scraper.py
Expand Up @@ -190,18 +190,20 @@ def other_players_names(self):
def get_pots(self):
"""Get current and total pot"""
self.current_round_pot = ocr(self.screenshot, 'current_round_pot', self.table_dict, fast=True)
log.info(f"Current round pot {self.current_round_pot}")
log.info(f"Current round pot {self.current_round_pot} (before processing)")
self.total_pot = ocr(self.screenshot, 'total_pot_area', self.table_dict)
log.info(f"Total pot {self.total_pot}")

def get_player_pots(self, skip=[]): # pylint: disable=dangerous-default-value
def get_player_pots(self, skip=[], table_name=None): # pylint: disable=dangerous-default-value
"""Get pots of the players"""
self.player_pots = []
for i in range(self.total_players):
if i in skip:
funds = 0
else:
funds = ocr(self.screenshot, 'player_pot_area', self.table_dict, str(i))
funds = ocr(self.screenshot, 'player_pot_area', self.table_dict, str(i), False, table_name)
if funds == -1:
funds = 0
self.player_pots.append(funds)
log.info(f"Player pots: {self.player_pots}")

Expand Down Expand Up @@ -232,6 +234,7 @@ def has_check_button(self):
"""Check if check button is present"""
self.check_button = is_template_in_search_area(self.table_dict, self.screenshot,
'check_button', 'buttons_search_area')
self.screenshot.save("pics/check_button.png")
log.info(f"Check button found: {self.check_button}")
return self.check_button

Expand Down
26 changes: 16 additions & 10 deletions poker/scraper/table_screen_based.py
Expand Up @@ -279,15 +279,15 @@ def get_other_player_funds(self, p):

return True

def get_other_player_pots(self):
def get_other_player_pots(self, table_name):
self.gui_signals.signal_status.emit(f"Get table pots")
self.gui_signals.signal_progressbar_increase.emit(2)
self.get_pots()

exclude = set(range(self.total_players)) - set(self.players_in_game)
self.gui_signals.signal_status.emit(f"Get player pots of players in game {self.players_in_game}")
self.gui_signals.signal_progressbar_increase.emit(5)
self.get_player_pots(skip=list(exclude.union({0})))
self.get_player_pots(skip=list(exclude.union({0})), table_name=table_name)

for n in range(1, self.total_players):
if self.player_pots[n] != "":
Expand Down Expand Up @@ -315,7 +315,7 @@ def get_bot_pot(self, p):

return True

def get_other_player_status(self, p, h):
def get_other_player_status(self, p, h, table_name):
self.gui_signals.signal_status.emit("Get other playsrs' status")
self.gui_signals.signal_progressbar_increase.emit(2)
self.get_players_in_game()
Expand Down Expand Up @@ -350,7 +350,7 @@ def get_other_player_status(self, p, h):
else:
reference_pot = self.get_bot_pot(p)

self.get_other_player_pots()
self.get_other_player_pots(table_name)
# get first raiser in (tested for preflop)
self.first_raiser, \
self.second_raiser, \
Expand Down Expand Up @@ -420,7 +420,7 @@ def get_total_pot_value(self, h):
log.info("Final Total Pot Value: " + str(self.totalPotValue))
return True

def get_round_pot_value(self, h):
def get_round_pot_value(self, h, table_name):

self.round_pot_value = self.current_round_pot

Expand All @@ -430,13 +430,19 @@ def get_round_pot_value(self, h):
except:
self.round_pot_value = 0

if self.round_pot_value == "":
if self.round_pot_value == "" or self.round_pot_value == -1:
self.round_pot_value = 0
self.gui_signals.signal_status.emit("Unable to get round pot value")
log.warning("unable to get round pot value")
# self.round_pot_value = h.previous_round_pot_value
self.screenshot.save("pics/ErrRoundPotValue.png")

if not table_name.lower().startswith("swc poker"):
self.gui_signals.signal_status.emit("Unable to get round pot value")
log.warning("unable to get round pot value")
# self.round_pot_value = h.previous_round_pot_value
self.screenshot.save("pics/ErrRoundPotValue.png")

if table_name.lower().startswith("swc poker"):
self.round_pot_value = self.total_pot - self.round_pot_value

log.info(f"Current round pot {self.round_pot_value} (after processing)")
self.gui_signals.signal_progressbar_increase.emit(5)
return True

Expand Down
3 changes: 2 additions & 1 deletion poker/scraper/table_setup_actions_and_signals.py
Expand Up @@ -454,7 +454,8 @@ def _update_preview_label(self, preview):

@pyqtSlot(object)
def _recognize_number(self):
self.recognized_number = get_ocr_float(self.preview)
table_name = self.ui.table_name.currentText()
self.recognized_number = get_ocr_float(self.preview, table_name)
log.info(f"Recognized number is: {self.recognized_number}")
self.signal_update_label.emit('tesseract_label', str(self.recognized_number))

Expand Down
Binary file added poker/tests/swc/images/player0/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player0/1_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player0/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player0/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player0/3_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player0/4 _orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player0/4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player1/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player1/1_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player2/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player2/1_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player2/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player2/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player2/3_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player4/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player4/1_no_chips.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player4/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player4/2_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player4/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player4/3_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player5/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player5/1_orig.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poker/tests/swc/images/player5/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 130 additions & 0 deletions poker/tests/swc/test_number_recognition.py
@@ -0,0 +1,130 @@
import os

import cv2
from PIL import Image

from poker.tools.screen_operations import swc_ocr as ocr

is_debug = False # used for saving images for debug purposes

images = {
0: {1: {"amount": 40.0},
2: {"amount": 9.0},
3: {"amount": 6000.0},
4: {"amount": 1.0}},
1: {1: {"amount": 1480.0}},
2: {1: {"amount": 250.0},
2: {"amount": 5000.0},
3: {"amount": 8945.0},
4: {"amount": 0.5}},
4: {1: {"amount": 2.5},
"1_no_chips": {"amount": 2.5},
2: {"amount": 150.0},
3: {"amount": 2069.16}},
5: {1: {"amount": 250.0},
2: {"amount": 0.5}}
}

dirname = os.path.dirname(__file__)
img_dir = os.path.join(dirname, "images")


class TestNumberRecognition():
def get_img(self, player, num):
img_path = os.path.join(img_dir,
"player" + str(player),
str(num) + ".png")
return Image.open(img_path)

def test_player0(self):
player = 0
num = 1
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player0_2(self):
player = 0
num = 2
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player0_3(self):
player = 0
num = 3
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player0_4(self):
player = 0
num = 4
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player1(self):
player = 1
num = 1
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player2(self):
player = 2
num = 1
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player2_2(self):
player = 2
num = 2
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player2_3(self):
player = 2
num = 3
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]


def test_player2_4(self):
player = 2
num = 4
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player4(self):
player = 4
num = 1
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player4_no_chips(self):
player = 4
num = "1_no_chips"
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player4_2(self):
player = 4
self.i = 2
num = self.i
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player4_3(self):
player = 4
num = 3
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]

def test_player5(self):
player = 5
num = 1
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]


def test_player5_2(self):
player = 5
num = 2
img = self.get_img(player, num)
assert ocr(img) == images[player][num]["amount"]
4 changes: 2 additions & 2 deletions poker/tests/test_table_and_ocr.py
Expand Up @@ -111,14 +111,14 @@ def test_ocr_pp4():
def test_orc_problems1():
"""Tricky OCR situations"""
img = Image.open(os.path.join(get_dir('codebase'), r"tests/ocr/num1.png"))
result = get_ocr_float(img)
result = get_ocr_float(img, None)
assert result == 3.94


def test_orc_problems2():
"""Tricky OCR situations"""
img = Image.open(os.path.join(get_dir('codebase'), r"tests/ocr/num2.png"))
result = get_ocr_float(img)
result = get_ocr_float(img, None)
assert result == 3.94


Expand Down
25 changes: 20 additions & 5 deletions poker/tools/screen_operations.py
Expand Up @@ -13,6 +13,7 @@

from poker.tools.helper import memory_cache, get_dir
from poker.tools.mongo_manager import MongoManager
from poker.tools.swc import swc_ocr
from poker.tools.vbox_manager import VirtualBoxController

log = logging.getLogger(__name__)
Expand All @@ -28,6 +29,9 @@
oem=OEM.LSTM_ONLY)


IMG_COUNTER = 0


def find_template_on_screen(template, screenshot, threshold, extended=False):
"""Find template on screen"""
res = cv2.matchTemplate(screenshot, template, cv2.TM_SQDIFF_NORMED)
Expand Down Expand Up @@ -61,9 +65,12 @@ def get_table_template_image(table_name='default', label='topleft_corner'):
return template_cv2


def get_ocr_float(img_orig, fast=False):
def get_ocr_float(img_orig, table_name, fast=False):
"""Return float value from image. -1.0f when OCR failed"""
return get_ocr_number(img_orig, fast)
if table_name and table_name.lower().startswith("swc poker"):
return swc_ocr(img_orig)
else:
return get_ocr_number(img_orig, fast)


def prepareImage(img_orig, binarize=True, threshold=76):
Expand Down Expand Up @@ -104,7 +111,7 @@ def binarize_array_opencv(image, threshold):

def get_ocr_number2(img_orig, fast=False):
"""New OCR based on tesserocr rather than pytesseract, should be much faster"""
api.SetVariable("tessedit_char_whitelist", "0123456789.$£B")
api.SetVariable("tessedit_char_whitelist", "0123456789.,$£B")
api.SetImage(img_orig)
result = api.GetUTF8Text()
return result
Expand Down Expand Up @@ -265,7 +272,7 @@ def is_template_in_search_area(table_dict, screenshot, image_name, image_area, p
return is_in_range


def ocr(screenshot, image_area, table_dict, player=None, fast=False):
def ocr(screenshot, image_area, table_dict, player=None, fast=False, table_name=None):
"""
get ocr of area of screenshot
Expand All @@ -291,4 +298,12 @@ def ocr(screenshot, image_area, table_dict, player=None, fast=False):
search_area = table_dict[image_area]
cropped_screenshot = screenshot.crop(
(search_area['x1'], search_area['y1'], search_area['x2'], search_area['y2']))
return get_ocr_float(cropped_screenshot, fast)
result = get_ocr_float(cropped_screenshot, table_name, fast)


if result == -1 and image_area == 'player_pot_area':
global IMG_COUNTER
path = "log/pics/player_pot_area_player_" + str(player) + "_" + str(IMG_COUNTER) + ".png"
cropped_screenshot.save(path)
IMG_COUNTER += 1
return result

0 comments on commit dcc8c84

Please sign in to comment.