Files
All-in/table.py
T
2026-12-03 03:15:02 +01:00

125 lines
4.3 KiB
Python

import cv2
import numpy as np
LOWER_GREEN = np.array([35, 40, 30])
UPPER_GREEN = np.array([90, 255, 255])
LOWER_WHITE = np.array([0, 0, 155])
UPPER_WHITE = np.array([180, 70, 255])
class ZoneExtractor:
"""
Klasa odpowiadajaca za wyciecie z obrazu poszczegolnych miejsc zainteresowania.
Zwraca Liste o kluczach: community, player_top, playter_right, player_bottom.
"""
def __init__(self):
pass
def get_green_mask(self, image):
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(image_hsv, LOWER_GREEN, UPPER_GREEN)
return cv2.morphologyEx(mask, cv2.MORPH_CLOSE, np.ones((7, 7), np.uint8))
def findTable(self, image):
"""
Funkcja znajduje plansze do gry.
Poprzez progowanie obrazu na podstawie koloru zielonego.
Zwraca sama plansze.
"""
clean_mask = self.get_green_mask(image)
ys, xs = np.where(clean_mask == 255)
y_min, y_max = np.percentile(ys, [4, 97]).astype(int)
x_min, x_max = np.percentile(xs, [4, 97]).astype(int)
table = image[y_min:y_max, x_min:x_max]
return table
def extract(self, table):
"""
Korzystajac z segmentacji obrazu otrzymujemy obiekty ktore pozniej na podstawie wspolrzednych przypisujemy do konkretnego gracza.
:param table: wycieta plansza gry
:return zones: zwraca slownik z obrazami kart dla danego gracza.
"""
h, w, _ = table.shape
table_area = h * w
result = {
"cards": {
"community": [],
"player_one": [],
"player_two": [],
"player_three": [],
},
"chips": {
"player_one": [],
"player_two": [],
"player_three": [],
},
}
hsv_table = cv2.cvtColor(table, cv2.COLOR_BGR2HSV)
white_mask = cv2.inRange(hsv_table, LOWER_WHITE, UPPER_WHITE)
white_mask = cv2.morphologyEx(white_mask, cv2.MORPH_CLOSE, np.ones((10, 10), np.uint8))
cv2.imshow("table", white_mask)
num_cards, _, card_stats, card_centroids = cv2.connectedComponentsWithStats(white_mask)
for i in range(1, num_cards):
x, y, bw, bh, area = card_stats[i]
cx, cy = card_centroids[i]
if area < table_area * 0.007 or bh > h * 0.70 or bw > w * 0.70:
continue
crop = table[max(0, y - 5):min(h, y + bh + 5), max(0, x - 5):min(w, x + bw + 5)]
if cx < w * 0.30 and cy > h * 0.22:
result["cards"]["community"].append(crop)
elif w * 0.28 < cx < w * 0.72 and cy < h * 0.38:
result["cards"]["player_one"].append(crop)
elif cx > w * 0.58 and cy < h * 0.70:
result["cards"]["player_two"].append(crop)
elif cy > h * 0.62 and w * 0.28 < cx < w * 0.72:
result["cards"]["player_three"].append(crop)
green_mask = self.get_green_mask(table)
chips_mask = cv2.bitwise_not(green_mask)
num_chips, _, chip_stats, chip_centroids = cv2.connectedComponentsWithStats(chips_mask)
for i in range(1, num_chips):
x, y, bw, bh, area = chip_stats[i]
cx, cy = chip_centroids[i]
is_chip_size = (table_area * 0.0015) < area < (table_area * 0.0065)
in_betting_zone = (w * 0.28 < cx < w * 0.72) and (h * 0.22 < cy < h * 0.78)
if not (is_chip_size and in_betting_zone):
continue
crop = table[max(0, y - 5):min(h, y + bh + 5), max(0, x - 5):min(w, x + bw + 5)]
if cy < h * 0.42 and cx < w * 0.62:
result["chips"]["player_one"].append(crop)
elif cx >= w * 0.52 and h * 0.30 <= cy <= h * 0.70:
result["chips"]["player_two"].append(crop)
elif cy > h * 0.58 and cx < w * 0.62:
result["chips"]["player_three"].append(crop)
return result
zoneE = ZoneExtractor()
if __name__ == '__main__':
frame = cv2.imread('images/test_table_03.jpg')
table = zoneE.findTable(frame)
zones = zoneE.extract(table)
for zone_name, crops in zones["cards"].items():
for idx, crop in enumerate(crops):
cv2.imshow(f"{zone_name}_{idx}", crop)
cv2.waitKey(0)
cv2.destroyAllWindows()