赞
踩
正样本数据 : 也就是我们需要检测的物体图片,可以自己用手机拍摄下你要检测的物体的图片,多拍摄一些不同角度的图片。
我的正样本数据在这个目录下 image\positive\img,大概有50多张图片
然后在image\positive目录下新建一个info.dat文件,在其中记录正样本图片信息
参数介绍
负样本数据:不包含我们要检测物体的图片,可以拍摄一些风景之类的图片,尽量多一些。
我的负样本数据在这个目录下 image\negitive\img
然后在image\negitive目录下新建一个bg.txt文件,在其中记录负样本图片信息
负样本图片信息我们只需记录路径和文件名就行了,但是这里要注意,路径名要写绝对路径,后面会说为什么。
[-info <collection_file_name>] # 记录样本数据的文件(就是我们刚才创建的info.data文件)
[-img <image_file_name>]
[-vec <vec_file_name>] # 输出文件,内含用于训练的正样本。
[-bg <background_file_name>] # 背景图像的描述文件
[-num <number_of_samples = 1000>] #样本数量(默认为1000)
[-bgcolor <background_color = 0>] #指定背景颜色
[-w <sample_width = 24>]#输出样本的宽度(以像素为单位)
[-h <sample_height = 24>]#输出样本的高度(以像素为单位)
D:\opencv3.4.12\opencv\build\x64\vc15\bin\opencv_createsamples.exe -info C:\Users\lng\Desktop\image\positive\info.dat -vec C:\Users\lng\Desktop\image\sample.vec -num 58 -bgcolor 0 -bgthresh 0 -w 24 -h 24
-data <cascade_dir_name> #目录名,如不存在训练程序会创建它,用于存放训练好的分类器
-vec <vec_file_name> #包含正样本的vec文件名
-bg <background_file_name> #背景描述文件
[-numPos <number_of_positive_samples = 2000>] #每级分类器训练时所用的正样本数目
[-numNeg <number_of_negative_samples = 1000>] #每级分类器训练时所用的负样本数目
[-numStages <number_of_stages = 20>] #训练的分类器的级数
--cascadeParams--
[-featureType <{HAAR(default), LBP, HOG}>] # 特征的类型: HAAR - 类Haar特征; LBP - 局部纹理模式特征
[-w <sampleWidth = 24>] #训练样本的尺寸(单位为像素)
[-h <sampleHeight = 24>] #训练样本的尺寸(单位为像素)
--boostParams--
[-minHitRate <min_hit_rate> = 0.995>] #分类器的每一级希望得到的最小检测率
[-maxFalseAlarmRate <max_false_alarm_rate = 0.5>] #分类器的每一级希望得到的最大误检率
D:\opencv3.4.12\opencv\build\x64\vc15\bin\opencv_traincascade.exe -data C:\Users\lng\Desktop\image -vec C:\Users\lng\Desktop\image\sample.vec -bg bg.txt -numPos 50 -numNeg 500 -numStages 12 -feattureType HAAR -w 24 -h 24 -minHitRate 0.995 -maxFalseAlarmRate 0.5
PARAMETERS: cascadeDirName: C:\Users\lng\Desktop\image vecFileName: C:\Users\lng\Desktop\image\sample.vec bgFileName: bg.txt numPos: 50 numNeg: 500 numStages: 12 precalcValBufSize[Mb] : 1024 precalcIdxBufSize[Mb] : 1024 acceptanceRatioBreakValue : -1 stageType: BOOST featureType: HAAR sampleWidth: 24 sampleHeight: 24 boostType: GAB minHitRate: 0.995 maxFalseAlarmRate: 0.5 weightTrimRate: 0.95 maxDepth: 1 maxWeakCount: 100 mode: BASIC Number of unique features given windowSize [24,24] : 162336 ===== TRAINING 0-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 500 : 1 Precalculation time: 0.581 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.05| +----+---------+---------+ END> Training until now has taken 0 days 0 hours 0 minutes 1 seconds. ===== TRAINING 1-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 500 : 0.084832 Precalculation time: 0.576 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.146| +----+---------+---------+ END> Training until now has taken 0 days 0 hours 0 minutes 3 seconds. ===== TRAINING 2-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 500 : 0.0149993 Precalculation time: 0.592 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.186| +----+---------+---------+ END> Training until now has taken 0 days 0 hours 0 minutes 5 seconds. ===== TRAINING 3-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 500 : 0.00288033 Precalculation time: 0.652 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.298| +----+---------+---------+ END> Training until now has taken 0 days 0 hours 0 minutes 7 seconds. ===== TRAINING 4-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 500 : 0.000768845 Precalculation time: 0.615 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.366| +----+---------+---------+ END> Training until now has taken 0 days 0 hours 0 minutes 11 seconds. ===== TRAINING 5-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 500 : 0.000375057 Precalculation time: 0.61 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.366| +----+---------+---------+ END> Training until now has taken 0 days 0 hours 0 minutes 15 seconds. ===== TRAINING 6-stage ===== <BEGIN POS count : consumed 50 : 50 NEG count : acceptanceRatio 2 : 0.00016276 Required leaf false alarm rate achieved. Branch training t
#include <iostream> #include <opencv2/opencv.hpp> char* face_cascade_name = "C:\\Users\\lng\\Desktop\\image\\cascade.xml"; void faceRecongize(cv::CascadeClassifier faceCascade, cv::Mat frame); int main(){ cv::VideoCapture *videoCap = new cv::VideoCapture; cv::CascadeClassifier faceCascade; // 加载苹果分类器文件 if (!faceCascade.load(face_cascade_name)) { std::cout << "load face_cascade_name failed. " << std::endl; return -1; } // 打开摄像机 videoCap->open(0); if (!videoCap->isOpened()) { videoCap->release(); std::cout << "open camera failed"<< std::endl; return -1; } std::cout << "open camera success"<< std::endl; while(1){ cv::Mat frame; //读取视频帧 videoCap->read(frame); if (frame.empty()) { videoCap->release(); return -1; } //进行苹果识别 faceRecongize(faceCascade, frame); //窗口进行展示 imshow("face", frame); //等待回车键按下退出程序 if (cv::waitKey(30) == 13) { cv::destroyAllWindows(); return 0; } } system("pause"); return 0; } void faceRecongize(cv::CascadeClassifier faceCascade, cv::CascadeClassifier eyesCascade, cv::CascadeClassifier mouthCascade, cv::Mat frame) { std::vector<cv::Rect> faces; // 检测苹果 faceCascade.detectMultiScale(frame, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30)); for (int i = 0; i < faces.size(); i++) { // 用椭圆画出苹果部分 cv::Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2); ellipse(frame, center, cv::Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0); cv::Mat faceROI = frame(faces[i]); std::vector<cv::Rect> eyes; // 苹果上方区域写字进行标识 cv::Point centerText(faces[i].x + faces[i].width / 2 - 40, faces[i].y - 20); cv::putText(frame, "apple", centerText, cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 2); } }
cmake_minimum_required (VERSION 3.5) project (faceRecongize2015) MESSAGE(STATUS "PROJECT_SOURCE_DIR " ${PROJECT_SOURCE_DIR}) SET(SRC_LISTS ${PROJECT_SOURCE_DIR}/src/main.cpp) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") #set(CMAKE_AUTOMOC ON) #set(CMAKE_AUTOUIC ON) #set(CMAKE_AUTORCC ON) # 配置头文件目录 include_directories(${PROJECT_SOURCE_DIR}/src) include_directories("D:\\opencv3.4.12\\opencv\\build\\include") include_directories("D:\\opencv3.4.12\\opencv\\build\\include\\opencv2") # 设置不显示命令框 if(MSVC) #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") endif() # 添加库文件 set(PRO_OPENCV_LIB "D:\\opencv3.4.12\\opencv\\build\\x64\\vc15\\lib\\opencv_world3412.lib" "D:\\opencv3.4.12\\opencv\\build\\x64\\vc15\\lib\\opencv_world3412d.lib") IF(WIN32) # 生成可执行程序 ADD_EXECUTABLE(faceRecongize2015 ${SRC_LISTS}) # 链接库文件 TARGET_LINK_LIBRARIES(faceRecongize2015 ${PRO_OPENCV_LIB}) ENDIF()
- src
- mian.cpp
- build_x64
- CMakeLists
cmake -G "Visual Studio 14 2015 Win64" ..
cmake --build ./ --config Release
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。