OpenCV Development Notes (71): Fatty Red takes you in-depth cascade classifier training in 8 minutes

If the article is an original article, please indicate the source of the original article for reprinting.
The blog address of this article: https://blog.csdn.net/qq21497936/article/details/109096211
Readers, the knowledge is endless but the manpower is poor, either change the demand or Find professionals, or study the blog post of Fatty Red (Red Imitation) by yourself
: development technology collection (including Qt practical technology, Raspberry Pi, 3D, OpenCV, OpenGL, ffmpeg, OSG, single-chip microcomputer, software and hardware combination, etc.) continue to update In... (click on the portal)

OpenCV development column (click on the portal)

Previous article: " OpenCV Development Notes (70): Fatty Red takes you foolish compilation of VS2017x64 version of openCV4 "
Next article: " OpenCV Development Notes (72): Fatty Red takes you to use opencv+dnn+ in 8 minutes tensorFlow recognizes objects "


Preface

  Fat red, come too!
  When doing image processing, what is often a headache is that it is clearly separated (non-color) and divided into several areas. How do we know which of these areas is what we need? Then this part involves the need to identify.
  Recognition can write template matching, feature point recognition, and cascade classifier training and recognition.
  This article explains the training and recognition of cascaded classifiers.


Cascade classifier related

  There are two cascade classifiers of OpenCV, namely Harr cascade classifier and LBP cascade classifier. For specific cascade classifiers, please refer to:
  " OpenCV Development Notes (54): Fat Red 8 minutes to take you in-depth understanding of Haar cascade classifier for face detection (with pictures and texts + easy to understand + program source code) "
  " OpenCV development notes (Fifty-five): Fatty Red will take you in 8 minutes to understand Haar, LBP features and the recognition process of cascade classifiers (with pictures and texts + easy to understand + program source code)


clear goal

  The goal is to identify the singer in the video, we first collect the data collection manually.
  The video is "Green", as shown below:
Insert picture description here


Preparation before training the classifier

Collect positive sample pictures

  The size of the positive sample does not have to be the same, but it should have the same ratio as the width and height in the generated positive sample vector file (because during the training process, it will be scaled according to the set width and height, for example, set the positive version image to 128x128 Yes, the sample will be scaled at 256x256. If it is 256x128, the ratio will be different. How to deal with this picture? To be determined); The
  positive sample picture should contain as little interference background information as possible. In the training process, the redundant background information will also become a local feature of the positive sample. Unlike deep learning, deep learning is now the mainstream brainless deep learning, and some basic image denoising may not be done.
  The data sources should be as diverse as possible. For example, the sample is a car, and the posture scene of the car should be slightly richer. Too many images of the same positive sample target will make the local features too obvious, causing the training of this target to overfit, affecting the detection accuracy, and not conducive to the generalization of the trainer.
  We collect the face of the video, first decode the video and save it as a jpg picture.
  Ten thousand words are omitted here...


Create sample: opencv_createsamples.exe

  Use opencv's own command line tool opencv_createsamples.exe

  • [-info <collection_file_name>]
    Sample description file, the content of each line is
xxx.jpg nums x y width height

  Example: There are two goals in the picture

xxx.jpg 2 0 0 100 100 200 200 100 100

  Generate samples rely on the command line on windows

