opencv_traincascade - samplOpenCV Error: Bad argument
opencv_traincascade - samplOpenCV Error: Bad argument
背景: 我正在尝试训练我自己的 OpenCV Haar 分类器进行人脸检测。我正在使用 Ubuntu 16.04 的虚拟机工作,我的工作目录有 2 个子目录:face 包含 2429 张正片图像,non-face包含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,然后我创建了一个空目录 data 和 运行以下:
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 Error: Bad argument
如有任何帮助,我们将不胜感激。
编辑:
我把-numPos改小了一点:2186 (0.9 * 2429),我读完this answer就做了我去
===== TRAINING 3-stage =====
它给了我同样的错误。我应该如何调整 opencv_createsamples 命令的参数?
我最终通过尊重这个 formula:
vec-file >= (numPos + (numStages-1) * (1-minHitRate) * (numPose) + S)
numPose - 用于训练每个阶段的正样本数
numStages - 级联分类器在训练后的阶段数
S - vec 文件中所有跳过样本的计数(对于所有阶段)
背景: 我正在尝试训练我自己的 OpenCV Haar 分类器进行人脸检测。我正在使用 Ubuntu 16.04 的虚拟机工作,我的工作目录有 2 个子目录:face 包含 2429 张正片图像,non-face包含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,然后我创建了一个空目录 data 和 运行以下:
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 Error: Bad argument
如有任何帮助,我们将不胜感激。
编辑: 我把-numPos改小了一点:2186 (0.9 * 2429),我读完this answer就做了我去
===== TRAINING 3-stage =====
它给了我同样的错误。我应该如何调整 opencv_createsamples 命令的参数?
我最终通过尊重这个 formula:
vec-file >= (numPos + (numStages-1) * (1-minHitRate) * (numPose) + S)
numPose - 用于训练每个阶段的正样本数
numStages - 级联分类器在训练后的阶段数
S - vec 文件中所有跳过样本的计数(对于所有阶段)