官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 东台市| 长沙市| 兴文县| 团风县| 昆明市| 宝应县| 樟树市| 九龙坡区| 铜川市| 泸西县| 盐边县| 大埔县| 邵阳市| 双桥区| 古田县| 枞阳县| 深州市| 乐亭县| 新竹县| 曲靖市| 五华县| 寻甸| 奉节县| 左权县| 璧山县| 阜平县| 红安县| 朔州市| 宁都县| 柳州市| 吴堡县| 玉田县| 晴隆县| 顺昌县| 青海省| 高密市| 丹寨县| 中阳县| 江安县| 武清区| 横峰县|