--- license: mit library_name: onnx tags: - onnx - computer-vision - clash-royale - object-detection - ocr - image-embeddings --- # ClashRoyaleBuildABot Models ONNX models and reference embeddings used by [ClashRoyaleBuildABot](https://github.com/Pbatch/ClashRoyaleBuildABot) to read the battle state from Clash Royale screenshots (`368 × 652` RGB). All ONNX models accept `uint8` RGB image batches in **NHWC** layout with casting, scaling, and any layout transposition folded into the graph. They run via `onnxruntime` on CPU or NVIDIA GPU (CUDA). ## Files | File | Purpose | Size | Input (`uint8` NHWC) | Output (`float32`) | | :--- | :--- | ---: | :--- | :--- | | `units_tiles.onnx` | Battlefield unit detection (half-tile grid + foot offsets) | 4.4 MB | `images`: `[1, 489, 368, 3]` | `tile_probs`: `[1, 220, 64, 36]`
`foot_offsets`: `[1, 2, 64, 36]` | | `card_embedder.onnx` | Hand & next-card embedding model | 1.2 MB | `images`: `[n, 72, 56, 3]` | `embeddings`: `[n, 128]` | | `card_embeddings.npz` | Reference 128-D card art vectors (183 entries) | 110 KB | — | `names`: `(183,)`
`vectors`: `(183, 128)` | | `tower_hp_digits.onnx` | Tower HP OCR (CTC) | 1.7 MB | `images`: `[n, 32, 80, 3]` | `probs`: `[n, 20, 11]` | | `timer_digits.onnx` | Battle timer OCR (CTC) | 218 KB | `images`: `[n, 40, 64, 3]` | `probs`: `[n, 16, 13]` | | `elixir_digits.onnx` | Elixir count OCR (CTC) | 210 KB | `images`: `[n, 32, 32, 3]` | `probs`: `[n, 8, 11]` | ## Model Details ### Unit Detector (`units_tiles.onnx`) Detects units on the `18 × 32` tile arena directly on a half-tile (`64 × 36`) grid without bounding boxes: - **Input**: The vertical strip of the screenshot covering the arena (`5%`–`80%` height, `489 × 368` RGB). - **Architecture**: MobileNetV4 backbone combining multi-scale features (1/8, 1/16, 1/32). - **Outputs**: - `tile_probs` `[1, 220, 64, 36]`: Per-cell probabilities for **110 unit classes** across both sides (`2 * i` = ally, `2 * i + 1` = enemy, ordered by `DETECTOR_UNITS` in [`unit_detector.py`](https://github.com/Pbatch/ClashRoyaleBuildABot/blob/main/clashroyalebuildabot/detectors/unit_detector.py)). - `foot_offsets` `[1, 2, 64, 36]`: Per-cell `(row, col)` offset (in cells, from each cell's top-left corner) pointing to where the detected unit's feet stand. Cells above the confidence threshold (`0.4`) project to their foot position and are merged per channel within a `0.6`-tile radius, allowing neighbouring swarm units to stay distinct while merging duplicate activations near cell boundaries. - **Training Data**: Trained on synthetic battle frames composed of unit sprites pasted onto empty arenas with health bars, spells, and clutter, using game textures and the [KataCR dataset](https://github.com/wty-yy/Clash-Royale-Detection-Dataset). ### Card Embedder (`card_embedder.onnx` & `card_embeddings.npz`) Identifies the four cards in hand and the next card: - **Input**: Batch of `72 × 56` RGB crops of the card slots. - **Output**: 128-D embeddings matched against `card_embeddings.npz` via cosine similarity (with Hungarian assignment across slots). - **Robustness**: Trained so each card slot maps close to its reference card art whether drawn in full colour, greyed out with an elixir wedge, raised, or rendered as the smaller next-card preview. - **Extensibility**: `card_embeddings.npz` contains 183 art vectors (regular cards, `_ev1` evolutions, and `blank` for empty slots). Newly released cards only require adding their reference vector rather than retraining the embedder. ### HUD Digit Readers (`tower_hp_digits.onnx`, `timer_digits.onnx`, `elixir_digits.onnx`) Small CNNs trained on numbers rendered in the game's font over real backgrounds plus real battle crops, decoded with greedy **CTC** (class `0` is blank): - **`tower_hp_digits.onnx`**: Reads all six towers' HP bars in one batch (`32 × 80` canvas, 20 time steps, 11 classes: blank + `0123456789`). - **`timer_digits.onnx`**: Reads the top-right battle timer (`40 × 64` canvas, 16 time steps, 13 classes: blank + `0123456789:+`, where `+` prefixes overtime). - **`elixir_digits.onnx`**: Reads the elixir counter (`32 × 32` canvas, 8 time steps, 11 classes: blank + `0123456789`). ## Usage ClashRoyaleBuildABot downloads and SHA-256-verifies these files automatically on startup or via: ```bash clashroyalebuildabot install ``` To run a model directly with `onnxruntime`: ```python import numpy as np import onnxruntime as ort session = ort.InferenceSession("units_tiles.onnx") view = np.zeros((1, 489, 368, 3), dtype=np.uint8) tile_probs, foot_offsets = session.run(["tile_probs", "foot_offsets"], {"images": view}) ``` ## Limitations - Designed specifically for Clash Royale screenshots resized to `368 × 652` with the standard portrait HUD layout. - `units_tiles.onnx` is trained on synthetic frames; on real battles it may occasionally miss heavily occluded units or confuse visually similar troops (mitigated in the bot by deck filtering, tower/river masking, and multi-frame tracking). - Digit readers skip low-confidence readings (`< 0.8`) when pop-ups or sprites occlude HUD text. ## License MIT — see [ClashRoyaleBuildABot](https://github.com/Pbatch/ClashRoyaleBuildABot/blob/main/LICENSE.md).