D:/face constructing face trainer as an example
One: Sample creation
The training sample is divided into a positive sample and a counter sample, in which the sample is a sample of the target to be checked, and the inverse sample refers to any other image.
Negative samples can come from any image, but these images cannot contain target features. A negative sample is described by a background description file. The background description file is one.
Create a positive and negative sample catalog
mkdir D:\face\posdata
mkdir D:\face\negdata
Put the positive and negative sample pictures into 2 folders respectively.
Generating a negative sample description file
CD D:\face\negdata
dir/b > Negdata.dat # Delete the last line of Negdata.data, as the last is the description file itself.
Generating a positive sample description file
CD D:\face\posdata
dir/b > Posdata.dat #删除posdata the last line of. Data because the last is the description file itself
Then add a file description for each line in the Posdata.dat (number of images, target start, end position, height, width), for example
1.jpg 1 1 1 23 23
2.jpg 1 1 1 23 23
Use the Createsamples command to generate a positive example training. vec file.
Opencv_createsamples-info D:\face\posdata\posdata.data
-vec D:\face\data\pos.vec
-num 10-w 20-h 20
The Pos.vec files generated after running are used for later training
Two: Training classifier
With Traincascade (only the haartrainning command in Opencv2, note that the parameter format for version 2 3 is slightly different).
The command is as follows:
Opencv_traincascade-data D:\face\data\cascade #分类器的存放路径
-vec D:\face\data\pos.vec #正样本的vec文件
-BG D:\face\negdata.dat #付样本描述文件
-numpos #正样图片文件本个数
-numneg #付样本图片文件个数
-numstages 5 #训练阶段数, depending on the number of samples, is too large to indicate an error: Train DataSet for temp stage can is not filled. .
-mode all #训练的haar特征集的种类. Basic uses only vertical features. All uses a vertical and 45-degree angular rotation feature.
- w 20-h #样本宽和高
If the parameters are set to True, the error is still reported: Train DataSet for temp stage can is not filled. Branch training terminated.
It may be that the-GB parameter does not have a file name, which can be paid to the sample's description file in the execution directory, negative samples of the description of the image to increase the location of the path to solve.
Finally, there is no error, the training file will be generated under the-data path.
Three: Using a well-trained classifier to do the testing
# #TODO haven't found the Performance.exe program yet
Performance.exe
Use Python-cv2 to invoke the generated classifier:
Import Cv2
Cascade = Cv2. Cascadeclassifier (' D:\face\data\cascade\cascade.xml ')
faces = cascade . Detectmultiscale (Gray, 1.3, 5)
OPENCV Construction Training Device