Created
July 24, 2017 21:32
-
-
Save skanga/31a3cea35aebbec083d97f503eb00111 to your computer and use it in GitHub Desktop.
Train HAAR cascades with OpenCV 3.2 on windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
REM SETUP | |
REM Download opencv-3.2.0-vc14.exe from https://github.com/opencv/opencv/releases | |
REM Extract it and then copy all executables from ..\opencv-3.2.0-vc14\build\x64\vc14\bin\ into .\bin | |
REM Place BMP positive images into .\positives and JPG negative images into .\negatives and then run this script | |
REM REFERENCES: | |
REM https://www.academia.edu/9149928/A_complete_guide_to_train_a_cascade_classifier_filter | |
REM http://johnallen.github.io/opencv-object-detection-tutorial/ | |
REM http://www.technolabsz.com/2011/08/how-to-do-opencv-haar-training.html | |
REM http://note.sonots.com/SciSoftware/haartraining.html | |
REM Set the image height and width as needed | |
set IMWIDTH=24 | |
set IMHEIGHT=24 | |
REM Negative samples must not contain objects you need to detect. Use JPG format | |
set NEG=negatives | |
REM Positive samples must be in BMP format | |
set POS=positives | |
set CASCADES=cascades | |
for /f %%A in ('dir %NEG%\*.jpg ^| find "File(s)"') do set NEGCNT=%%A | |
for /f %%A in ('dir %POS%\*.bmp ^| find "File(s)"') do set POSCNT=%%A | |
echo negative image count = %NEGCNT% | |
echo positive image count = %POSCNT% | |
REM For some strange reason opencv_createsamples and opencv_traincascade want the bg files in differing formats (absolute/relative path), so we just create both. | |
dir /b %NEG%\*.jpg > %NEG%\bg1.txt | |
del /f /q %NEG%\bg2.txt | |
for %%a in ("%NEG%\*.jpg") do echo %%~fa >> %NEG%\bg2.txt | |
REM Marking images in order to make creation of info files quick and easy | |
bin\opencv_annotation --images=%POS% --annotations=info.txt | |
REM Two clicks on each corner and then c to turn it green and confirm it is stored or d to delete the last annotation (for errors)and n to continue to the next image | |
REM Use the opencv createsamples utility to create a vec file with 10K positive samples of given width/height | |
bin\opencv_createsamples -info info.txt -vec vector.vec -num 100000 -bg %NEG%/bg1.txt -w %IMWIDTH% -h %IMHEIGHT% | |
REM OPTIONAL - check out the vec file | |
REM bin\opencv_createsamples -vec vector.vec -w %IMWIDTH% -h %IMHEIGHT% | |
REM Empty out the cascade folder | |
IF EXIST %CASCADES% ( | |
rmdir %CASCADES% /s /q | |
) | |
REM Create a new cascade folder | |
mkdir %CASCADES% | |
REM Train the classifier cascade from the training set and convert the cascade into XML format | |
bin\opencv_traincascade -data %CASCADES% -vec vector.vec -bg %NEG%/bg2.txt -numPos %POSCNT% -numNeg %NEGCNT% -w %IMWIDTH% -h %IMHEIGHT% -numStages 15 -featureType LBP -precalcValBufSize 4048 -precalcIdxBufSize 4048 -numThreads 24 | |
copy %CASCADES%\cascade.xml . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment