3♠︎8♥︎K♣︎J♣︎8♣︎A♣︎K♥︎2♦︎10♠︎7♦︎8♠︎3♦︎A♦︎7♣︎3♥︎10♥︎J♣︎7♠︎
One of our test photos, read by the system described here. Each tilted box is an OBB around a card corner found by the first network, the Spotter; the arrow points toward the rank. Each label is the card the second network, the Sniper, read from that corner. All 18 corners are found and all 18 reads are correct.

Mojenn's card reader looks at a table through a phone camera and names every card it can see. It works in two stages. The Spotter finds every card corner in the frame. The Sniper reads each corner's index, the rank and suit printed in the corner, and names the card.

Both stages used to work with axis-aligned bounding boxes (AABB): rectangles whose sides follow the edges of the image. That's the standard way to train an object detector, and the reader already worked with cards at any angle. Its hardest case was cards fanned very close together, where a tilted corner's AABB also takes in its neighbours. This article explains how both networks moved to object-aligned bounding boxes (OBB), boxes that turn with the card, and how much more accurate that made the reader.

The OBB reader at work in DeckCheck, verifying a tightly spread deck. Each tilted box follows its corner: orange while the corner is found but not yet confirmed, green once the card is read. The counter climbs to 52 of 52.
0
wrong cards accepted, in 165,222 accepted reads on the untouched test set (renders and real photos, visible and partly covered corners)
97.2%
of fully visible corners in held-out real photos read and accepted
97.3%
of partly covered corners declined. The rest were read as their own card, never as another one
99.2%
of corners found by the Spotter in held-out real photos, with 99.8% of its detections correct

The design goal throughout was simple: never name the wrong card. A corner the reader can't read only costs a prompt to move the phone closer. A wrong card that the app believes is much worse. Every choice below was tuned against wrong cards first.

Two networks, two resolutions

Why not have one detector find the corners and name the cards? Because of pixels. To run fast on a phone, the Spotter looks at a scaled-down copy of the camera frame, 1280 × 960 instead of the sensor's 4032 × 3024. That's plenty to see where the corners are, even far away, but a far card's index is then only a couple of dozen pixels tall, too few to tell an 8 from a 3. So once a corner is spotted, that region is cut from the original frame at full resolution, turned upright using the OBB's direction, then scaled and stretched to the Sniper's fixed input.

1Camera frame
The camera frame: a fanned hand of cards on a striped cloth

The phone captures the full sensor, 4032 × 3024 pixels.

2Spotter, on a scaled-down copy

A 1280 × 960 copy is enough to find every corner and its direction, quickly.

3Corners cut from the full frame
  • Upright corner crop
  • Upright corner crop
  • Upright corner crop
  • Upright corner crop
  • Upright corner crop
  • Upright corner crop

Each corner is cut from the original frame at full resolution, turned upright and scaled to 64 × 128.

4Sniper names each card
3♠︎
91%
A♣︎
91%
8♥︎
94%
K♣︎
95%
8♠︎
95%
7♠︎
92%

The Sniper reads each strip, and accepts the read only when it is sure.

The two stages, on the photo above. (Only the 1280 × 960 copy of this photo was kept, so these strips are cut from it; in the app they come from the full-resolution frame.)
A small, pixelated patch of a table photo around a far 8 of clubs, as the Spotter sees it
What the Spotter sees: the index is 24 px tall
  • Upright crop read as 3♣︎
    cut from the scaled copy
    3♣︎
    42%
  • Upright crop read as 8♣︎
    cut from the full frame
    8♣︎
    89%
A far 8♣ in one of our real photos. In the Spotter's scaled-down copy its index is about 24 pixels tall: enough to find, not to read. A crop cut from that copy is a blur, and the Sniper's best guess is 3♣︎ at 42%. Cut from the full-resolution frame, where the index is 75 pixels tall, the same corner reads as 8♣︎ at 89%.

