top of page

Card Player: Detecting Cards


Phase 4 of my Card Player series. I am attempting to create a solitaire playing bot in python.

Phase 4: Label the playing field

Here is the location of the source code for this project: https://github.com/Corey255A1/PlayingCardsNeuralNet


I first had to figure out how to extract out all of the top most cards for labeling. This proved a little challenging since the cards can be offset by various degrees. Also when the stack has several face up ones stacked up, it looks like a single blob during processing.


So my processing steps include, blurring, thresholding based on HSV, and eroding.

That produces the black and white image on the right. As you can see, the 5 and 4 kind of get combined into a super card. So I had to do some extra calculations to get just the top facing card portion of the blob.

I leverage the fact that a card can be only a certain size and the fact that the top card is always the furthest down.


So in the end we wind up with a bunch of rectangles around things.

Now I do a check if it is longer than a card, I crop it to only the bottom part of it. If it is wider than a card, I crop it to only the right side of it. This accounts for long columns and the offset discard pile.

Once we have all of those rectangles, the script then extracts the pixels from each rectangle and passes them in to the neural network to determine what card it is.


It also determines which pile the card belongs to based on the position of the card.

CardExtraction is the source that deals with extracting the cards from the image. I wasn't focusing on making it general, so it is pretty tailored to the default size of the klondike board.

At first, the neural network was having troubles being accurate with the face cards. Specifically with the suits. So I had to do some retraining. What I finally did was tweak the layout of the model itself, tweak the augmentation a little bit, and also (probably the biggest factor) went through and captured all of the cards from the MS Solitaire to train against. It was kind of cool because I actually was using the neural network I had to give its best guess of all of the cards extract from the board, so that I didn't have to manually label each and every card! I just had to correct the few it was having troubles with.

Once I did that I wound up with a network that can detect all of the cards on the board perfectly.

SolitairePlayer handles grabbing the screen and passing the pieces into the model for labeling.

Comments


bottom of page