dir /b > pos.data
  • [-img <image_file_name>]
      By distorting a picture into multiple pictures as a sample, just fill in this parameter. The content of the parameter is the path of the picture to be distorted. After filling in, the -info parameter is no longer valid.
  • [-vec <vec_file_name>]
      The name and path of the sample description file
  • [-bg <background_file_name>]
      The name and path of the negative sample description file. If omitted, the value of bgcolor will be used as the background. It is the path of the description file that is the same as the directory where the negative sample picture (background picture) is stored. It can be saved in txt, dat and other formats. The content of each line is: xxx.jpg.
  • [-inv]
      If this flag is specified, the color of the foreground image will be inverted
  • [-randinv]
      If this logo is specified, the color will be randomly flipped
  • [-num <number_of_samples = 1000>] How
      many samples in total, which can be omitted, will be generated according to the actual number of samples entered
  • [-bgcolor <background_color = 0>]
      Background color (currently grayscale); background color represents transparent color. Because image compression can cause color deviation, the color tolerance can be specified by -bgthresh. All pixels between bgcolor-bgthresh and bgcolor+bgthresh are set as transparent pixels.
  • [-bgthresh <background_color_threshold = 80>]
      (参考-bgcolor)
  • [-maxidev <max_intensity_deviation = 40>]
      The maximum value of the intensity gradient of pixels in the foreground sample
  • [-maxxangle <max_x_rotation_angle = 1.100000>] The
      maximum rotation angle of the x-axis, in radians
  • [-maxyangle <max_y_rotation_angle = 1.100000>] The
      maximum rotation angle of the y-axis, in radian
  • [-maxzangle <max_z_rotation_angle = 0.500000>] The
      maximum rotation angle of the z-axis, in radians
  • [-show [<scale = 4.000000>]]
      Display samples, as a debugging when creating samples
  • [-w <sample_width = 24>]
      The size to which the sample is scaled
  • [-h <sample_height = 24>]
      The size to which the sample is scaled
  • [-maxscale ]:
  • [-rngseed ]:

Create a positive sample

Insert picture description here
Insert picture description here

Create a negative sample

Insert picture description here
Insert picture description here

Create sample file vec

opencv_createsamples -vec pos.vec -info pos/pos.txt -bg neg/neg.txt -show -w 50 -h 70  、
                     -maxxangle 6.28 -maxyangle 6.28 -maxzangle 6.28

(Note: LBP features 50x70, etc. can be trained, the actual HAAR must be 24x24 or 20x20)
Insert picture description here
Insert picture description here


Training sample opencv_traincascade.exe

  Using opencv's own command line tool opencv_traincascade.exe, you can train three characteristic targets: HAAR, HOG, and LBP.

  • -data <cascade_dir_name>
      The storage directory of the trained classifier.
  • -vec <vec_file_name>
      The vec file generated by opencv_createsamples, the data of the positive sample.
  • -bg <background_file_name> The
      negative sample description file, which mainly contains the directory where the negative sample file is located and the negative sample file name.
  • [-numPos <number_of_positive_samples = 2000>] The
      number of positive samples used in the training of each classifier, but it should be noted that this value must be less than the number of positive samples.
  • [-numNeg <number_of_negative_samples = 1000>] The
      number of negative samples used in the training of each classifier can be greater than the number of pictures specified by -bg.
  • [-numStages <number_of_stages = 20>]
      The number of stages of training classifiers, the number of strong classifiers
  • [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = 1024>]
      Buffer size, used to store pre-calculated feature values, in MB
  • [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = 1024>]
      Buffer size, used to store pre-calculated feature index, unit MB
  • [-baseFormatSave]
      is only valid when using Haar features. If specified, the cascade classifier will be stored in the old format.
  • [-numThreads <max_number_of_threads = 1>]
      Support multi-threaded parallel training
  • [-acceptanceRatioBreakValue = -1>]
      This parameter is used to determine how accurately the model should keep learning and when to stop. A good guideline is to train no more than 10e-5 (equivalent to 10*10^(-5)) to ensure that the model does not overtrain your training data. By default, this value is set to -1 to disable this feature.

-cascadeParams-

  • [-stageType <BOOST(default)>]
      Cascade type, currently only BOOST
  • [-featureType <{HAAR(default), LBP, HOG}>]
      The feature type used for training, currently supported features are Haar, LBP and HOG
  • [-w <sampleWidth = 24>]
      The width of the training positive sample, the w and h of the Haar feature are generally 20, the w and h of the LBP feature are generally 24, and the w and h of the HOG feature are generally 64
  • [-h <sampleHeight = 24>]
      The height of the positive training sample