Splitting the work has a second advantage: speed. The Spotter runs once per frame, however many cards are on the table, but the Sniper runs once per corner, and a full table can show dozens of corners. So each frame, the app sends the Sniper only a fixed number of corners, which keeps the frame rate steady. Because every corner is followed from frame to frame, the app knows which cards are already recognized, and spends that budget where it helps most:

  1. a couple of corners that are already recognized, re-read in turn to check that every card is still where the app thinks it is;
  2. corners that are being read but aren't confirmed yet;
  3. corners seen for the first time;
  4. last, corners the Sniper couldn't read so far, such as partly covered ones, retried when there's room.

Every other recognized card keeps its name without being read again. That is what lets a phone follow a whole table without slowing down.

What an AABB holds on a crowded table

A card's index is a tall, narrow strip, more than twice as tall as it is wide, and it follows the card's angle. The smallest AABB around a tilted index grows quickly: at 45° it covers almost two and a half times the index's own area. In a tight fan, that extra area is where the next card's index sits. Try it:

AABB area
2.4× the index
Neighbour's index inside it
25%
OBB (object-aligned)AABB (axis-aligned)Neighbour caught in it
Drag the slider to turn the 7♠ and its neighbour. The AABB (dashed) grows to hold the tilted index and soon takes in part of the 8♥. The OBB fits the 7♠ at every angle.

On the photo at the top, the AABBs are on average 1.64× the size of the OBBs, and up to 2.75×. Compare the two views of the same corners:

Top: AABBs around the corners. The more a corner is tilted, the more its box takes in background and pieces of the neighbouring cards. Bottom: OBBs, one per index, with only that index inside.

The Sniper used to read a square crop around each AABB. It learned to read the corner in the middle and to ignore the neighbouring indexes around it, and it did. But a crop cut along an OBB is a simpler problem: it holds only the corner to recognize, centred, with no neighbouring card. The model trains more easily on it and gets more right. Moving to OBBs improved four things at once:

  • Tighter crops. The Sniper sees one index and nothing else, even in the tightest fan.
  • Better duplicate removal. The detector proposes several boxes per corner and removes the weaker ones that overlap a stronger one. OBBs of close tilted corners don't overlap, so neighbours are kept. More on this below.
  • Orientation becomes information. The old Sniper read corners in any orientation, which meant learning every glyph at every angle and telling a 6 from a 9 by layout cues alone. With the OBB's direction, every crop can be turned upright first, so the rank is always on top.
  • Partly covered corners join the training data. The new labels say how much of each corner is visible, so both networks now learn what to do with a corner half hidden under another card. Those are the corners most likely to be misread.

Stage 1: a Spotter that knows which way is up

The Spotter is a YOLOX-Tiny detector with a single class, "card corner". Besides each corner's centre and size, it now predicts the direction of the index's "up", toward the rank, as a unit vector. A unit vector covers the full 360°, so the Spotter tells the rank end from the suit end: an OBB is never just "tilted 30°", it is "tilted 30° with the rank at this end". The arrows in the photo above are that prediction. On held-out real photos the median angle error is about 1.5°, and no box points the wrong way.

Comparing true shapes

A detector produces many overlapping guesses for each corner. A clean-up step called non-maximum suppression (NMS) keeps the most confident guess and removes the others that overlap it too much. With AABBs, two tilted corners side by side can overlap a lot even when the corners themselves don't touch, and the clean-up then removes a real corner:

Overlap of the AABBs
28%
Overlap of the OBBs
0%

The filter takes the 8♦︎ for a second, weaker detection of the 9♣︎ and removes it (faded): a real corner is lost.In this demo, boxes overlapping by more than 25% count as duplicates.

Two neighbouring corners in a tight spread. Upright or sideways, their AABBs fit and don't overlap. In between, the AABBs overlap enough for one corner to be taken for a duplicate. Their OBBs never touch.

The OBB clean-up compares the true, turned shapes, plus a "same spot" rule for thin boxes that barely overlap when slightly shifted. With no retraining at all, comparing true shapes removed a quarter of the Spotter's wrong detections on our test renders (204 to 153) and recovered missed corners (322 to 306).

Don't-care corners

The training images are mostly Unreal Engine renders of card tables: about 95% of the Spotter's training images and 96% of the Sniper's training corners. (Generating these synthetic images in Unreal Engine will probably be the subject of an upcoming article.) A render knows exactly where every corner is, including corners hidden under other cards, so since November each corner is labelled with how much of its rank and of its suit is visible.

