简体   繁体   English

Haarcascade分类器培训

[英]Haarcascade classifier training

I'm trying to create an xml file using samples I took with my camera. 我正在尝试使用相机拍摄的示例创建xml文件。 This is a test run where I put my camera against my window and let it take a picture every 30 seconds of passing cars for a while. 这是一次测试,我将相机放在窗户上,每30秒经过一会儿的汽车拍照。 I have now about 200 pictures (just for a small test), but I can't get any further. 我现在有大约200张照片(仅用于小测试),但我无法再继续了。

I made a selecting tool to pick out the cars on pictures (bounding boxes) and mark pictures as negatives if there aren't any. 我制作了一个选择工具来挑选图片(边界框)上的汽车,并将图片标记为底片(如果没有)。 Here are 2 examples of what the application looks like: 以下是该应用程序外观的两个示例:

在此处输入图片说明在此处输入图片说明

The application then saves the marked objects in positive.txt file as follows, skipping the non-marked pictures or the pictures marked as negative: 然后,应用程序将标记的对象保存在positive.txt文件中,如下所示,跳过未标记的图片或标记为负的图片:

/samples/img_0000.jpg 1 74 66 34 30
/samples/img_0001.jpg 2 78 69 31 25 218 129 61 38
/samples/img_0003.jpg 1 83 72 21 21
/samples/img_0005.jpg 1 76 65 19 17
/samples/img_0006.jpg 1 127 112 37 24
/samples/img_0007.jpg 2 83 72 22 21 127 112 36 22
...

The negative pictures are simply saved to negative.txt file as follows: 底片图片仅保存到negative.txt文件中,如下所示:

/samples/img_0002.jpg
/samples/img_0004.jpg
/samples/img_0024.jpg
/samples/img_0026.jpg
...

Finally I try to run the haar training algorithm with /usr/bin/opencv_haartraining -data samples -vec positive.txt -bg negative.txt -npos 99 -nneg 20 -nstages 5 -mem 128 -minhitrate 0.999 -maxfalsealarm 0.5 -nonsym -mode ALL . 最后,我尝试使用/usr/bin/opencv_haartraining -data samples -vec positive.txt -bg negative.txt -npos 99 -nneg 20 -nstages 5 -mem 128 -minhitrate 0.999 -maxfalsealarm 0.5 -nonsym -mode ALL运行haar训练算法/usr/bin/opencv_haartraining -data samples -vec positive.txt -bg negative.txt -npos 99 -nneg 20 -nstages 5 -mem 128 -minhitrate 0.999 -maxfalsealarm 0.5 -nonsym -mode ALL

Don't mind the settings, I simply need this to work before I use a much more powerful computer to do the actual training on actual data with much more pictures. 不用担心设置,在使用功能更强大的计算机对包含更多图片的实际数据进行实际训练之前,我只需要执行此操作即可。

For that I get the following output and error: 为此,我得到以下输出和错误:

Data dir name: samples
Vec file name: positive.txt
BG  file name: negative.txt, is a vecfile: no
Num pos: 99
Num neg: 20
Num stages: 5
Num splits: 1 (stump as weak classifier)
Mem: 128 MB
Symmetric: FALSE
Min hit rate: 0.999000
Max false alarm rate: 0.500000
Weight trimming: 0.950000
Equal weights: FALSE
Mode: ALL
Width: 24
Height: 24
Applied boosting algorithm: GAB
Error (valid only for Discrete and Real AdaBoost): misclass
Max number of splits in tree cascade: 0
Min number of positive samples per cluster: 500
Required leaf false alarm rate: 0.03125

Tree Classifier
Stage
+---+
|  0|
+---+



Number of features used : 261600

Parent node: NULL

*** 1 cluster ***
OpenCV Error: Unspecified error (Vec file sample size mismatch) in icvGetHaarTrainingDataFromVec, file /build/opencv-XZa2gn/opencv-2.3.1/modules/haartraining/cvhaartraining.cpp, line 1930
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv-XZa2gn/opencv-2.3.1/modules/haartraining/cvhaartraining.cpp:1930: error: (-2) Vec file sample size mismatch in function icvGetHaarTrainingDataFromVec

Aborted

Does anybody know what that means? 有人知道这意味着什么吗? Why is there an error with sample size, whatever that is... I also tried replacing relative paths with absolute paths, but I got the same error. 为什么样本大小有错误,无论是什么...我也尝试用绝对路径替换相对路径,但是我遇到了同样的错误。 Is what I'm trying to do actually right, I haven't found any explicit examples on how to create a classifier from existing and marked pictures. 我实际上想做的是正确的,我还没有找到任何明确的示例,说明如何从现有的和标记的图片创建分类器。

It turns out the .vec file is not simply the positive.txt file, it must be generated using opencv_createsamples . 事实证明.vec文件不仅是positive.txt文件,还必须使用opencv_createsamples生成。

So first I created the training_24-24.vec file (which is not just a text file) using the following command: 因此,我首先使用以下命令创建了training_24-24.vec文件(不仅仅是文本文件):

/usr/bin/opencv_createsamples -vec training_24-24.vec -info positive.txt -bg negative.txt -w 24 -h 24 -num 100

which created the training_24-24.vec file. 这创建了training_24-24.vec文件。 Compared to my original question I remade the positive.txt file with square selections. 与我最初的问题相比,我用正方形选择重新制作了positive.txt文件。 Then I launched the classifier training with 然后,我通过

/usr/bin/opencv_haartraining -data samples -vec training_24-24.vec -bg negative.txt -npos 99 -nneg 20 -nstages 5 -mem 128 -minhitrate 0.999 -maxfalsealarm 0.5 -nonsym -mode ALL

The performance is simply awful at this point, it recognises everything but the cars. 此时的性能简直糟透了,它可以识别除汽车之外的所有东西。 This article says that one should use something like 1000 positive and 2000 negative images to start having something acceptable. 本文说,一个人应该使用1000幅正像和2000幅负像来开始获得可接受的图像。 Also it says for the -nstages option: 它还说-nstages选项:

It's useless to set many stage, if you have small number of positive, negative samples 如果您有少量的正负样本,那么设置很多阶段是没有用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM