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:
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:
Our policemen have been detected amongst a collection of reprobate (including a Lego soldier).
And now the entire constabulary turn out for a parade. Worth noting that these particular three policemen were NOT used in the training process.
Our Lego doctor’s uniform is not mistaken for an officer of the law’s.
Neither is this red dude.
Our Japanese warrior is undetected due to his dances with the shadows.
And our vampire can likewise blend into the night.
Moving the Lego chaps about does not pose a problem for our classifier.
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:
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:
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.