This raises a question: what should the Spotter do with a corner that's only partly visible? Calling it background teaches the model to ignore exactly the stacked corners we want it to find. Calling it a target forces it to fire on corners nobody can read. The answer is neither. Corners too covered to read are marked don't care: the Spotter is neither rewarded nor punished for finding them, and in evaluation a detection there counts as neither right nor wrong. Deciding what such a corner says is left to the Sniper.

One of the Unreal test renders with its labels. Green: corners the Spotter must find, with an arrow toward the rank. Orange, dashed: don't-care corners, too covered to read. Fully hidden corners have no box at all.

On a sample training batch, 17% of the Spotter's "is there a corner here?" error came from being told that partly hidden corners were background, a direct contradiction of what we want. With don't-care training, recall on corners 40–60% visible rose from 77% to 93%.

Technical detail: how don't-care corners enter the loss

Don't-care corners are stored as COCO iscrowd regions, so the standard tools already treat them as "ignore". In the YOLOX head, target assignment sees only real corners, so a grid point assigned to a visible corner stays positive even when it lies inside a don't-care box. Any other point whose predicted centre falls inside a don't-care box is left out of the objectness loss. The box, class and angle losses use positives only, so they're unchanged. That mirrors the evaluation rule exactly.

Evaluation needed care too. Far corners can be 3 px wide in the Spotter's input, where a 1.5 px shift halves any overlap score. A detection now matches a corner when the rotated overlap is large enough or its centre is close relative to the corner's height with a similar size. That alone moved real-photo recall from 99.04% to 99.19% and precision from 99.49% to 99.80%, with no change to the model, because correct finds stopped being counted as both a miss and a false hit.

The whole frame, from far away to close up

The first OBB Spotter took a 960 × 960 square. A phone's 4:3 frame fits in it as 960 × 720, so a quarter of the input was grey padding. It had never seen a corner larger than about 100 px, so a player holding a card up to the phone got nothing: it found 0% of real corners larger than 256 px.

The Spotter now takes the full 4:3 frame at 1280 × 960, 78% more picture, with no padding. Training adds close-up tiles cut from the 4K renders, so it sees corners from about 10 px to 600 px. Later rounds added rendered close-ups of one or two cards.

Measured on 1,145 real photos the Spotter never trained on. Over the same versions, photos with no detection at all fell from 228 to 4.

Corners in one photo are about the same size

One fine woven tablecloth produced a strange result: a neat column of small false detections, the weave lining up with the network's sampling grid. Simple size limits couldn't remove them without losing real distant corners, because the false boxes were the size of ordinary corners far away. What gives them away is their size relative to the other corners in the same photo. All the cards on a table are the same physical size, so a weak detection much smaller than the confident corners around it is dropped. On the photos with that cloth, this size-coherence rule removed 138 of the 140 false detections without losing a single labelled corner.

Real photos

Renders are perfect teachers for geometry but imperfect for appearance: real decks, real lighting and real cameras differ. The last round added 626 real photos (786 images, counting turned copies of top-down close-ups), with about 13,700 corner OBBs, each drawn and checked by hand. They were split into training, validation and test images within each group, so every kind of photo is represented in the held-out sets. On 106 held-out real images, the Spotter's missed corners fell from 36 to 16 and its false detections from 13 to 4, while results on renders stayed the same. Of those 16 misses, 12 are in one steep photo where glare hides the far corners, which a person can hardly read either.

Even so, real photos remain a small share of what the networks learn from. What they decide is how the results are judged:

From rendersFrom real photos
Spotter training images11,471 (95%)568 (5%)
Sniper training corners193,092 (96%)8,924 (4%)
Held out for validation and testingvalidation and test renders106 + 112 images

Stage 2: a Sniper that reads upright

The Sniper is a MobileNetV3-Large classifier with 52 outputs, one per card. What changed most is its input. Instead of a square crop around an AABB, in any orientation, it gets a 64 × 128 upright strip, cut along the Spotter's OBB from the full-resolution camera frame: rank on top, suit below, neighbours left out.

64 × 128
1. The Spotter's OBB

The Spotter outlines the corner's index with an OBB and points an arrow toward the rank. Here the 3♥ lies on its side.

Step through how the 3♥ from the photo becomes the Sniper's input.

Here are all 18 corners of the photo as the Sniper sees them, with the card it named and how sure it was:

  • Upright crop read as 3♠︎
    3♠︎
    91%
  • Upright crop read as K♥︎
    K♥︎
    84%
  • Upright crop read as 2♦︎
    2♦︎
    96%
  • Upright crop read as A♣︎
    A♣︎
    91%
  • Upright crop read as 7♣︎
    7♣︎
    92%
  • Upright crop read as A♦︎
    A♦︎
    95%
  • Upright crop read as 8♥︎
    8♥︎
    94%
  • Upright crop read as 10♠︎
    10♠︎
    95%
  • Upright crop read as 7♦︎
    7♦︎
    93%
  • Upright crop read as K♣︎
    K♣︎
    95%
  • Upright crop read as 8♣︎
    8♣︎
    94%
  • Upright crop read as 3♦︎
    3♦︎
    83%
  • Upright crop read as 8♠︎
    8♠︎
    95%
  • Upright crop read as 10♥︎
    10♥︎
    93%
  • Upright crop read as J♣︎
    J♣︎
    95%
  • Upright crop read as 7♠︎
    7♠︎
    92%
  • Upright crop read as 3♥︎
    3♥︎
    92%
  • Upright crop read as J♣︎
    J♣︎
    96%
Every corner of the photo after cropping, from left to right across the frame. Each strip is turned upright using the Spotter's arrow, whatever the card's angle on the table.

Orientation is information

Because every crop is upright, the Sniper is never trained on rotated or mirrored crops. It reads one canonical view, and an upside-down index is simply not a card. That gives the 6-or-9 question a clean answer:

  • Upright crop read as 9♠︎
    upright
    9♠︎
    94%
  • Upright crop read as 6♥︎
    turned 180°
    6♥︎
    14%
A 9♠ from a render, upright and turned upside down. Upright, it's read as the 9♠ at 94%. Upside down, the best the Sniper offers is "6♥︎" at 14%, far too unsure to accept. Over 5,189 test crops of 6s and 9s turned upside down, none was accepted, and the highest confidence was under 40%.

Saying only what you can see

Partly covered corners are now part of the Sniper's training, with honest targets. If the suit is hidden, the target spreads the probability evenly over the four suits of that rank. If the rank is hidden, over the thirteen ranks of that suit. A ramp in between handles corners that are partly readable. The Sniper learns to answer "a 7, suit unknown", which is a low-confidence answer, instead of guessing a suit.

What the Sniper is taught to answer. There's no separate 'unreadable' class: a read is accepted only when the top card's probability is high enough, and a spread-out answer never is.
Fully visible corners from renders: read and accepted
  • Upright crop read as 9♠︎
    9♠︎
    94%
  • Upright crop read as 7♠︎
    7♠︎
    92%
  • Upright crop read as 5♥︎
    5♥︎
    96%
  • Upright crop read as 4♦︎
    4♦︎
    94%
  • Upright crop read as A♣︎
    A♣︎
    96%
  • Upright crop read as 4♥︎
    4♥︎
    95%
Covered corners: the Sniper's best guess, declined
  • Upright crop read as K♣︎
    really 3♦︎
    K♣︎
    2%
  • Upright crop read as 4♦︎
    really 9♦︎
    4♦︎
    8%
  • Upright crop read as Q♣︎
    really 10♣︎
    Q♣︎
    3%
  • Upright crop read as 9♣︎
    really 4♣︎
    9♣︎
    7%
  • Upright crop read as 5♥︎
    really 9♥︎
    5♥︎
    4%
  • Upright crop read as 9♣︎
    really 9♣︎
    9♣︎
    6%
Test crops from renders. For the covered corners, the label shows the true card, the Sniper's best guess and its confidence, all far below what it takes to accept a read. On the test set, 97.3% of partly covered corners were declined, and none was accepted as a different card.

In the app, this shows up as corners that wait until they can be read:

