Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: pytorch
|
| 4 |
+
pipeline_tag: image-classification
|
| 5 |
+
tags:
|
| 6 |
+
- computer-vision
|
| 7 |
+
- image-classification
|
| 8 |
+
- mobilenet-v2
|
| 9 |
+
- cards
|
| 10 |
+
- solitaire
|
| 11 |
+
- computer-use
|
| 12 |
+
datasets:
|
| 13 |
+
- solitaire-cards-dataset
|
| 14 |
+
metrics:
|
| 15 |
+
- accuracy
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# Solitaire Card Recognizer
|
| 19 |
+
|
| 20 |
+
A 52-class playing-card classifier fine-tuned from **MobileNet v2** (ImageNet
|
| 21 |
+
weights from `torchvision`). It was trained for an autonomous computer-use agent
|
| 22 |
+
that plays Microsoft Solitaire Collection through the real screen and mouse:
|
| 23 |
+
the agent sees only pixels, so this model is its "eyes".
|
| 24 |
+
|
| 25 |
+
* **Accuracy:** 99.91 % (3251/3254) on the full dataset, **100 %** (650/650) on
|
| 26 |
+
the held-out split
|
| 27 |
+
* **Size:** 2,290,484 parameters (9.4 MB, `model.pth`)
|
| 28 |
+
* **Latency:** 6.7 ms pure inference, 8.1 ms including crop and preprocessing
|
| 29 |
+
(single crop, RTX 2060S); tens of milliseconds on CPU
|
| 30 |
+
* **Input:** a full, fully visible card image (the training crops are 131×176 px;
|
| 31 |
+
the model resizes to 224×224 and applies ImageNet normalization)
|
| 32 |
+
* **Output:** one of 52 classes in the order of `classes.json`
|
| 33 |
+
(`10_clubs … Q_spades`, matching `torchvision.datasets.ImageFolder` ordering)
|
| 34 |
+
|
| 35 |
+
## Usage
|
| 36 |
+
|
| 37 |
+
```python
|
| 38 |
+
import json
|
| 39 |
+
from huggingface_hub import hf_hub_download
|
| 40 |
+
import torch, torchvision
|
| 41 |
+
from PIL import Image
|
| 42 |
+
from torchvision import transforms
|
| 43 |
+
|
| 44 |
+
repo = "WildFuria/solitaire-card-recognizer" # ← your namespace after upload
|
| 45 |
+
weights = hf_hub_download(repo, "model.pth")
|
| 46 |
+
classes = json.load(open(hf_hub_download(repo, "classes.json"), encoding="utf-8"))["classes"]
|
| 47 |
+
|
| 48 |
+
model = torchvision.models.mobilenet_v2(weights=None)
|
| 49 |
+
model.classifier[1] = torch.nn.Linear(model.classifier[1].in_features, len(classes))
|
| 50 |
+
model.load_state_dict(torch.load(weights, map_location="cpu", weights_only=True))
|
| 51 |
+
model.eval()
|
| 52 |
+
|
| 53 |
+
tf = transforms.Compose([
|
| 54 |
+
transforms.Resize((224, 224)),
|
| 55 |
+
transforms.ToTensor(),
|
| 56 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 57 |
+
])
|
| 58 |
+
|
| 59 |
+
img = Image.open("card.png").convert("RGB")
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
probs = torch.softmax(model(tf(img).unsqueeze(0)), dim=1)[0]
|
| 62 |
+
rank_suit, idx = classes[int(probs.argmax())], int(probs.argmax())
|
| 63 |
+
print(rank_suit, f"{float(probs[idx]):.3f}")
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
## Training data
|
| 67 |
+
|
| 68 |
+
Collected and auto-labelled from live play: **3254 full cards (131×176 px)** in 52
|
| 69 |
+
balanced classes, plus 3967 earlier corner crops (42×54 px) from the abandoned
|
| 70 |
+
first pipeline. Published as
|
| 71 |
+
[`solitaire-cards-dataset`](https://huggingface.co/datasets/solitaire-cards-dataset).
|
| 72 |
+
Labels were produced semi-automatically (a local vision-language model plus manual
|
| 73 |
+
sorting), so expect a small amount of label noise — the reported accuracy is
|
| 74 |
+
measured against those labels, not against a human-audited test set.
|
| 75 |
+
|
| 76 |
+
## Limitations
|
| 77 |
+
|
| 78 |
+
* It expects a **whole card**, not a tiny corner crop. The older 42×54 corner
|
| 79 |
+
dataset (also published, folder `corners`) is out of distribution: a corner
|
| 80 |
+
crop fed to this model returns a wrong class with moderate confidence — the
|
| 81 |
+
corner pipeline was abandoned for exactly that reason.
|
| 82 |
+
* Trained on the **Microsoft Solitaire Collection** card art at 1920×1040. Other
|
| 83 |
+
card designs, fonts or resolutions are out of distribution.
|
| 84 |
+
* It expects a **fully visible card**. A partially overlapped card inside a fan
|
| 85 |
+
(only its top strip visible) is read unreliably — that is a data limitation, not
|
| 86 |
+
a code bug, and it is documented in the agent's engineering notes.
|
| 87 |
+
* It classifies a single card crop; it does not detect cards on a full screenshot
|
| 88 |
+
(that geometry lives in the agent's `vision/` layer).
|
| 89 |
+
|
| 90 |
+
## Links
|
| 91 |
+
|
| 92 |
+
* Code, tests and the full agent: [solitaire-computer-agent](https://github.com/WildFuria/solitaire-computer-agent)
|
| 93 |
+
* Interactive demo: [Space](https://huggingface.co/spaces/WildFuria/solitaire-card-recognizer-demo)
|
| 94 |
+
* Case study: [ai-wiki.tech](https://ai-wiki.tech/en/cases/autonomous-computer-agent/)
|
| 95 |
+
|
| 96 |
+
## License
|
| 97 |
+
|
| 98 |
+
MIT. MobileNet v2 architecture comes from `torchvision` (BSD-3-Clause).
|