-
Notifications
You must be signed in to change notification settings - Fork 0
Haar cascade training
thomas_sabbe edited this page May 20, 2021
·
5 revisions
how-to-set-up-haar-cascade-model -- coding: utf-8 --
<https://www.jetbrains.com/pycharm/>
-
<https://pypi.org/project/opencv-python/>
(this is the version to use functions in PyCharm) - For the download link underneath: You can place the folder that you've downloaded wherever you want, but ONLY use Version 3.X.X (not 4.X.X!!)
<https://sourceforge.net/projects/opencvlibrary/files/>
(this is the program to use the .exe programs illustrated below)
- Create two maps in the root directory of your pycharm project: 'pos' and 'neg'.
In the pos folder, you will need to put positive images of the object you want to detect. For example: a boat.
In the neg folder, you will need to put negative images. These do NOT contain the object you want to detect.
It is recommended to acquire more negative (twice as much) than positive images.
As for negative images, I recommend you check out
<http://image-net.org>
. At that site, you'll have a lot of images you can download to use as negative (or positive). For example: pictures of people do not contain boats! (If you wish to see how to efficiently acquire a lot of these images, please go to the link down below under 'sources'.)
-
Please use the script below in a new .py file in your project to generate a bg.txt file, which you will need later on. Make sure your negative images are stored in 'neg', in your root directory.
import os for file_type in ['neg']: for img in os.listdir(file_type): if file_type == 'neg': line = file_type + '/' + img + '\n' with open('bg.txt', 'a') as f: f.write(line)
-
If everything went fine, you should now have that file in your root directory.
- Make sure you have opened PyCharm, accesed the Terminal (below in the window) and are ready to enter a command! Now, annotations (bounding boxes) have to be drawn on the positive images. We'll use an OpenCV tool for this.
- Look for the folder you downloaded before from OpenCV 3.x.x . Copy the path.
- Generate annotations by typing this in the Terminal:
<%YOUR_PATH_TO_YOUR_OPENCV-FOLDER%\opencv\build\x64\vc15\bin\opencv_annotation.exe --annotations=pos.txt --images=pos/>
- Follow the instructions as seen in your terminal. Drag a box with your mouse, confirm the box by clicking on it (LMB), and then type 'c'. If there are multiple objects, repeat. Otherwise, click 'n' for the next image. Note that your progress will not be saved if you restart. You'll have to start over. Unless you remove the images you've already made annotations on in 'pos' folder and you move the previously generated 'pos.txt' file to another location!
- Make sure you have opened PyCharm, accesed the Terminal (below in the window) and are ready to enter a command!
- Now, open the pos.txt file and check if the slashes are backward '', and not forward ('/').
- Use the pos.txt file to generate a vector file, which can be used by the 'train_cascade' program.
<%YOUR_PATH_TO_YOUR_OPENCV-FOLDER%\opencv\build\x64\vc15\bin\opencv_createsamples.exe -info pos.txt -w 25 -h 25 -num 4030 -vec pos.vec>
- -info pos.txt: the filename you've generated under 4.
- -w -h: width and height of the bounding box to be generated. If you choose this higher than 25, the time to train the cascade model will increase significantly.
- -num 4030: Number of objects in total (not amount of images). Just put a high value in this position. It will be limited to the amount of objects you have annoted in '4.', so it doesn't really matter.
- -vec pos.vec: output .vec-file
- Make sure you have opened PyCharm, accesed the Terminal (below in the window) and are ready to enter a command!
- Create a folder 'cascade' in the root folder of your project.
- Finally, we will train our model. This is a really important step ofcourse so I'll try to document this thoroughly.
Enter the command below, after you've altered some parameters.
<%YOUR_PATH_TO_YOUR_OPENCV-FOLDER%\opencv\build\x64\vc15\bin\opencv_traincascade.exe -data cascade/ -vec pos.vec -precalcValBufSize 6000 -precalcIdxBufSize 6000 -bg bg.txt -w 25 -h 25 -numPos 1875 -numNeg 3750 -numStages 7>
- -data cascade/: output folder in your root folder where the cascade.xml should be generated.
- -vec pos.vec: explained earlier
- -precalcValBufSize 6000 -precalcIdxBufSize 6000: to accelerate the proces, you can allocate more ram to the process. Be careful though, do NOT let the sum these values be equal to the total amount of dedicated RAM you have!
- -bg bg.txt: explained earlier
- -w 25, -h 25: Has to be the same as in '5.'.
- -numPos 1875: Cannot be a higher number than the amount of vectors generated in pos.vec! It is recommended to use that number deducted by 30.
- -numNeg 3750: Recommended to have double the amount of negative images, as there are positive vectors! Can be equal but there may be a degraded result.
- -numStages 7: Start with a low number (5/6). This is the amount of stages given to train your model. The lower this number, the more undertrained your model will be (a lot of false detections). Choose this number too high, and you'll see that no object will be detected, and that the calculation time will take extremely long. My computer took 4 days to render 7 stages, with a bounding box size of 50 pixels... The advantage is that after every stage, your progress is saved. You can always continue on later on.
- Sources used:
<https://pythonprogramming.net/haar-cascade-object-detection-python-opencv-tutorial/>
(old but still useful for some things)