DeckCheck on a loose pile, with a hand moving cards over it. Corners covered by the hand or by another card stay orange. A yellow label such as "?♠" means the suit is clear but the rank isn't yet. Each corner turns green only once it can be read.

Expecting the Spotter's small mistakes

The Spotter's angle is very good but not perfect, and a crop cut from a slightly turned box shows a slightly turned index. The first OBB Sniper, trained only on perfect boxes, didn't cope: with the box turned by 10°, it read only 16% of corners, and it swapped aces and 4s with high confidence.

So each training corner also gets turned copies, cut from the source image with the box turned a few degrees each way and shifted slightly, covering the Spotter's measured error. And because a tilted camera skews a card's corner in perspective, each corner also gets skewed copies, cut from the real image with a slanted window, so every pixel is real.

  • Upright crop read as 3♥︎
    turned −10°
    3♥︎
    94%
  • Upright crop read as 3♥︎
    turned −5°
    3♥︎
    94%
  • Upright crop read as 3♥︎
    turned +5°
    3♥︎
    87%
  • Upright crop read as 3♥︎
    turned +10°
    3♥︎
    85%
  • Upright crop read as 3♥︎
    skewed left
    3♥︎
    91%
  • Upright crop read as 3♥︎
    skewed right
    3♥︎
    92%
The 3♥ from the photo, cut with the box turned a few degrees each way and with perspective skew, as in training. The Sniper reads every version correctly.
Corners read and accepted when the Spotter's box is turned
0%25%50%75%100%0°5°10°15°20°25°30°45°How far the box is turned from the true angleRead and accepted
Before turned copiesNow
3,000 test corners re-cut with the box turned. The Sniper now reads boxes up to about 15° off almost as well as perfect ones, and past that it declines rather than guesses. Before, up to 23 of the 3,000 were accepted as the wrong card at some angles. Now it's 0 at every angle tested.
Technical detail: perspective skew, and choosing the epoch

On a tilted camera, a card's edge and its rank-suit axis are no longer at right angles in the image. In the upright crop, pixel columns shift linearly from left to right, which no rectangle can undo. From the camera geometry, the largest skew is s = (r − 1/r) / 2 for a local foreshortening r, in rows per column:

Camera tilt from verticalCentre of frameFar edge
0° (pointing down)00
20°0.060.27
40°0.270.90

The skewed training copies are cut with a sheared window that grows to hold the whole corner, exactly as the Spotter's box around a skewed corner would. Nothing is invented by warping.

Choosing the epoch. Raw accuracy rewards the wrong thing. After each epoch, validation finds the confidence of the single most confident wrong read: a visible corner read as the wrong card, or a covered corner read as another card. The score is the share of visible corners read correctly with a clear margin above that worst mistake. That picks the model that reads the most cards with room to spare, rather than the one that's right most often but occasionally confidently wrong. The acceptance threshold is fixed on validation, then checked once on the untouched test set.

From renders to real photos

The fully visible corners of the real training photos were added to the Sniper's training. Together with the skewed copies, that was the largest single step:

The first two rows were measured on the older photo set and the last on the new held-out test photos, so the sets differ, but the direction and size of the gain are clear. Wrong cards accepted on real photos went from 8 to 2 to 0.

Results

On the final test split, never used for any choice, with the threshold fixed beforehand:

  • No wrong card accepted in 165,222 accepted reads.
  • 95.3% of fully visible corners read and accepted overall, and 97.2% on held-out real photos.
  • 97.3% of partly covered corners declined. The rest were read as their own card.
  • Spotter on held-out real photos: 99.2% recall, 99.8% precision, median angle error about 1.5°.

Each hard case now has a specific answer:

Hard caseHow the OBB pipeline handles it
Neighbouring indexes next to the cornerThe crop follows the corner's own OBB, so it holds only that index
Close tilted corners in a tight fanDuplicate removal compares OBBs, the true shapes
Telling a 6 from a 9Crops are always upright, and upside-down crops are never accepted
A partly covered cornerTraining targets say only what is visible
Small errors in the Spotter's angleTurned training copies
Perspective skew on a tilted cameraSkewed training copies, cut from real pixels
Far cards, too few pixels to readThe Spotter works on a scaled-down copy, but each crop is cut from the full-resolution frame; corners that are still too small prompt "move closer"
Renders don't look like real decksHand-labelled real photos in both networks, with a held-out real test
False detections on woven clothSize coherence
Choosing the acceptance thresholdChosen by margin on validation, then checked once on an untouched test

