Here are what I did for training face recognition using OpenCV.
With these steps, I learned how to run opencv_createsamples and opencv_traincascade,
and also how hard it is to train the computer to recognize something.
1. The very beginning
I was learning OpenCV, and wanted to train something by my own hand.
There were hundreds of selfie photos of myself taken by this way,
so I made up my mind to train my computer to recognize my face.
2. Refine training samples
Knowing not all my photos were usable for the training, I wanted to filter out unusable ones.
For the refinement, I ran following python script:
This script detects faces from photos in PHOTOS_DIR with pre-trained cascade file,
draws red rectangles on them, and saves the result photos in CHECKED_PHOTOS_DIR.
All the result photos with no rectangles, or with rectangles on wrong position would not be a good sample for the training,
so I deleted them from PHOTOS_DIR.
3. Generate positive/negative list files
I downloaded negative facial images from here and placed them in NEGATIVE_PHOTOS_DIR.
(I converted them into .jpg format!)
With the negative images, following script generates positive.txt and negative.txt:
positive.txt is filled up with lines which consist of positive image’s filepath and facial positions.
negative.txt has negative images’ filepaths in it.
4. Create samples
I ran following command with generated positive.txt:
and got training.vec as a result.
5. Train
Finally, with generated training.vec and negative.txt, I ran:
(Parameters may vary.)
Fortunately, there were no errors while running it.
I could find the final result: cascade.xml in result directory.
6. Verify the result
I got cascade.xml, and wanted to verify it if the training was successful.
I slightly modified the first python script:
This script now draws rectangles on faces recognized by the newly-generated cascade file(result/cascade.xml).
Time to check the marked photos in CHECKED_PHOTOS_DIR!
8. The result is…
Some of the photos had red rectangles on correct positions,
but others did not:
The result was poorer than I expected.
Maybe the positive/negative photos were not perfect for this training, or the train parameters were not good enough.
9. Wrap-up
Now I can create samples and train OpenCV to recognize something, but the accuracy is not satisfying yet.