Training the classifier with reference images

 

Can you identify this coastline? Given time, yes.

 
 -- Photo caption, Dante Stella (http://www.dantestella.com/technical/hex352.html)

A small selection of reference images is included in this chapter's code bundle, which can be downloaded from my website, http://nummist.com/opencv/7376_02.zip. They are in a folder called images. Feel free to experiment with the classifier by adding more reference images, since a larger set might yield more reliable results. Bear in mind that our classifier relies on average similarity, so the more times you include a given color scheme in the reference images, the more heavily you are weighting the classifier in favor of that color scheme.

At the end of HistogramClassifier.py, let's add a main method to train and serialize a classifier using our reference images. We will also run the classifier on a couple of the images as a test. A partial implementation of the method is as follows:

def main():
  classifier = HistogramClassifier()
  classifier.verbose = True

  # 'Stalinist, interior' reference images
  classifier.addReferenceFromFile(
    'images/communal_apartments_01.jpg',
    'Stalinist, interior')
  # ...
  # Other reference images are omitted for brevity.
  # See this chapter's code bundle for the full implementation.
  # ...

  classifier.serialize('classifier.mat')
  classifier.deserialize('classifier.mat')
  classifier.classifyFromFile('images/dubai_damac_heights.jpg')
  classifier.classifyFromFile('images/communal_apartments_01.jpg')

if __name__ == '__main__':
  main()

Depending on the number of reference images, this method might take several minutes (or even longer) to run. Fortunately, since we are serializing the trained classifier, we will not have to run such a method every time we open our main application.

Tip

For a large number of training images, you might wish to modify the main function of HistogramClassifier.py to use all images in a specified folder. (Refer to the file describe.py provided for Chapter 3, Training a Smart Alarm to Recognize the Villain and his Cat, for examples of iteration over all images in a folder.) However, for a small number of training images, I find it more convenient to specify a list of images in code so that we can comment and uncomment individual images in order to see the effect on the training.

Next, let's consider how our main application will acquire query images.