2000/2500 Image Haarcascade

My last blog in this series was over a year ago, and in it I said that I was going to delve into the computations necessary to find out what values to use in creating a haarcascade file, given the number of positive and negative samples used. Originally I was thinking of using the mathematical relations given on Jeremy Kun's webpage, Weak Learning, Boosting, and the AdaBoost algorithm. However, I came across the relationships between sample numbers, positive image numbers, negative image numbers, etc. on the opencv website; Traincascade Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file. I found out that Maria Dimashova had written a "formula" for this relationship, but some people asked her to elaborate too make her "formula" more easily understood. From the commenters, I got that using 90% of the positive samples from the vector file (created by using opencv_createsamples) is pretty safe (too avoid the error message that the original poster on answers.opencv.org got; i.e., was posting about). However, another commenter recommended 80%. I created the file used to create the video on this web page with the following command:

opencv_traincascade -data cascades -vec vector/facevector.vec -bg bg.txt -numstages 11 -minHitRate .99 -maxFalseAlarmRate 0.31 -numPos 2000 -numNeg 500 -w 25 -h 25 -bt GAB -mode ALL -precalcValBufSize 768 -precalcIdxBufSize 768

I used 2000 pos images in my last blog, but this time I downloaded 500 more smile images for a total 2500 smile samples. However, after reading that webpage on opencv.org, I went with 80% of the number of positive images in my vec file; 2500 positive image samples. I only got a total of 10 stages before the program terminated with a max error message. I could've tried a lower error number too get another stage, but I thought I' stop here and check out what kind of smile captures I would get; about the same as my last 2000 image haarcascade file.

Visitors who have looked at the other blogs in this series, will notice that I'm no longer using an old Windows implementation for creating haarcascade files; this is the Linux (or Unix, or BSD) version for creating haarcascades, opencv_traincascade. I could've continued using the out of date Windows programs I had downloaded from https://www.cs.auckland.ac.nz/~m.rezaei/Tutorials/Creating_a_Cascade_of_Haar-Like_Classifiers_Step_by_Step.pdf but I'd planned on upgrading my technique to see if it made a difference or not, and an event occurred that hastened my decision. Serval, my main system stopped working. Serval was running Windows 7, and I downloaded that old haarcascade package from New Zealnad too use on my old Windows system. One day, Serval wouldn't boot. I got the same error message when I tried booting off of a live cd with Puppy Linux on it. I tried Debian, I tried everything on usb flash drives, too. So the problem isn't a corrupt disk (could be a problem in BIOS), or a corrupt OS (though what I did didn't rule out that those could be comorbid conditions); maybe bad memory or a problem with the mother board (or some component on it; e.g., a cpu). Rather then try to further debug Serval, I used this turn of events as an opportunity too make my Raspberry Pi 3 Model B my main system. I'm writing this webpage with the Bluefish editor that came with the copy of Raspbian Stretch on my Raspberry Pi.

Was I able to use my Raspberry Pi to create a haarcascade file? The short answer is no. I already had Python (came preloaded on Raspbian), but I still had to add several packages, which I did by following the instructions found at https://pythonprogramming.net/haar-cascade-object-detection-python-opencv-tutorial/. Unfortunately, running opencv_hartraining dies with an insufficient memory error; not too shocking. Raspberry Pis only have 1 GByte of memory (wasn't it only yesterday that you had to buy an IBM 3090 mainframe too get that much memory?) so it's not shocking that they are unable to run something as memory intensive as creating a large haarcascade file. On the bright side, I was able to create a vector file using opencv_createsamples, and got the syntax down for using opencv_traincascade. ...and, I found out that I could no longer write out the processed video with fourcc = cv2.VideoWriter_fourcc(*'XVID'); had to change it to fourcc = cv2.cv.CV_FOURCC(*'XVID'). Take a look at video_streamer16RPiModel3B.py

I do have a system with enough memory for haarcascade training; a MS Surface Pro II, with 8 GBytes of memory and a 256 GByte SSD card. The i5 processor (on my Surface Pro) has considerably more punch then the armv71 on my RPi, too. I had already installed my favorite version of Python (Anaconda 3.7), which doesn't require a bunch of packages you'd normally have to add to a plain jane installation of Python; all I had to add was OpenCV (and some opencv for python routines), but I decided to stick with a Linux environment (mostly because I'd become so used to running Raspbian). I downloaded VirtualBox, and installed Debian 9.6 (Stretch) on it; gave it 3 GBytes of memory. I installed the OpenCV environment the same way I did on my RPi, and ran the same opencv_traincascade command that failed on my RPi; created a haarcascade XML file in just one day.

My results with this haarcascade file aren't identical to my last file. I found a few smiles the last file was unable to, but missed a few smiles the last file found; roughly parity. I was hoping for an improvement, but it's not too shocking that a 2000 image haarcascade file is only as good as another 2000 image haarcascade file built for the same purpose. The opencv_traincascade routine has three parameters that I let default; -maxxangle {max_x_rotation_angle}, -maxyangle {max_y_rotation_angle}, -maxzangle {max_z_rotation_angle}. These tree parameters would take into account the head gyration motions of the models in the smile video I've been using. Some of the mouth captures (shown in the video as a red rectangle drawn around the mouth) miss the the outer corners of the mouth (turned up for a smile). I thought about increasing the area around the mouth capture by a little (maybe 10%). In my next haarcascade file I'll try using the max angle parameters (one thing at a time. If I increased the area around the "mouth" at the same time, I wouldn't know which change did the trick - assuming there was an improvement). After finding out if I can improve the results of my 2000 image file, it's on to 2500 positive images.

Return To My Blog Page       Haarcascade Smiles File       Return To My Programming Page