opencv_traincascade - samplOpenCV错误:参数错误

时间:2018-04-25 10:12:30

标签: opencv haar-classifier

背景:我正在尝试训练自己的OpenCV Haar分类器进行人脸检测。我正在使用Ubuntu 16.04工作的VM,我的工作目录有2个子目录: face 包含2429个正面图像,非面部包含4548个负面图像。所有图像均为png,灰度,宽度和高度均为19像素。我生成了一个文件 positives.info ,其中包含每个正图像的绝对路径,后跟" 1 0 0 18 18",如此:

/home/user/ML-Trainer/face/face1.png 1 0 0 18 18
/home/user/ML-Trainer/face/face2.png 1 0 0 18 18
/home/user/ML-Trainer/face/face3.png 1 0 0 18 18

和另一个文件 negatives.txt ,其中包含每个正片的绝对路径

/home/user/ML-Trainer/non-face/other1.png
/home/user/ML-Trainer/non-face/other2.png
/home/user/ML-Trainer/non-face/other3.png

首先我运行了以下命令:

opencv_createsamples -info positives.info -vec positives.vec -num 2429 -w 19 -h 19

我按预期得到 positives.vec ,然后我创建了一个空目录数据并运行以下内容:

opencv_traincascade -data data -vec positives.vec -bg negatives.txt -numPos 2429 -numNeg 4548 -numStages 10 -w 19 -h 19 &

似乎运行顺利:

PARAMETERS:
cascadeDirName: data
vecFileName: positives.vec
bgFileName: negatives.txt
numPos: 2429
numNeg: 4548
numStages: 10
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: HAAR
sampleWidth: 19
sampleHeight: 19
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC
Number of unique features given windowSize [19,19] : 63960

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   2429 : 2429
NEG count : acceptanceRatio    4548 : 1
Precalculation time: 13
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3| 0.998765| 0.396218|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 1 minutes 7 seconds.

但后来我收到以下错误:

===== TRAINING 1-stage =====
<BEGIN
POS current samplOpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
) in get, file /home/user/opencv-3.4.0/apps/traincascade/imagestorage.cpp, line 158
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/user/opencv-3.4.0/apps/traincascade/imagestorage.cpp:158: error: (-5) Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
 in function get

我该如何解决这个问题:

  

samplOpenCV错误:错误的参数

非常感谢任何帮助。

编辑: 我已将 -numPos 修改为较小的数字: 2186 (0.9 * 2429),我在阅读this answer之后这样做了它让我

  

=====培训3阶段=====

它给了我同样的错误。我应该如何调整 opencv_createsamples 命令的参数?

1 个答案:

答案 0 :(得分:0)

我最终设法通过尊重formula

来使其发挥作用
  

vec-file&gt; =(numPos +(numStages-1)*(1-minHitRate)*(numPose)+ S)

numPose - 用于训练每个阶段的阳性样本数

numStages - 级联分类器在训练后将具有的阶段数

S - 来自vec文件的所有跳过样本的计数(适用于所有阶段)