How to train a model with OPENCV

             The model is trained using the OPENCV cascade classifier. First of all, we must have two executable files and dependencies, opencv_createsamples.exe and opencv_traincascade.exe. You can chat with me privately if you want.

        Step 1: Create a folder and put the positive sample (the picture you want to recognize) into a folder. Here we name this folder 1.

         Here I randomly found an object as the recognition object. The names of the photos I took at the beginning were messy. Here we need to rename them so that they can be used in the program. (Note: The number of positive samples here must be large enough, so that the recognition rate will increase. Here, only 8 positive samples are used for convenience)

        Step 2: We are going to convert these images into grayscale images.

import cv2
imagepath = "C:/Users/huawei/Desktop/1/"
imagepath1 = "C:/Users/huawei/Desktop/pos/"
#转化灰度图和重置大小函数
for i in range(1,9):
    image = cv2.imread(imagepath+'1 ('+str(i)+')'+'.jpg',1)
    gray = cv2.cvtColor(image,code = cv2.COLOR_BGR2GRAY)
    gray1 = cv2.resize(gray,dsize = (50,50))
    cv2.imwrite(imagepath1+str(i)+'.jpg',gray1)

        Here, the path of imagepath is the path of the positive sample image without processing, and the path of imagepath1 is the path to be saved after processing such as grayscale. The result is as shown below.

 

        Step 3: Here is a negative sample processed image, link: https://pan.baidu.com/s/1omQyLDSn9Buo2jeG5SEJxg
Extraction code: acno

        Step 4: Create a description file for positive samples.

import os
PATH = "C:/Users/huawei/Desktop/pos"
dirs = os.listdir(PATH)
for i in dirs:
    print(i)
    line1 = PATH+'/'+i+' 1 0 0 50 50\n'
    with open('C:/Users/huawei/Desktop/info.txt','a') as f:
        f.write(line1)
        print(f)

        The description file of the positive sample is placed in the info.txt file, where 1 0 0 50 50 means 1 represents the category, and 0 0 50 50 is the size format of the positive sample after you process it.

 

        Step 5: Create a description file for negative samples.

#描述负文本
import os
PATH = "D:/OPENCVtrain/neg"
dirs = os.listdir(PATH)
for i in dirs:
    print(i)
    line1 = PATH+'/'+i+'\n'
    with open('C:/Users/huawei/Desktop/bg.txt','a') as f:
        f.write(line1)

        The description file of the negative sample is saved in the bg.txt file, and some screenshots are as follows.

        

         Step 6: Put the positive sample description file (info.txt) and negative sample description file (bg.txt) into the folder containing opencv_createsamples.exe and opencv_traincascade.exe, and create a data folder.

        Step 7: In the cmd command window, enter the file containing the two executable files in step 6, and enter opencv_createsamples -info info.txt -num 8 -w 50 -h 50 -vec positives.vec, which will create the following document. This is the file we need to use when training the model.

         Step 8: After the seventh step is executed, enter the opencv_traincascade.exe -data data -vec positives.vec -bg bg.txt -numPos 8 -numNeg 80 -numStages 10 -w 50 -h 50 command. Training will begin.

        There are many parameters in these two commands. The meaning of the parameters is summarized below.

General parameters:

-data <cascade_dir_name> : The directory is used to save the classifier xml files and intermediate files generated by training (for the above LBP_classifier), if there is no training program, it will be created;

-vec <vec_file_name>: The vec file name containing positive samples generated by the opencv_createsamples program (corresponding to pos_24_24.vec above);

-bg <background_file_name>: Background description file, that is, the description file containing the negative sample file name (corresponding to neg\neg.txt above);

-numPos <number_of_positive_samples>: The number of positive samples used for each classifier training (default value is 2000);

-numNeg <number_of_negative_samples>: The number of negative samples used for each classifier training, which can be greater than the number of pictures specified by -bg (the default value is 1000);

-numStages <number_of_stages>: The number of stages of the trained classifier (default is 20);

-precalcValBufSize <precalculated_vals_buffer_size_in_Mb>: cache size, used to store precalculated feature values ​​(feature values), the unit is MB (the default value is 256);

-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb>: cache size, used to store precalculated feature indices, in MB (default value is 256);

The larger the memory, the shorter the training time.

-baseFormatSave: This parameter is only valid when using Haar features. If this parameter is specified, the cascade classifier will be stored in the old format (the parameter item is not specified by default, and its value is false at this time; once specified, its value defaults to true);

Cascade parameters: CvCascadeParams class, defined in cascadeclassifier.h

-stageType <BOOST(default)>: Level (stage) parameter. Currently, only BOOST classifiers are supported as cascaded types;

-featureType<{HAAR(default), LBP}>: The type of feature: HAAR - Haar-like feature; LBP - local texture pattern feature (default Harr);

-w <sampleWidth>: the width of the training sample (in pixels, default 24);

-h <sampleHeight>: the height of the training sample (in pixels, default 24);

The size of the training sample must be consistent with the size of the training sample created (created using the opencv_createsamples program).

Boosted classifier parameters: CvCascadeBoostParams class, defined in boost.h

-bt <{DAB, RAB, LB, GAB(default)}> : type of Boosted classifier (DAB - Discrete AdaBoost, RAB - Real AdaBoost, LB - LogitBoost, GAB - Gentle AdaBoost are default);

-minHitRate <min_hit_rate> : The minimum detection rate desired for each stage of the classifier (the default value is 0.995), and the total detection rate is about min_hit_rate^number_of_stages;

-maxFalseAlarmRate <max_false_alarm_rate> : The maximum false detection rate expected for each level of the classifier (the default value is 0.5), and the total false detection rate is about max_false_alarm_rate^number_of_stages;

-weightTrimRate <weight_trim_rate> : Specifies whether trimming should be used and its weight, a good value is 0.95;

-maxDepth <max_depth_of_weak_tree> : The maximum depth of the weak classifier tree. A good value is 1 , which are binary trees (stumps);

-maxWeakCount <max_weak_tree_count> : Maximum number of weak classifiers in each level (default 100). The boosted classifier (stage) will have so many weak trees (<=maxWeakCount), as needed to achieve the given -maxFalseAlarmRate;

        Step 9: After the training is completed, the cascade file will be generated in the data file (this is the folder created in the sixth step), which is the file we need.

         Step 10: Write a program to recognize objects.

#识别图片
import cv2
import numpy as np

image = cv2.imread("C:/Users/huawei/Desktop/1/1 (1).jpg",1)
gary = cv2.cvtColor(image,code = cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('D:/OPENCVtrain/opencv_bin/data/cascade.xml')
box = face_cascade.detectMultiScale(gary,scaleFactor=1.1,minNeighbors=7,minSize=[100,100])
for x,y,w,h in box:
    img1 = cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('img',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

        For convenience, we read the unprocessed positive sample image for testing.

cv2.CascadeClassifier('D:/OPENCVtrain/opencv_bin/data/cascade.xml')
#这里的路径就是保存cascade.xml文件的路径

        In this way, we have completed the training, because we only use 8 positive samples here, so the recognition effect is not very good. This is just a general process described here. If you want to have a good recognition effect, try to use as many positive sample pictures as possible.

        This is the first time I wrote a blog, if there is something wrong, please give me some pointers.

Guess you like

Origin blog.csdn.net/weixin_54963942/article/details/128305325