Environment to prepare

The required environment is very simple as follows:

  • Operating system: Windows

  • Opencv [OpencV version is optional, but it is best to lower version, like Opencv3.4.1, because some of the files required behind the high version of OpencV is not its own, of course, surely look at this article has been good OpencV, so for has been the high version of OpencV comrades don’t worry, This article will tell you how to solve it.

  • Opencv_createsamples. Exe and opencv_traincascade. Exe

    Opencv_traincascade.exe, opencv_haarTrain.exe, opencv_haarTrain.exe, opencv_traincascade.exe. There are a lot of blogs that say that these two exe files come with OpencV, which is true, but they come with lower versions of OpencV, and not with higher versions of OpencV, which is why I recommended lower versions of OpencV. Finding these two files is really killing us, and they also require various dependencies, and many of the packages on the Internet also require certain points to download, which is really a headache. Well, without all the fuss, here’s a link to download these files :gitee.com/wsj-create/… .

    Of course, you can also check your Own OpencV have these two files, the file path is as follows:

Data preparation

First we need to create some folders to manage our data. The file structure is as follows:

A brief introduction to the three folders:

  • Neg: Store negative sample pictures [if we need face detection, that is, this folder stores all kinds of pictures except faces]
  • Pos: Store positive sample pictures [If we need face detection, that is, this folder stores pictures of various faces]
  • XML: stores the final program running structure file

Once these folders are set up we need to store the images we want to train in the NEG and POS folders. 【 note here that our pictures in pos, which is the picture of the sample, should try to keep same size images, such as are set to the size of the (20, 20). The size of a batch processing folder pictures program everywhere, here is a link: batch processing image size; and, the picture in the neg processing 】 don’t do

   

Data preprocessing ✨✨✨

Create a description file for positive samples

First we enter pos folder through Windows CDM as follows:

Then we enter the code in CMD: dir /b > pos.txt. This will generate a pos.txt file in pos folder, which records the names of all the images in this folder.

TXT file, remove the pos folder in the last line of pos.txt and replace all PNG files with PNG 1 0 0 20 20. Here 1 means the number of times the current image is repeated is 1, 0, 0, 20, 20 means the size of the target image is a rectangle from (0, 0) to (20, 20). The final pos. TXT file should have the following format.

       

Create a description file for negative samples

PNG > neg. TXT in CMD. A neg. TXT file will be generated in neg. The content in the neg. TXT file should have the following format. [Note: Ensure that the path here is an absolute path, otherwise there may be errors in later training]

   

Generate positive sample. Vec file

Note that generating.vec requires opencv_createsamples. Exe, which I gave out when preparing the environment, to be placed as follows: The yellow box is what we really need, some of the other two rely on, some may not, fortunately I put the files in OpencV, so many files will not be wrong.

First we go to the circle_detect directory, where pos and NEg folders are located, and enter the following commands:

opencv_createsamples.exe -vec pos.vec -info pos\pos.txt -bg neg\neg.txt -w 20 -h 20 -num 1500Copy the code

A brief introduction to instruction parameters:

-vec pos.vec: specifies the generated file. The generated file is pos.vec. -info pos\pos. TXT: target image description file in pos\pos.txt; -bg neg\ ng. TXT: background image description file in neg\ ng. TXT; -w 20: width of output sample, 20; -h 20: output sample height, 20; -num 1500: number of positive samples to be generated, 1500.

Finally, we will generate a pos.vec file in the circle_detect folder

   

Training model ✨✨✨

Again, we go to the circle_detect directory and enter the following command:

opencv_traincascade.exe -vec pos.vec -bg neg\neg.txt -data xml -w 20 -h 20 -mem 1024 -numPos 1000 -numNeg  3000 -nstages 2 -nsplits 5
Copy the code

A brief introduction to instruction parameters:

-vec pos.vec: positive sample file name; -bg neg\neg. TXT: background description file; -data XML: specifies the path name of the trained classifier, that is, the XML folder created previously. -w 20: sample image width, 20; -h 20: sample image height, 20; -mem 1024: Provides memory in MB. Obviously, the larger the value is, the more memory is provided and the faster the operation is. -numPos1000: Obtain 1000 positive samples. The value must be smaller than the number when preparing positive samples. Otherwise, can not get new positive samples will be reported. N/A numNeg3000: takes 3000 negative samples. N/A Nstages 2: Specifies the number of training layers. -nSplits the number of child nodes. The default value is 2.

The cascade.xml file will be generated in the XML directory after the training is successful. This file will be the final training file and we can test our model with code.

   

Model test

Directly on the code:

# import opencv - python
import cv2
The cascade. XML path obtained in the previous step, note that I changed the path here
path = 'D://cascade.xml'
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + path)

face_cascade.load(path)
# Read an image
img = cv2.imread('warped.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Faces is the face coordinate list, 1.3 is the amplification ratio, 5 is the number of repeated recognition
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3,minNeighbors=5)

for (x,y,w,h) in faces:
    # Draw the face frame, blue (BGR color system), brush width 2
    img = cv2.rectangle(img,(x,y),(x+w,y+h),(255.0.0),2)

# Display the renderings in the "IMg2" window
cv2.imshow('img2',img)
# Listen for any key on the keyboard, exit and close the window if there is a key, and save the image as output.jpg
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('output.png',img)

Copy the code

There are also a few things to be aware of when training your model, as you can see in my blog: Haar Test Considerations

Reference link: blog.csdn.net/hongbin_xu/…

If this article can help you, please go to 🛴🛴🛴

Swish swish duang give it a thumbs up