–BoostParams–

  • [-bt <{DAB, RAB, LB, GAB(default)}>]
      Enhanced classifier types: DAB-Discrete AdaBoost, RAB-Real AdaBoost, LB-LogitBoost, GAB-Gentle AdaBoost.
  • [-minHitRate <min_hit_rate> = 0.995>]
      The minimum expected hit rate of each stage of the classifier. The overall hit rate is estimated as (minimum hit rate^ number of stages).
  • [-maxFalseAlarmRate <max_false_alarm_rate = 0.5>]
      The maximum expected false alarm rate of each stage of the classifier.
  • [-weightTrimRate <weight_trim_rate = 0.95>]
      specifies whether trimming and its weight should be used. A good choice is 0.95.
  • [-maxDepth <max_depth_of_weak_tree = 1>]
      The maximum depth of the weak tree. A good choice is 1, which is the case of tree stumps.
  • [-maxWeakCount <max_weak_tree_count = 100>]
      The maximum number of weak trees in each cascade stage. The boosted classifier (stage) will have many weak trees (<=maxWeakCount) to achieve the given -maxFalseAllRate.

–HerFeatureParams–

  • [-mode <BASIC(default) | CORE | ALL>]
      Select the type of Haar feature set used in training. Basically, only upright features are used, and all features use a full set of upright and 45-degree rotation features.

Train a cascade classifier

opencv_traincascade.exe -data data -vec pos.vec -bg neg\neg.txt \
-numPos 41 -numNeg 215 -numStages 16 -featureType HAAR -w 64 -h 64

The training errors are as follows:
Insert picture description here

Change the LBP feature and continue training:

opencv_traincascade.exe -data data -vec pos.vec -bg neg\neg.txt \
-numPos 41 -numNeg 215 -numStages 16 -featureType LBP -w 64 -h 64

The training error is as follows:
Insert picture description here
  According to the error width and height, it should be the same as the created sample, change to 50x70 (the created sample is 50x70), and continue training:

opencv_traincascade.exe -data data -vec pos.vec -bg neg\neg.txt  \
-numPos 41 -numNeg 215 -numStages 16 -featureType LBP -w 50 -h 70

  The negative sample generates an absolute path again:

dir /b /s >negAb.txt

Insert picture description here
  Delete the non-picture lines
  and continue training:

opencv_traincascade.exe -data data -vec pos.vec -bg neg\negAb.txt \
-numPos 41 -numNeg 215 -numStages 16 -featureType LBP -w 50 -h 70

Training error, as follows:
Insert picture description here
manually create data directory
Insert picture description here
  to continue training:

opencv_traincascade.exe -data data -vec pos.vec -bg neg\negAb.txt -numPos 41 -numNeg 215 -numStages 16 -featureType LBP -w 50 -h 70

  Then, normal training, the training is completed:
Insert picture description here

How to train Haar

  To train the haar feature cascade classifier, the initial creation sample must be 24x24 or 20x20 (after many attempts to demonstrate), the final setting of 24x24 training is as follows:

opencv_traincascade.exe -data data -vec pos.vec -bg neg\negAb.txt \
-numPos 41 -numNeg 215 -numStages 16 -featureType HAAR -w 24 -h 24

Insert picture description here
Insert picture description here
  To use the cascade classifier, use cascade.xml to load it.


Haar cascade classifier test

  For the test of the cascade classifier, please refer to " OpenCV Development Notes (Fifty-Four): Fat Red 8 minutes to take you in-depth understanding of the Haar cascade classifier for face detection (with pictures and texts + easy to understand + program source code) "


Previous article: " OpenCV Development Notes (70): Fatty Red takes you foolish compilation of VS2017x64 version of openCV4 "
Next article: " OpenCV Development Notes (72): Fatty Red takes you to use opencv+dnn+ in 8 minutes tensorFlow recognizes objects "

Guess you like

Origin blog.csdn.net/qq21497936/article/details/109096211