Tags

, , , , , , , , ,

Arkwood said, ‘Hey, those Lego policemen are watching me! Over by the plants.’ It does not help that he smokes marijuana through a glass pipe, but nevertheless I said I would help him out. I will use OpenCV object detection software on my Raspberry Pi computer to trap those pesky cops. But first, I need to create my own haar cascade classifier for detecting Lego policemen, and then test it through with some Python code and a webcam attached to my Pi.

Thankfully I already have all the necessary OpenCV software to create my classifier, and the Python code that will use it to detect the fuzz. All the detail is in my Guitar detection using OpenCV post.

I need some positive images to train my classifier with:

lego_policeman_01
lego_policeman_02
lego_policeman_03

I have 14 positive images (.png, 120px width, cropped, front view), making use of 3 policemen and varying backgrounds.

I have 100 negative images (.png, 960px), random photos of my house (not a bobby in sight).

I created my training samples:

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 40 -h 63"

And churned out a combined .vec file:

find samples/ -name '*.vec' > samples.dat
./mergevec.exe samples.dat samples.vec

Now it is time to do some training:

opencv_haartraining -data haarcascade_lego -vec samples.vec -bg negatives.dat -nstages 20 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 100 -w 40 -h 63 -nonsym -mem 2048 -mode ALL

It stalled at stage 15 of training, so I used the convert_cascade application to spit out my Lego classifier xml file:

convert_cascade --size="40x63" haarcascade_lego haarcascade_lego-inter.xml

Would be good to get more training stages under my belt, so I’ll try some different configurations later on. But for now…

Here are the results of testing my Lego classifier on a little tableau upon my livingroom carpet – using a webcam attached to my Raspberry Pi and the Python code I put together on my previous guitar detection post:

lego_01_b_rb_b

Our policemen have been detected amongst a collection of reprobate (including a Lego soldier).

lego_02_b_b_b

And now the entire constabulary turn out for a parade. Worth noting that these particular three policemen were NOT used in the training process.

lego_03_b_w_b

Our Lego doctor’s uniform is not mistaken for an officer of the law’s.

lego_04_b_r_b

Neither is this red dude.

lego_05_b_j_b

Our Japanese warrior is undetected due to his dances with the shadows.

lego_06_b_v_b

And our vampire can likewise blend into the night.

lego_07_rb_b_b
lego_08_b_b_r

Moving the Lego chaps about does not pose a problem for our classifier.

lego_09_b_n_j_b
lego_10_b_n_v_b
lego_11_b_n_v_r

Or putting someone beside the police motorbike.

Now, the Lego classifier is far from perfect, what with being a fairly small number of samples and training stages. There were a few false-positives too:

lego_12_b_j_b_02

Damn Japanese warrior has blown his cover. Once or twice the carpet was detected (what the @*&!) or a random chunk of nothingness.

Plus, interestingly, the red Lego dude was detected on occasion:

lego_13_b_r_b_02

Perhaps his all-red uniform struck a chord with the policemen’s all-black uniform? The doctor and the soldier were not detected.

Next up I want to tighten the classifier and accomplish more training stages, then perhaps I will have a system capable of keeping an eagle eye on the local constabulary. Arkwood will be able to smoke his pipe full of weed without fear of arrest.

Ciao.

Advertisements