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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Generate perfectly-labeled synthetic poker table screenshots for perception evals.
|
| 2 |
+
|
| 3 |
+
Outputs:
|
| 4 |
+
dataset/images/test_XXX.png (1600x900)
|
| 5 |
+
dataset/labels/test_XXX.json (ground truth: cards, stacks, pot, bboxes)
|
| 6 |
+
|
| 7 |
+
Everything is deterministic given --seed. Labels use "A♠"-style card strings
|
| 8 |
+
and pixel-space bounding boxes. No redistribution concerns: all pixels are ours.
|
| 9 |
+
"""
|
| 10 |
+
import argparse
|
| 11 |
+
import json
|
| 12 |
+
import math
|
| 13 |
+
import os
|
| 14 |
+
import random
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
|
| 17 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 18 |
+
|
| 19 |
+
W, H = 1600, 900
|
| 20 |
+
RANKS = ["A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"]
|
| 21 |
+
SUITS = ["♠", "♥", "♦", "♣"]
|
| 22 |
+
RED_SUITS = {"♥", "♦"}
|
| 23 |
+
POSITIONS = {2: ["BTN", "SB", ], # completed later: BB
|
| 24 |
+
3: ["BTN", "SB", "BB"],
|
| 25 |
+
4: ["BTN", "SB", "BB", "UTG"],
|
| 26 |
+
5: ["BTN", "SB", "BB", "MP", "UTG"],
|
| 27 |
+
6: ["BTN", "SB", "BB", "MP", "MP2", "UTG"]}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _font(size, bold=True):
|
| 31 |
+
for name in ("DejaVuSans-Bold.ttf" if bold else "DejaVuSans.ttf",
|
| 32 |
+
"LiberationSans-Bold.ttf" if bold else "LiberationSans-Regular.ttf"):
|
| 33 |
+
for d in ("/usr/share/fonts/truetype/dejavu", "/usr/share/fonts/truetype/liberation"):
|
| 34 |
+
p = os.path.join(d, name)
|
| 35 |
+
if os.path.exists(p):
|
| 36 |
+
return ImageFont.truetype(p, size)
|
| 37 |
+
try:
|
| 38 |
+
return ImageFont.load_default(size=size)
|
| 39 |
+
except TypeError:
|
| 40 |
+
return ImageFont.load_default()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
FONT_CARD = _font(34)
|
| 44 |
+
FONT_SUIT = _font(58)
|
| 45 |
+
FONT_UI = _font(22)
|
| 46 |
+
FONT_UI_BIG = _font(30)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _rand_deck(rng):
|
| 50 |
+
deck = [(r, s) for r in RANKS for s in SUITS]
|
| 51 |
+
rng.shuffle(deck)
|
| 52 |
+
return deck
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _card_str(card):
|
| 56 |
+
r, s = card
|
| 57 |
+
return r + s
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def render_card(canvas, x, y, card, w=84, h=118, cid=None, labels=None):
|
| 61 |
+
"""Draw one face-up card at (x, y) top-left. Records pixel bbox."""
|
| 62 |
+
r, s = card
|
| 63 |
+
draw = ImageDraw.Draw(canvas)
|
| 64 |
+
draw.rounded_rectangle([x, y, x + w, y + h], radius=8, fill=(248, 248, 245),
|
| 65 |
+
outline=(40, 40, 40), width=2)
|
| 66 |
+
color = (200, 30, 30) if s in RED_SUITS else (20, 20, 20)
|
| 67 |
+
draw.text((x + 6, y + 4), r, font=FONT_CARD, fill=color)
|
| 68 |
+
draw.text((x + 6, y + 34), s, font=FONT_CARD, fill=color)
|
| 69 |
+
draw.text((x + w - 40, y + h - 66), s, font=FONT_SUIT, fill=color)
|
| 70 |
+
if labels is not None:
|
| 71 |
+
labels["card_bboxes"].append({"card": _card_str(card), "bbox": [x, y, x + w, y + h],
|
| 72 |
+
"where": cid})
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def generate_one(idx, rng, race=0):
|
| 76 |
+
n_players = rng.choice([2, 3, 4, 5, 6])
|
| 77 |
+
deck = _rand_deck(rng)
|
| 78 |
+
stage = rng.choice(["preflop", "flop", "turn", "river"])
|
| 79 |
+
n_board = {"preflop": 0, "flop": 3, "turn": 4, "river": 5}[stage]
|
| 80 |
+
pos_order = POSITIONS[n_players][:] if n_players > 2 else ["BTN", "SB/BB"]
|
| 81 |
+
pos_order = (pos_order + ["BB"])[:n_players]
|
| 82 |
+
pos_order = ["UTG"] + pos_order[:-1] if n_players >= 4 else pos_order
|
| 83 |
+
pot = rng.choice([40, 80, 120, 240, 500, 1250, 3400])
|
| 84 |
+
|
| 85 |
+
labels = {
|
| 86 |
+
"image": f"test_{idx:03d}.png", "width": W, "height": H,
|
| 87 |
+
"stage": stage, "pot": pot, "seats": [],
|
| 88 |
+
"community": [], "card_bboxes": [],
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
canvas = Image.new("RGB", (W, H), (18, 92, 46))
|
| 92 |
+
draw = ImageDraw.Draw(canvas)
|
| 93 |
+
# felt shading rings for a bit of texture
|
| 94 |
+
cx, cy = W // 2, H // 2 + 30
|
| 95 |
+
draw.ellipse([cx - 560, cy - 300, cx + 560, cy + 300], fill=(24, 104, 52))
|
| 96 |
+
draw.ellipse([cx - 460, cy - 240, cx + 460, cy + 240], fill=(30, 116, 60))
|
| 97 |
+
|
| 98 |
+
# board cards (center upper area); unrevealed slots left blank
|
| 99 |
+
board = [deck.pop() for _ in range(n_board)]
|
| 100 |
+
bw, bh = 92, 130
|
| 101 |
+
total_w = 5 * (bw + 12)
|
| 102 |
+
bx0 = cx - total_w // 2
|
| 103 |
+
for i, card in enumerate(board):
|
| 104 |
+
bx = bx0 + i * (bw + 12)
|
| 105 |
+
render_card(canvas, bx, 250, card, bw, bh, cid=f"board_{i}", labels=labels)
|
| 106 |
+
labels["community"].append({"card": _card_str(card), "bbox": [bx, 250, bx + bw, 250 + bh]})
|
| 107 |
+
|
| 108 |
+
# hero cards, center bottom, slightly larger
|
| 109 |
+
hero = [deck.pop(), deck.pop()]
|
| 110 |
+
for i, card in enumerate(hero):
|
| 111 |
+
hx = cx - bw - 10 + i * (bw + 20)
|
| 112 |
+
render_card(canvas, hx, 640, card, bw + 10, bh + 14, cid="hero",
|
| 113 |
+
labels=labels)
|
| 114 |
+
labels["hero"] = [_card_str(hero[0]), _card_str(hero[1])]
|
| 115 |
+
|
| 116 |
+
# pot text
|
| 117 |
+
pot_y = 430
|
| 118 |
+
draw = ImageDraw.Draw(canvas)
|
| 119 |
+
draw.text((cx - 70, pot_y), f"POT: {pot}", font=FONT_UI_BIG, fill=(245, 235, 200))
|
| 120 |
+
labels["pot_bbox"] = [cx - 90, pot_y - 6, cx + 120, pot_y + 40]
|
| 121 |
+
|
| 122 |
+
# seats around the ellipse, alternating left/right/top/bottom
|
| 123 |
+
seat_angles = [(90 + 360 * i / n_players) for i in range(n_players)]
|
| 124 |
+
names = ["Alice", "Bob", "Clara", "Dan", "Eve", "Flo"]
|
| 125 |
+
for i, ang in enumerate(seat_angles):
|
| 126 |
+
sx = cx + int(500 * math.cos(math.radians(ang))) - 90
|
| 127 |
+
sy = cy + int(265 * math.sin(math.radians(ang))) - 40
|
| 128 |
+
sy = min(max(sy, 40), H - 130)
|
| 129 |
+
sx = min(max(sx, 20), W - 220)
|
| 130 |
+
stack = rng.choice([500, 900, 1800, 4200, 12000])
|
| 131 |
+
seat_label = pos_order[i % len(pos_order)]
|
| 132 |
+
seat_cards = [deck.pop(), deck.pop()]
|
| 133 |
+
slot = {"seat": i, "name": names[i], "stack": stack, "position": seat_label,
|
| 134 |
+
"hole": [_card_str(seat_cards[0]), _card_str(seat_cards[1])],
|
| 135 |
+
"card_bboxes": []}
|
| 136 |
+
# two cards per seat
|
| 137 |
+
for j, card in enumerate(seat_cards):
|
| 138 |
+
jx = sx + j * 92
|
| 139 |
+
if sy > cy: # bottom seats: cards above the name plate
|
| 140 |
+
card_y = sy - 10
|
| 141 |
+
else:
|
| 142 |
+
card_y = sy + 34
|
| 143 |
+
render_card(canvas, jx, card_y, seat_cards[j] if False else card,
|
| 144 |
+
cid=f"seat{i}", labels={"card_bboxes": labels["card_bboxes"]})
|
| 145 |
+
labels["card_bboxes"][-1]["where"] = f"seat{i}"
|
| 146 |
+
slot["card_bboxes"].append(labels["card_bboxes"][-1])
|
| 147 |
+
# name plate with stack
|
| 148 |
+
draw = ImageDraw.Draw(canvas)
|
| 149 |
+
plate_y = sy if sy > cy else sy
|
| 150 |
+
draw.rounded_rectangle([sx - 6, plate_y, sx + 200, plate_y + 30], radius=6,
|
| 151 |
+
fill=(40, 40, 60), outline=(10, 10, 10), width=1)
|
| 152 |
+
draw.text((sx + 2, plate_y + 3), f"{names[i]} [{seat_label}]", font=FONT_UI,
|
| 153 |
+
fill=(235, 235, 235))
|
| 154 |
+
draw.text((sx + 118, plate_y + 3), f"STACK {stack}", font=FONT_UI,
|
| 155 |
+
fill=(255, 215, 120))
|
| 156 |
+
slot["bbox"] = [sx - 6, plate_y, sx + 200, plate_y + 30]
|
| 157 |
+
# optional bet circle
|
| 158 |
+
if rng.random() < 0.5:
|
| 159 |
+
bet = rng.choice([20, 40, 60, 120, 300, 720])
|
| 160 |
+
draw.ellipse([sx - 70, plate_y + 34, sx + 40, plate_y + 74],
|
| 161 |
+
fill=(220, 180, 40), outline=(120, 90, 10), width=2)
|
| 162 |
+
draw.text((sx - 62, plate_y + 44), f"{bet}", font=FONT_UI, fill=(40, 30, 5))
|
| 163 |
+
slot["bet"] = bet
|
| 164 |
+
labels["seats"].append(slot)
|
| 165 |
+
|
| 166 |
+
# dealer button
|
| 167 |
+
bi = rng.randrange(n_players)
|
| 168 |
+
ang = seat_angles[bi]
|
| 169 |
+
dx = cx + int(430 * math.cos(math.radians(ang)))
|
| 170 |
+
dy = cy + int(225 * math.sin(math.radians(ang)))
|
| 171 |
+
draw.ellipse([dx - 16, dy - 16, dx + 16, dy + 16], fill=(235, 235, 235),
|
| 172 |
+
outline=(60, 60, 60), width=2)
|
| 173 |
+
draw.text((dx - 7, dy - 12), "D", font=FONT_UI, fill=(20, 20, 20))
|
| 174 |
+
labels["dealer_seat"] = bi
|
| 175 |
+
|
| 176 |
+
# mild noise so OCR isn't trivially easy
|
| 177 |
+
for _ in range(900):
|
| 178 |
+
npx = (rng.randrange(0, W), rng.randrange(0, H))
|
| 179 |
+
g = rng.randrange(140, 220)
|
| 180 |
+
draw.point(npx, fill=(g, g, g))
|
| 181 |
+
return canvas, labels
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def main():
|
| 185 |
+
ap = argparse.ArgumentParser()
|
| 186 |
+
ap.add_argument("--out", default="dataset")
|
| 187 |
+
ap.add_argument("--n", type=int, default=60)
|
| 188 |
+
ap.add_argument("--seed", type=int, default=17)
|
| 189 |
+
args = ap.parse_args()
|
| 190 |
+
rng = random.Random(args.seed)
|
| 191 |
+
img_dir = Path(args.out) / "images"
|
| 192 |
+
lbl_dir = Path(args.out) / "labels"
|
| 193 |
+
img_dir.mkdir(parents=True, exist_ok=True)
|
| 194 |
+
lbl_dir.mkdir(parents=True, exist_ok=True)
|
| 195 |
+
deck_check = []
|
| 196 |
+
for i in range(args.n):
|
| 197 |
+
img, labels = generate_one(i, rng)
|
| 198 |
+
img.save(img_dir / labels["image"], quality=92)
|
| 199 |
+
(lbl_dir / (labels["image"].replace(".png", ".json"))).write_text(
|
| 200 |
+
json.dumps(labels, indent=1))
|
| 201 |
+
ncards = len(labels["card_bboxes"])
|
| 202 |
+
deck_check.append(ncards)
|
| 203 |
+
if i < 3:
|
| 204 |
+
print(f"test_{i:03d}: {ncards} cards, {len(labels['seats'])} seats, "
|
| 205 |
+
f"stage={labels['stage']}, pot={labels['pot']}")
|
| 206 |
+
print(f"done: {args.n} images; cards/image avg={sum(deck_check)/len(deck_check):.1f}")
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
if __name__ == "__main__":
|
| 210 |
+
main()
|