Are you bored at home with nobody to play with? Does your family refuse to play cards with you after you defeat them too many times?
Well you don't need a partner to play with anymore, because we're building an artificially intelligent buddy that will always play cards with you! Our AI buddy will be able to play the card game Dobble, also known as SpotIt. Using computer vision, it will be able to match cards just like a human does, but possibly faster.
We wanted this game to be affordable, and we didn't want to clog up your closet with extra hardware, so we're making this as simple as possible - you provide practically any PC or laptop and we provide the artificial intelligence!
Who are we? We're just three random coworkers who happen to be interested in AI.
Together we have almost 50 years of engineering experience, and we're honing our skills by creating a card-game playing software that might be smart enough to beat you! So if you're stuck by yourself with nobody to play with, we've got you!
Only one question remains: are you smarter and faster than an AI?
Well you can become a master AI expert by following along with our tutorial and learn how we taught ourselves more about Machine Learning by using this game as motivation for testing our skillset and pushing the boundaries of our knowledge to tackle a fun and challenging problem!
The Math behind the Card GameMartin Whitworth gave an excellent overview of the mathematics behind the Dobble card game at the MathsJam conference in 2012.
https://www.mathsjam.com/gathering/uk/archive/2012/
He describes the French card game with an interesting combinatoric property : each card has eight symbols on it, and every pair of cards has exactly one symbol in common.
Mathematically, the card game exploits a finite projective plane of order n=7
- n*n + n + 1 = 57 cards
- n*n + n + 1 = 57 symbols
- (n + 1) cards / symbol
- (n + 1) symbols / card
- 1 matching symbols for any 2 cards
The symbols are various images, such as the following:
Each card contains 8 symbols.
Each two card, has 1 (and only 1) common symbol:
NOTE : actual card decks only have 55 cards, for reasons unknown
Different Card GamesThe card deck illustrated in Martin Whitworth's presentation, and the Dobble card deck that I purchased, have some differences.
More specifically, some of the symbols are different between the two card decks.
For the purpose of this project, I will consider the decks to be the same, and attempt to create a solution that supports both. The symbol descriptions have been chosen to represent both variants : Bird(OK), Hammer(STOP), Turtle(ART).
The Data SetWe have captured images of the Dobble card deck with different lighting conditions. The data set has been shared on Kaggle as a public data set.
https://www.kaggle.com/grouby/dobble-card-images
The data set includes the following content:
- 10 card decks, for training
- 1 random set of cards, for testing
- 1 csv file describing which symbols are on each card
- 1 csv file containing the description of each symbol
The first card deck was taken from Martin Whitworth's presentation, and is the only card deck with 57 cards.
Deck 02 and 04 were created by Mario Bergeron with a web cam on a black desk.
The third card deck was created by Mario Bergeron with a flat bed scanner.
Decks 5, 6, 7, 8, and 9 were taken by Kevin Keryk with different lighting conditions.
Deck 10 was taken by Monica Houston with variable nanoleaf lighting source.
This collection of decks provides a limited dataset, but under different lighting sources.
TutorialIn order to run through the tutorial, you need to have git, as well as python 3 installed with opencv, tensorflow, keras.
- On a windows machine, start by installing Anaconda, then launching the Anaconda PowerShell.
- On a linux machine, make sure you have python3 and pip3 installed on your machine.
With python3 installed, add the following additional python packages with pip3:
pip3 install opencv-contrib-python
pip3 install tensorflow
pip3 install keras
pip3 install kaggle
Start by clone the dobble_buddy repository:
git clone https://github.com/AlbertaBeef/dobble_buddy
cd dobble_buddy
Download
the dobble_dataset which contains all of the images that will be used for network training, testing, and validation.
https://www.kaggle.com/grouby/dobble-card-images
The quickest way to do this is manually go and download the archive from Kaggle and extract the contents into the local dobble_dataset folder.
Alternatively, you can use the dobble_dataset_download.py script to automatically download and extract the Dobble data set for you:
python3 dobble_dataset_download.py
Note: If you are using Anaconda under Windows, you will get an error that the term 'python3' is not recognized as the name of a command or program. For the remainder of this tutorial, you should use 'python' in your command entry instead of 'python3' which should map into a version of Python which is version 3 or later.
python --version
On a Windows machine, the Python version reported by Anaconda is 3.8.5 when running the above command:
Downloading the Kaggle data set using the convenience script will require you to have either a Kaggle API key available in your local .kaggle/ folder OR you can override the API key programmatically within the Python code by modifying the default values of the KAGGLE_USERNAME and KAGGLE_KEY variables:
# Replace the following username with the one from the json file for your
# Kaggle API Token
KAGGLE_USERNAME = "aidventure"
# Replace this key with the one from the json file for your Kaggle API Token
KAGGLE_KEY = "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6"
If you are not familiar with how to setup a Kaggle API key, check out the official tutorial on this topic:
https://github.com/Kaggle/kaggle-api
Once you have the dataset available locally to work with, the dobble_dataset_overview.py script provides an overview of the Dobble data set. Execute it with the following command:
python3 dobble_dataset_overview.py
This will display three sets of images, representing:
- the card decks in the data set
- the individual cards (collage from each card deck)
- random examples of card pairs, and the matching symbol
Each card deck will be displayed in a separate window. Press any key to proceed to the next one.
PARAMETERS:
Normalized shape of images : 146 x 146
Card Decks : 10 ['dobble_deck01_cards_57', 'dobble_deck02_cards_55', 'dobble_deck03_cards_55', 'dobble_deck04_cards_55', 'dobble_deck05_cards_55', 'dobble_deck06_cards_55', 'dobble_deck07_cards_55', 'dobble_deck08_cards_55', 'dobble_deck09_cards_55', 'dobble_deck10_cards_55']
TRAINING DATA SET:
Shape of deck01 data (X) is : 0 (57, 146, 146, 3)
Shape of deck02 data (X) is : 1 (55, 146, 146, 3)
Shape of deck03 data (X) is : 2 (55, 146, 146, 3)
Shape of deck04 data (X) is : 3 (55, 146, 146, 3)
Shape of deck05 data (X) is : 4 (62, 146, 146, 3)
Shape of deck06 data (X) is : 5 (55, 146, 146, 3)
Shape of deck07 data (X) is : 6 (55, 146, 146, 3)
Shape of deck08 data (X) is : 7 (55, 146, 146, 3)
Shape of deck09 data (X) is : 8 (58, 146, 146, 3)
Shape of deck10 data (X) is : 9 (55, 146, 146, 3)
VIEW CARD DECKS:
... press any key to continue ...
NOTE
: Make certain that the imagewindow is selected, for the "press any key to continue" to work.
Next, each of the 57 cards will be display, Press any key to continue to the next card, or "ESC" to stop displaying the individual cards.
VIEW CARDS:
... press any key to continue (ESC to quit) ...
We have allocated id 0 for non-playing cards, as shown in the first image. These cards are included in decks 04 and 09, and have a total count of 10, which is balanced with the other cards.
Finally, examples of card pairs, and their matching symbol will be displayed. Press any key to continue with a new random example, or "ESC" to stop.
SYMBOLS:
OrderedDict([(1, 'OneWay'), (2, 'Target'), (3, 'Lightning'), (4, 'Clown'), (5, 'Shades'), (6, 'Spider'), (7, 'Anchor'), (8, 'Tree'), (9, 'Web'), (10, 'Cat'), (11, 'Bomb'), (12, 'Lock'), (13, 'Dobble'), (14, 'Apple'), (15, 'Ghost'), (16, 'Man'), (17, 'Car'), (18, 'Moon'), (19, 'Eye'), (20, 'Clock'), (21, 'Scissors'), (22, 'Dinosaur'), (23, 'Spot'), (24, 'Key'), (25, 'Dog'), (26, 'Crayon'), (27, 'Exclamation'), (28, 'Horse'), (29, 'Cactus'), (30, 'Dolphin'), (31, 'Candle'), (32, 'Zebra'), (33, 'Drop'), (34, 'MapleLeaf'), (35, 'Fire'), (36, 'Turtle(ART)'), (37, 'Skull'), (38, 'Lips'), (39, 'Heart'), (40, 'Question'), (41, 'Cheeze'), (42, 'Sun'), (43, 'Ladybug'), (44, 'Snowflake'), (45, 'Treble'), (46, 'Icecube'), (47, 'Igloo'), (48, 'Carrot'), (49, 'Lightbulb'), (50, 'Bird(OK)'), (51, 'Clover'), (52, 'Flower'), (53, 'Bottle'), (54, 'YinYang'), (55, 'Hammer(STOP)'), (56, 'Dragon'), (57, 'Snowman')])
MAPPING:
OrderedDict([(1, [1, 8, 15, 16, 17, 18, 19, 20]), (2, [1, 9, 21, 22, 23, 24, 25, 26]), (3, [1, 10, 27, 28, 29, 30, 31, 32]), (4, [1, 11, 33, 34, 35, 36, 37, 38]), (5, [1, 12, 39, 40, 41, 42, 43, 44]), (6, [1, 13, 45, 46, 47, 48, 49, 50]), (7, [1, 14, 51, 52, 53, 54, 55, 56]), (8, [2, 8, 26, 27, 36, 41, 46, 51]), (9, [2, 9, 15, 32, 37, 42, 47, 52]), (10, [2, 10, 16, 21, 38, 43, 48, 53]), (11, [2, 11, 17, 22, 28, 44, 49, 54]), (12, [2, 12, 18, 23, 29, 33, 50, 55]), (13, [2, 13, 19, 24, 30, 34, 39, 56]), (14, [2, 14, 20, 25, 31, 35, 40, 45]), (15, [3, 8, 23, 32, 34, 43, 45, 54]), (16, [3, 9, 18, 30, 38, 40, 49, 51]), (17, [3, 10, 15, 24, 35, 44, 46, 55]), (18, [3, 11, 19, 21, 31, 41, 50, 52]), (19, [3, 12, 16, 25, 28, 36, 47, 56]), (20, [3, 13, 20, 22, 27, 33, 42, 53]), (21, [3, 14, 17, 26, 29, 37, 39, 48]), (22, [4, 8, 22, 31, 38, 39, 47, 55]), (23, [4, 9, 19, 29, 36, 44, 45, 53]), (24, [4, 10, 17, 25, 34, 42, 50, 51]), (25, [4, 11, 15, 23, 27, 40, 48, 56]), (26, [4, 12, 20, 21, 30, 37, 46, 54]), (27, [4, 13, 18, 26, 28, 35, 43, 52]), (28, [4, 14, 16, 24, 32, 33, 41, 49]), (29, [5, 8, 25, 30, 33, 44, 48, 52]), (30, [5, 9, 16, 27, 35, 39, 50, 54]), (31, [5, 10, 18, 22, 37, 41, 45, 56]), (32, [5, 11, 20, 24, 29, 43, 47, 51]), (33, [5, 12, 15, 26, 31, 34, 49, 53]), (34, [5, 13, 17, 21, 32, 36, 40, 55]), (35, [5, 14, 19, 23, 28, 38, 42, 46]), (36, [6, 8, 24, 28, 37, 40, 50, 53]), (37, [6, 9, 17, 31, 33, 43, 46, 56]), (38, [6, 10, 20, 23, 36, 39, 49, 52]), (39, [6, 11, 16, 26, 30, 42, 45, 55]), (40, [6, 12, 19, 22, 32, 35, 48, 51]), (41, [6, 13, 15, 25, 29, 38, 41, 54]), (42, [6, 14, 18, 21, 27, 34, 44, 47]), (43, [7, 8, 21, 29, 35, 42, 49, 56]), (44, [7, 9, 20, 28, 34, 41, 48, 55]), (45, [7, 10, 19, 26, 33, 40, 47, 54]), (46, [7, 11, 18, 25, 32, 39, 46, 53]), (47, [7, 12, 17, 24, 27, 38, 45, 52]), (48, [7, 13, 16, 23, 31, 37, 44, 51]), (49, [7, 14, 15, 22, 30, 36, 43, 50]), (50, [1, 2, 3, 4, 5, 6, 7, 57]), (51, [20, 26, 32, 38, 44, 50, 56, 57]), (52, [8, 9, 10, 11, 12, 13, 14, 57]), (53, [18, 24, 31, 36, 42, 48, 54, 57]), (54, [15, 21, 28, 33, 39, 45, 51, 57]), (55, [17, 23, 30, 35, 41, 47, 53, 57]), (56, [16, 22, 29, 34, 40, 46, 52, 57]), (57, [19, 25, 27, 37, 43, 49, 55, 57])])
VIEW MATCHING SYMBOLS:
... press any key to continue (ESC to quit) ...
Example 0 : Matching Symbol = Lightning
Example 1 : Matching Symbol = Bird(OK)
The goal of this project is to automatically detect the Dobble cards, and for each pair, indicate the matching symbol as fast as possible.
How would you solve this problem ?
Share your thoughts in the comments below.
Latest Team Update!We have been hard at work solving the problem with an example solution for all of our project backers to try out, check out this follow-on Hackster Project:
https://www.hackster.io/aidventure/training-the-dobble-challenge-568854
Comments