element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Raspberry Pi Poker Player
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ddoherty
  • Date Created: 22 Jan 2020 7:24 PM Date Created
  • Views 1538 views
  • Likes 8 likes
  • Comments 1 comment
Related
Recommended

Raspberry Pi Poker Player

ddoherty
ddoherty
22 Jan 2020

Introduction

As a self-proclaimed Raspberry Pi and Texas hold’em nerd, I decided to build a Raspberry Pi Poker Player to bring with me to games to help make smart bets.  Here is a video of it in action. The poker player performs 3 main steps: (1) identify cards, (2) identify hands, and (3) make bets. I will discuss how MATLAB helped me program these steps using deep learning, decision trees, and other techniques, and then deploy the final algorithm onto a Raspberry Pi.

Hardware used

  • Raspberry Pi: brains of the project.
  • USB webcam: to read images of the cards.
  • Raspberry Pi Display: to assist in visualization.
  • Push buttons: to call/fold bets.

 

image

Fig 1: Hardware used in project

 

Identify cards

image

Fig 2: Webcam images are used as the input to the neural network that identifies cards

The first step is for the Raspberry Pi to identify cards. The webcam connected to the Pi captures images of the cards and a deep learning algorithm identifies them. MATLAB supports several popular deep learning networks that can be used for image classification, including SqueezeNet, AlexNet and GoogLeNet. AlexNet was chosen as it is sufficiently small to be deployed to a Raspberry Pi and it has relatively high prediction accuracy. We use transfer learning to tune it for our specific problem. Here is a detailed example that shows how to perform transfer learning using AlexNet.

image

Fig 3: Collect data for transfer learning

The first step was to collect images of the cards we want to be able to identify. We took a series of snapshots using a webcam as we showed cards in different light settings and orientations. After manually selecting images that captured the features of each card with minimal blur / distortion, we rotated each image with MATLAB to avoid overfitting the network and to ensure it can recognize cards for different orientations. We ended up with over 10,000 images which were used in transfer learning. Instead of loading each image file into memory during training, we used the imageDatastore in MATLAB which let us work with the overall collection of images. The function augmentedImageDatastore was used to resize the images to 227x227 pixels which is the predefined input size of AlexNet. After applying transfer learning using these images, our network was able to effectively identify cards in all of our tests.

Identify hands and make bets

After identifying cards, the poker player is ready to identify hands. The flowchart in Figure 4 describes how this is implemented in MATLAB.

image

Fig 4: Flowchart to identify hands

Let’s use the example hands in the table below to better understand this flowchart. Note that the final 5 cards in each hand are community cards that are shared among all players.

image

After identifying cards and hands, the Poker Player is ready to place bets!  The betting algorithm is very simple and based solely on what the identified hand is.  It starts at 1 USD for a Highcard and increases by 1 USD for each subsequent ranked hand up to 10 USD for a Royal flush. The algorithm only makes bets on the complete hand, but it could certainly be updated and expanded.

 

Embed algorithm onto Raspberry Pi

We generate C++ code from our MATLAB algorithms and embed it onto the Raspberry Pi using MATLAB Coder.  We wrote a function main_poker_player.cpp that serves as the main script of the executable, where we specify which functions to generate code for. The generated code and executable (including modified AlexNet and betting logic algorithm) are downloaded directly to the Pi for standalone execution – we just need to plug in a camera and run the .exe file.

image

Fig 5: Generate code and deploy to the Raspberry Pi using MATLAB Coder

 

To understand this workflow in more detail and find additional resources, refer to the video here.

Conclusion

With that we have seen how to build a simple Raspberry Pi Poker Player using MATLAB. While this is a good start there are certainly areas for improvement, for example we could write a more sophisticated betting algorithm that is able to escalate bets and even bluff.  Share your comments and ideas on how to improve the poker player! To request code, contact raspberrypi@mathworks.com.

  • Sign in to reply
  • rscasny
    rscasny over 5 years ago

    Great blog. Now I'm ready for Vegas!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube