通过opencv训练自己的人脸分类器

来源:互联网 发布:海陆丰制毒 知乎 编辑:程序博客网 时间:2024/06/11 16:49

1.首先得在此处下载win pack版的opencv

2.正在此目录(opencv\build\x64\vc14\bin)下找到 opencv_createsamples.exe 和 opencv_traincascade.exe

3.在正样本的文件夹下创建记事本,写入如下内容:

dir /b/s/p/w *.jpg > positives.txt
最后修改为 bat 格式

步骤:

问题:

打开cmd,输入 opencv_createsamples.exe 就会得到帮助信息
Usage: opencv_createsamples.exe
  [-info <collection_file_name>]
  [-img <image_file_name>]
  [-vec <vec_file_name>]
  [-bg <background_file_name>]
  [-num <number_of_samples = 1000>]
  [-bgcolor <background_color = 0>]
  [-inv] [-randinv] [-bgthresh <background_color_threshold = 80>]
  [-maxidev <max_intensity_deviation = 40>]
  [-maxxangle <max_x_rotation_angle = 1.100000>]
  [-maxyangle <max_y_rotation_angle = 1.100000>]
  [-maxzangle <max_z_rotation_angle = 0.500000>]
  [-show [<scale = 4.000000>]]
  [-w <sample_width = 24>]
  [-h <sample_height = 24>]
  [-maxscale <max sample scale = -1.000000>]


打开cmd,输入 opencv_traincascade.exe 就会得到帮助信息
Usage: opencv_traincascade.exe
  -data <cascade_dir_name>
  -vec <vec_file_name>
  -bg <background_file_name>
  [-numPos <number_of_positive_samples = 2000>]
  [-numNeg <number_of_negative_samples = 1000>]
  [-numStages <number_of_stages = 20>]
  [-precalcValBufSize <precalculated_vals_buffer_size_in_Mb = 1024>]
  [-precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb = 1024>]
  [-baseFormatSave]
  [-numThreads <max_number_of_threads = 5>]
  [-acceptanceRatioBreakValue <value> = -1>]
--cascadeParams--
  [-stageType <BOOST(default)>]
  [-featureType <{HAAR(default), LBP, HOG}>]
  [-w <sampleWidth = 24>]
  [-h <sampleHeight = 24>]
--boostParams--
  [-bt <{DAB, RAB, LB, GAB(default)}>]
  [-minHitRate <min_hit_rate> = 0.995>]
  [-maxFalseAlarmRate <max_false_alarm_rate = 0.5>]
  [-weightTrimRate <weight_trim_rate = 0.95>]
  [-maxDepth <max_depth_of_weak_tree = 1>]
  [-maxWeakCount <max_weak_tree_count = 100>]
--haarFeatureParams--
  [-mode <BASIC(default) | CORE | ALL
--lbpFeatureParams--
--HOGFeatureParams--


(问题1)positives.txt(1) : parse errorDone. Created 0 samples
说明你的 正样本 路径写错了 
正确格式:positive_images\1.jpg 1 0 0 20 20(路径+1+0+0+w+h)

(问题2)注意:负样本直接写成路径即可,否则会出现后面错误:
Train dataset for temp stage can not be filled. Branch training terminated.
Cascade classifier can't be trained. Check the used training parameters.

(问题3)运行:opencv_traincascade.exe时,注意添加-numPos 50(不一定为样本数,可能比样本数少,不然会出现以下错误) -numNeg 147(除非为默认样本数)
否则会出现以下错误:
OpenCV Error: Bad argument (Can not get new positive sample. 
The most possible reason is insufficient count of samples in given vec-file.) in CvCascadeImageReader::PosReader::get, 
file C:\build\master_winpack-build-win64-vc14\opencv\apps\traincascade\imagestorage.cpp, line 158

(问题4)Bad argument (_cascadeDirName or _bgfileName or _vecFileName is NULL) in CvCascadeClassifier::train,
 file C:\build\master_winpack-build-win64-vc14\opencv\apps\traincascade\cascadeclassifier.cpp
输入格式不对:可能少了 -

(问题5)OpenCV Error: Assertion failed (_img.rows * _img.cols == vecSize) in CvCascadeImageReader::PosReader 
加上 -w 20 -h 20 就好了(你正样本的w和h)


原创粉丝点击