Creating a card data set:

Lucas B
3 min readDec 20, 2019

--

Currently, I’m a student at Flatiron datascience bootcamp. I’m interested, for my end of the year capstone project, in creating a convolutional neural network — CNN that can recognize playing cards. In order to do this, I need to create a training set of several thousand playing card images but the problem is that I need several thousand images of every single playing card in different enviornments. Rather than doing this, I created a class called card that would allow me to import a card and create a training set:

To make things simple, I began by defining a the class card() that takes in path as an argument to a jpeg image of a playing card. I also created a function that returned both an image and the name of the card. The name of the card comes from splicing the string and the card number comes from a lookup table that looks at the name and assigins it a value between 0–52 where 0 is the ace of spades, 51 is the king of hearts and 52 is no card.

Next, I had to come up with a way of deleting the background without deleting anything inside the card the card. I wrote a method called preprocess that scanned through the card and deleted any pixel below a certain value of 150 until it ran into something it couldn’t delete. It would then skip the rest of the row and start at a new one. Since I coded this function myself in python, it took significantly longer than any other function. Doing this created a parent instance representing the original card and and new instance which could be refreshed after each operation.

After this, using OpenCv and PIL Images I created a function that would rotate and image and then wrap the image. This would only affect the altered image as I didn’t want have to preprocess the ace each time:

After this, I created a function that would load a random background and then by scanning over the altered card image and replacing the pixel in the background image with a pixel with the card image for all non-zero values in the image.

Although this looks alright, I thought that the image was still a bit off so I passed it through a threshold filter:

After this, I created an Iteration function that performed all these steps. On my Imac at home, I could create a new image in about 10ms.

--

--