Train dataset for temp stage can not be filled (solution)
Yesterday, I tried to use opencv_traincascade to train a classifier, as shown on this website:
http://docs.opencv.org/doc/user_guide/ug_traincascade.html
But, I always encountered the same error as:
Problem:
l@DEL1207W118-Ubuntu $ opencv_traincascade -data data/ -vec signsPositive.vec -bg negSample.txt -numPos 10 -numNeg 200
PARAMETERS:
cascadeDirName: data/
vecFileName: signsPositive.vec
bgFileName: negSample.txt
numPos: 10
numNeg: 200
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC
Stage 0 is loaded
===== TRAINING 1-stage =====
<BEGIN
POS count : consumed 10 : 10
Train dataset for temp stage can not be filled. Branch training terminated.
Cascade classifier can't be trained. Check the used training parameters.
I took me the whole afternoon to find the solution by google, some other guys have the same problem like me as discussed in this topic: http://answers.opencv.org/question/16868/error-in-train-casacde/
Why?
The reason is very simple, in your opencv folder, find "opencv-2.4.6.1 -> apps -> traincascade ", open " imagestorage.cpp", in function "bool CvCascadeImageReader::NegReader::create( const String _filename, Size _winSize )", we can see that all the image file names in negInfo.txt are read into imgFilenames by std:getline. It seems right! But only if you have used ubuntu to generate the negInfo.txt. If you have used windows ( what I did) to generate this text file, there would be two chars at the end of each line \r\n; on linux there is just one, \n. So, with the original code in opencv, the \r is kept at the end of each file name. Then, in Line 60, "src = imread( imgFilenames[last++], 0 )", the image file can't be loaded.
So, there are two solutions:
1) generate your bt.txt (negative sample file) on Ubuntu, make sure that there is a newline for each image file no empty line in the whole file;
2) or generate your bt.txt (negative sample file) on Windows, but add one line in the original opencv code " imagestorage.cpp":
before line 45, add "
if (str[str.size() - 1] == '\r')
str.resize(str.size() - 1); // to erase \r if the file is generated by windows"; then, recompile (cmake .. -> make -> make install) your opencv for a new opencv_traincascade application.
Now, it works:
@DEL1207W118-Ubuntu $ opencv_traincascade -data data/ -vec signsPositive.vec -bg negSample.txt -numPos 10 -numNeg 200
PARAMETERS:
cascadeDirName: data/
vecFileName: signsPositive.vec
bgFileName: negSample.txt
numPos: 10
numNeg: 200
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC
Stage 0 is loaded
===== TRAINING 1-stage =====
<BEGIN
POS count : consumed 10 : 10
NEG count : acceptanceRatio 200 : 0.0311284
Precalculation time: 2
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 0|
+----+---------+---------+
END>
===== TRAINING 2-stage =====
<BEGIN
POS count : consumed 10 : 10
NEG count : acceptanceRatio 200 : 0.000914256
Precalculation time: 3
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 0.01|
+----+---------+---------+
END>
===== TRAINING 3-stage =====