Unable to train my own Haar Cascade

asked 2017-11-07 15:36:54 -0500

ash.jo4444 gravatar image

updated 2017-11-07 18:46:43 -0500

I have been trying to train my own haar cascade referring to the following site https://pythonprogramming.net/haar-ca... since so many days but unable to get my object detected. Following are the different scenarios I tried

  1. I tried to single image of my watch
  2. stopped my watch (to keep in same position)
  3. tried different angles of the same watch (had 8images) - I used opencv_createsamples 8 different times on the same set of 1000 negative images to create 8000 positive images

      opencv_createsamples -img pos/1.jpg -bg bg.txt -info info/info.lst -pngoutput info -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 1000
    
      opencv_createsamples -img pos/2.jpg -bg bg.txt -info info/info.lst -pngoutput info -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 1000
    

    ......

      opencv_createsamples -info info/info.lst -num 8000 -w 20 -h 20 -vec positives.vec
    
      opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 7000 -numNeg 1000 -numStages 10 -w 20 -h 20
    

4.then tried a square box. 5. Tried positive images twice the number of negative images

      opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 1900 -numNeg 850 -numStages 10 -w 20 -h 20

6. Tried negative images twice the number of positive images

      opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 1000 -numNeg 2000 -numStages 10 -w 20 -h 20

My aim was to train 2-3 different objects separately and then identify them. Please suggest your opinion. for file_type in ['neg']:

for img in os.listdir(file_type):
    if file_type == 'pos':
        line = file_type+'/'+img+' 1 0 0 50 50\n'
        with open('info.dat','a') as f:
            f.write(line)
    elif file_type == 'neg':
        line = file_type+'/'+img+'\n'
        with open('bg.txt','a') as f:
            f.write(line)

opencv_createsamples -img pos/1.jpg -bg bg.txt -info info/info.lst -pngoutput info -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 1000

opencv_createsamples -info info/info.lst -num 1000 -w 20 -h 20 -vec positives.vec

opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 900 -numNeg 450 -numStages 10 -w 20 -h 20

Most of the times, my cascade is getting trained in just few minutes instead of few hours.

      ===== TRAINING 7-stage =====
      <BEGIN
      POS count : consumed   900 : 913
      NEG count : acceptanceRatio    1500 : 0.00139174
      Precalculation time: 10.819
      +----+---------+---------+
      |  N |    HR   |    FA   |
      +----+---------+---------+
      |   1|        1|        1|
      +----+---------+---------+
      |   2| 0.996667| 0.631333|
      +----+---------+---------+
      |   3| 0.996667| 0.599333|
      +----+---------+---------+
      |   4| 0.995556| 0.359333|
      +----+---------+---------+

HaarCascade #CascadeClassifier #Python #OpenCV #ImageProcessing #ImageRecognition #Computer Vision #ImageProcessing

edit retag flag offensive close merge delete