title: opencv_traincascade - samplOpenCV Error: Bad argument
body: <p><strong>Background:</strong> I am trying to train my own OpenCV Haar Classifier for face detection. I am working on a VM with Ubuntu 16.04, my working directory has 2 sub-directories: <strong>face</strong> containing 2429 images of positives, <strong>non-face</strong> containing 4548 images of negatives. All images are png, gray scale and have both width and height 19 pixels. I have generated a file <strong>positives.info</strong> that contains the absolute path to every positive image followed by " 1 0 0 18 18", like so:</p>
  
  <pre><code>/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
  </code></pre>
  
  <p>and another file <strong>negatives.txt</strong> that contains the absolute path to every positive image</p>
  
  <pre><code>/home/user/ML-Trainer/non-face/other1.png
  /home/user/ML-Trainer/non-face/other2.png
  /home/user/ML-Trainer/non-face/other3.png
  </code></pre>
  
  <p>First I ran the following command:</p>
  
  <pre><code>opencv_createsamples -info positives.info -vec positives.vec -num 2429 -w 19 -h 19
  </code></pre>
  
  <p>and I get the <strong>positives.vec</strong> as expected, I then created a empty directory <strong>data</strong> and ran the following:</p>
  
  <pre><code>opencv_traincascade -data data -vec positives.vec -bg negatives.txt -numPos 2429 -numNeg 4548 -numStages 10 -w 19 -h 19 &amp;
  </code></pre>
  
  <p>It seems to run smoothly:</p>
  
  <pre><code>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 =====
  &lt;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&gt;
  Training until now has taken 0 days 0 hours 1 minutes 7 seconds.
  </code></pre>
  
  <p>But then I get the following error:</p>
  
  <pre><code>===== TRAINING 1-stage =====
  &lt;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
  </code></pre>
  
  <p>How do I solve this: </p>
  
  <blockquote>
    <p>samplOpenCV Error: Bad argument</p>
  </blockquote>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>EDIT:
  I have modified <strong>-numPos</strong> to a smaller number: <strong>2186</strong> (0.9 * 2429), I did this after reading <a href="http://answers.opencv.org/question/776/error-in-parameter-of-traincascade/" rel="nofollow noreferrer">this answer</a> and it got me to</p>
  
  <blockquote>
    <p>===== TRAINING 3-stage =====</p>
  </blockquote>
  
  <p>and it gives me the same error. How should I tune the parameters for the <strong>opencv_createsamples</strong> command?</p>