, , , , , , , , , , ,

‘Aren’t you done yet?’ Arkwood said sneeringly. ‘No. No I am not.’ Of course, he is referring to my endeavours to detect guitars through a webcam. The webcam is attached to my Raspberry Pi computer. I am running Python code on my Raspberry Pi. The Python code is using the OpenCV library for object detection. I have written my own OpenCV haar cascade classifier for detecting guitars. It’s all kinda working to some extent.

In my initial post I set up all the software required for creating a classifier, after undertaking some research into the subject. Using 15 photographs of my electric, bass and acoustic guitars, I trained a guitar classifier. I tested it using the webcam. It did not detect a guitar. No, it detected a baseball bat.

In my second post I redrafted my plan, so as to only detect Fender Stratocaster style electric guitars. I took 60 photographs of my Fender and Harmony guitars and the results fared better. I even had an attractive gorilla to model the guitar for the webcam.

In this post I am going to train my classifier using photographs of Fender Stratocaster guitars that I have procured online – 14 in total, all with red bodies and white scratchplates. We’ll use 100 negatives and produce 250 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 80 -h 30"

30 training stages were set:

opencv_haartraining -data guitarcascade -vec samples.vec -bg negatives.dat -nstages 30 -nsplits 2 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 250 -nneg 100 -w 80 -h 30 -nonsym -mem 2048 -mode ALL

But it only reached 20 stages before stalling, so I spat out an intermediate xml file:

convert_cascade --size="80x30" guitarcascade guitarcascade-inter.xml

Now we are ready to detect guitars through the webcam!

Initial testing yielded a few false-positives, what with the small number of samples, so I decided to tweak the OpenCV settings in my Python code:

guitars = guitar_cascade.detectMultiScale(gray, scaleFactor=2.3, minNeighbors=5, minSize=(240, 90))

I added a minSize parameter, so as to ignore the detection of any object which is significantly smaller than a guitar. Stack Overflow provided some useful detail on the detectMultiScale parameters.

My red Fender Stratocaster guitar was detected every time without fail:


Yet my grey Harmony guitar was not (even though it’s a Stratocaster style guitar):


My bass guitar stood no chance:


‘What does it prove?’ Arkwood said, barely able to conceal his disinterest. ‘It proves, my dear Belgian friend, that my guitar classifier is able to pick out Fender Stratocaster guitars with red bodies and white scratchplates. It has been trained using online photos, then tested on my own guitars.’ He shrugged his shoulders.

The guitar classifier is far from perfect. I promised Arkwood that it would be used to spot guitars on a TV screen, but we ain’t there yet. Still, as Richard Richard would say, “Faint heart ne’er won fair maid”.