Playing card detection using OpenCV (Mark IV)

Tags

, , , , , , , , ,

Peters, my big-boned lodger, has used his superb poker skills to win a sizable amount of cash from my coffers. Damn him! But I have a plan. I am building a poker bot using a webcam, my Raspberry Pi computer and some Python code. I will soon exact my revenge.

In my last post, Playing card detection using OpenCV (Mark III), I successfully detected the three of hearts playing card using a webcam. In this post I will attempt to detect the Queen of hearts.

The only problem is, the Queen of hearts is a rather busy looking card, with all those colours and lines playing havoc with an OpenCV haar cascade classifier:

qhearts_001

So, in order to train a classifier that has a parakeet’s chance in hell of detecting the Queen of hearts, I am only going to target a part of the card:

qhearts_minicard_001

Think of it as a mini-card, a card within a card if you will. The code in my previous post will work just the same:

  • First we attempt to detect the mini-card, consisting of the letter Q and the tiny heart motif
  • Next we attempt to detect the colour red within the mini-card
  • Finally we attempt to detect the tiny heart motif within the mini-card

Okay, let’s create a classifier for our Queen of hearts mini-card. 9 stages of training were completed on 1 positive image, using the following parameters:

perl createtrainsamples.pl positives.dat negatives.dat samples 250 "./opencv_createsamples  -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 16 -h 44"
opencv_haartraining -data haarcascade_Qhearts -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 100 -w 16 -h 44 -nonsym -mem 2048 -mode ALL

I also created a classifier for the tiny heart motif. 16 stages of training were completed on 42 positive images, using the following parameters:

perl createtrainsamples.pl positives.dat negatives.dat samples 500 "./opencv_createsamples  -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 10 -h 12"
opencv_haartraining -data haarcascade_Qhearts -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 500 -nneg 200 -w 10 -h 12 -nonsym -mem 2048 -mode ALL

Using the Python code in said previous post, here are the results of my poker bot:

playingcards_qhearts_khearts_minicard

Fantastic! The bot has detected the Queen of hearts from the King of hearts.

playingcards_qhearts_jhearts_minicard

It’s had no problem detecting the Queen of hearts from the Jack of hearts either.

playingcards_qhearts_qspades_minicard

Our red colour detection has ensured that the sultry Queen of spades does not get falsely detected.

playingcards_qhearts_qdiamonds_minicard

The Queen of hearts has even spurned its doppelgänger the Queen of diamonds.

playingcards_qhearts_qdiamonds_minicard_falsedetection

Sadly, though, the Queen of diamonds did breech our tiny heart motif classifier on occasion – hmm, this will need sorting.

‘It’s only a matter of time,’ I told Peters, ‘before my poker bot whips you at cards.’

The gravity of the situation hit my behemoth buddy like a brick. His flabby face turned crimson as he grabbed me by the wrists, stuffing my hands into the toaster and pulling down the lever. My poor fingers sizzling to a crisp, I managed to relinquish his hold with a sturdy head-butt to the bridge of his nose, breaking it and spilling blood down his cheap polyester shirt.

‘Fuckin’ die!’ I screamed, my hot digits now clamped tight around his neck. ‘Never!’ was his thick Dutch retort, as a knee socketed my groin.

Still, the project goes on. Soon I will begin to program the logic, so that my poker bot can make intelligent decisions on what to do with the cards it detects.

But for now, my hands are in a bucket of ice. And Peters has a wet towel round his neck where the throttling marks of my fingers have left an imprint.

Ciao!

P.S.

I made a few minor adjustments to the code from the previous post, tweaking the minNeighbors setting and dropping the minSize setting:

self.card_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=2)
self.motif_cascade.detectMultiScale(roi_gray, scaleFactor=1.1, minNeighbors=4)

And here’s the tiny heart motif detection in all its glorious finery:

qhearts_minicard_motif

Follow

Get every new post delivered to your Inbox.

Join 53 other followers