Opencv training classifier creation XML document

Source: Internet
Author: User
Tags file info

Opencv training classifier creation XML document (for conversion)



I found Chinese documents on the Internet and found that most of the articles were reposted, and there were errors in the two articles. After two days of exploration, I finally succeeded in training the classifier, I would like to share with you here.

Http:// ()


The entire process is divided into two steps:
1. Create a sample
2. Train the Classifier

Let me talk about it one by one.

1. Create a sample

◆ There are two types of samples: positive samples and negative samples (also translated into positive samples and inverse samples). Positive Samples refer to the target samples to be checked (such as faces, automobiles, negative sample refers to any other image.

◆ All the sample images should have the same size, such as 20*20, and be placed in the corresponding file directory,



◆ Collection file format and description file format)

Collection file format is the following description file:






Description file format:
[Filename] [# of objects] [[x y width height] [... 2nd object]...]

[Filename] [# of objects] [[x y width height] [... 2nd object]...]

[Filename] [# of objects] [[x y width height] [... 2nd object]...]



(X, y) indicates the coordinates in the upper left corner. width and height indicate the width and height of the sample respectively. Here, my image is 20*20, so both values are 20.


◆ The negative sample is described in the collection file format and the positive sample is described in the description file format! (Many articles on the Internet are wrong !)

Steps for creating a sample for a sequence:

1. Place all positive sample images in the posdata folder and all negative sample images in the negdata folder.

(Here I use a human face image sample as an example)

(Note: The above 20*20 images are from the MIT face library and can be downloaded from csdn)

2. Create descriptive files for positive and negative samples respectively

A. Create the format file info.txt for the positive sample, and put the file in the same directory as the sample image. For example, my directory is C:/opencv2.1/bin/posdata.

A) enter the following command in the command line: Dir/B> info.txt

B) Open info.txt, press Ctrl + H, and replace all BMP files with BMP 1 0 0 20 20

C) Delete the last line of info.txt"
D) The result is as follows:


B. Create a collection File Format File bg.txt for negative samples and put the file in the same directory as the sample image. For example, my directory is I:/negdata.

A) enter the following command in the command line: Dir/B> bg.txt

B) Delete the “bg.txt in the last line of bg.txt"
C) The result is as follows:


3. Create a sample.
Opencv self-built EXE file with sample creation, in... In the/opencv2.1/bin directory, I create 10 samples:

Command: opencv_createsamples.exe-Info C:/opencv2.1/bin/posdata/info.txt-vec a. Vec-num 10-W 20-H 20



(For details about the parameter usage of opencv_createsamples.exe, visit;

It should be noted that the parameters I use here do not have-BG, because according to that document, with-VEC and-Info, it indicates: create training samples from some (create sample from many positive samples without distortions)

After a great deal of experience, we finally saw that the sample was successfully created, and the next work was much simpler.

Train Classifier


Still... In the/opencv2.1/bin directory, enter the following command:
Opencv_haartraining.exe-data-vec c:/opencv2.1/bin/. vec-bg I:/negdata/bg.txt-NPOs 250-nneg 800-nstages 4-nsplits 2-MEM 512-nonsym-W 20-H 20-minpos 100





1. the-VEC parameter must be. VEC, and write the complete path. I tried to use the relative path, but the training will always fail; bg.txt also uses the absolute path;
2. For both-W and-h, write the same size as the sample size.
3. If the memory cannot be read, the format of bg.txt may be incorrect.
4. For parameter usage of opencv_haartraining.exe, refer to the URL of opencv_haartraining.exe.

Http://, which will be detailed.

The result is as follows:

(The actual results may differ from each other. However, if you see the final result, the training is successful.


A cute data. xml file is generated in the bin directory. This is what we want!

This article from the csdn blog, reproduced please indicate the source:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.