Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new ocr for swc player pots #196

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions poker/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
terminalmode = 0
setupmode = 0
montecarlo_timeout = 8
last_strategy = Official 1
control = w1064
table_scraper_name = Official Party Poker
login = guest
password = guest
last_strategy = SWC 1
control = Direct mouse control
table_scraper_name = SWC Poker - 2
login = timeyyy_da_man@hotmail.com
password = winning999
db = http://dickreuter.com:7777/

6 changes: 3 additions & 3 deletions poker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,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 @@ -243,7 +243,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,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/img.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.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/player0/5.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/player2/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/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
Binary file added poker/tests/swc/log/pics/img_cropped.png
Binary file added poker/tests/swc/log/pics/img_debug.png
Binary file added poker/tests/swc/log/pics/img_orig.png
139 changes: 139 additions & 0 deletions poker/tests/swc/test_number_recognition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
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},
5: {"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"]


# SELECTION_CONTOUR_THICKNESS of 4 will fix this test
def test_player0_5(self):
player = 0
num = 5
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
Original file line number Diff line number Diff line change
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
11 changes: 11 additions & 0 deletions poker/tools/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
from configparser import ConfigParser, ExtendedInterpolation
from logging import handlers

import cv2
import numpy as np
import pandas as pd
from PIL import Image
import requests

if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
Expand Down Expand Up @@ -338,3 +341,11 @@ def open_payment_link():
c = requests.post(URL + "get_internal").json()[0]
payment_link = c['payment_link']
webbrowser.open(payment_link, new=1)


def pil_to_cv2(img):
return cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)


def cv2_to_pil(img):
return Image.fromarray(img)