A bonus: grouping the table in Ranger

Ranger computes the odds of every hand at a poker table. For that, knowing the cards isn't enough: it needs to know whose they are, which cards make the board and which two belong to each player. OBBs turned out to be just as valuable here:

  • A whole card from one corner. An AABB only says where a corner is. An OBB also says which way the card points and how big it is, so the card's centre and outline follow from a single corner. Cards can then be compared centre to centre, in card sizes: two cards close together make a hand, and an evenly spaced row of three to five makes the board.
  • Two corners, one card. A card often shows both of its corners. Two corners with the same name, pointing away from each other at the right distance, are one card, not two. The same name twice, pointing the same way, means one of the reads is wrong.
  • Cards under cards. From one visible corner, Ranger knows where the card's other corner must be. If that spot lies under another card, the two cards overlap, so they belong together, like a player's hole cards lying one on the other.
  • A map of the whole table. As the phone pans, corners are matched from frame to frame by their position and direction and placed on a map of the table, so cards that leave the view are kept.
Ranger 3 scanning a table. The board (yellow) and each player's hand (Seats 1 to 4) get an outline as soon as their cards are read, and the line at the bottom lists the groups. Most cards are labelled at both corners, and each counts once.

What's still hard

Each example below is the real pipeline, run on one of our photos.

Occluder reads

Sometimes a corner's suit is completely hidden, and the covering card's own suit symbol sits exactly where the hidden one was. Then the crop really does show another card. Here a J♦ lies over a Q♥, and the jack's diamond sits right where the queen's heart should be:

Q♥︎J♦︎J♦︎Q♦︎ ?
  • Upright crop read as Q♦︎
    covered Q♥
    Q♦︎
    61%
  • Upright crop read as Q♥︎
    Q♥︎
    95%
  • Upright crop read as J♦︎
    J♦︎
    93%
  • Upright crop read as J♦︎
    J♦︎
    92%
The Spotter finds all four corners, and three are read and accepted: both J♦ corners and the Q♥'s uncovered corner. The covered Q♥ corner (grey) really shows a Q above a red diamond, and the Sniper's best guess is Q♦︎ at 61%: a wrong card, held back only because the Sniper isn't sure enough. On validation data, reads like this went as high as 87%, so the confidence threshold alone can't be the defence.

Nothing inside the crop can tell this case apart, so the defences have to come from outside it. In the app, a card is only confirmed after several agreeing reads, and a disagreeing read cancels it. The same card can't be on the table twice. A check for a covering card's edge across the index is a candidate for later.

Card faces printed on the felt

Some tables have card designs printed on them. The Spotter finds them, because they are card corners, and the Sniper reads them:

A♥︎ ?A♣︎A♦︎
Lower left: aces printed on the felt, partly under a printed chip. The Spotter finds 3 of them, and the Sniper reads all 3 correctly, accepting A♣︎ and A♦︎ as if they were real cards. For the networks that is the right answer. The app has to reject them another way: they never move, and they have no card edge around them. (This photo is part of the training set.)

Steep, glare-filled scenes

A low camera angle squashes the far cards, and light from a window can wash them out:

Of the 104 labelled corners, the Spotter finds 72 and misses 32 (red, dashed): the far rows, small, foreshortened and faded by glare. The Sniper accepts 62 reads, all correct, and declines the rest (grey, dashed), which would prompt the player to move closer. (Cropped to the table; this photo is part of the training set.)

Steep, glare-filled scenes and stylised novelty decks produce most of the remaining misses. More real photos of those is the likely fix.

The broader lesson: much of the gain came from making each network's job simpler and its labels more honest. An OBB gives the Sniper one centred index instead of a crowded square, and labels that say only what can be seen give both networks a consistent job to learn. Together, that's what made zero accepted wrong cards possible.