Counting Cards with OpenCV to Build a Blackjack Playing Robot

Every card player knows that blackjack comes down to following a few simple rules, which statistically maximize your odds of winning based…

Cameron Coward
7 years ago

Every card player knows that blackjack comes down to following a few simple rules, which statistically maximize your odds of winning based on the cards that have been dealt. Counting cards takes this to the logical extreme by taking into account the history of all of the cards that have been dealt, even from previous rounds. If you were playing with a single 52 card deck and four aces had already been drawn, you would know that no more aces were available and would adjust your next play based on that.

Card counting was how MIT’s infamous blackjack team was able to see incredible returns. This sounds great in theory (if you can get away with it), but card counting isn’t that easy. It requires the ability to remember what’s already been drawn, as well as a thorough understanding of the probabilities and statistics that affect your next move. Of course, this is all mathematical, and that means a computer (or robot) would be very good at it.

In this video, YouTuber Edje Electronics demonstrates the first part of his blackjack-playing robot that will be able to count cards. While he hasn’t actually got to the robot part yet, he has already finished what is arguably the more difficult part of the project: reading the cards themselves. To do this, he used a Raspberry Pi 3 with a camera focused on the table. As cards are put down, an OpenCV program (written in Python) determines what they are.

That program checks the corner of the card, and extracts images of the rank and suit. Those images are then compared to a stored library, and whichever is most similar is chosen as the match. The program can a full hand of cards at a time, and can accurately detect the cards’ value at 3–5 FPS. The next step is to build the actual robot to manipulate the cards, and a card counting algorithm–though the robot may look a little suspicious in an actual casino.

Cameron Coward
Writer for Hackster News. Proud husband and dog dad. Maker and serial hobbyist. Check out my YouTube channel: Serial Hobbyism
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles