写在前面的话:文章内容来源于但不限于网络、书籍、个人心得体会等,意在总结和方便各位同行快速参考,共同学习进步,记录自己的问题。错误在所难免,有请各位批评斧正。如有侵权,烦请第一时间通知,我会立即删除相关内容,万分感谢!
人形检测...目测深度学习更好....
//ivs_people_detection.h
#include "ivs.h"
class PeopleDetection : public VirtualFence
{
public:
PeopleDetection();
~PeopleDetection();
/*------------------function------------------------*/
//float Fun_Count_Histo(Mat &src_mat, int i_thresh, int i_type);
void get_people_info(Mat &frame, vector found, OBJECT_INFO& object_data, Mat& regionframe, int roi_num);
void people_detection(Mat &frame, CONFIG_ENV& Config_env);//***main function non-GPU***
void upperbody_detection(Mat &frame, CONFIG_ENV& Config_env);//***main function non-GPU***
#ifdef GPU_CUDA
void people_detection_gpu(Mat &frame, CONFIG_ENV& Config_env);//***main function GPU***
void upperbody_detection_gpu(Mat &frame, CONFIG_ENV& Config_env);//***main function GPU***
#endif
/*------------------Variable------------------------*/
float detect_sensitivity;//0~1 more smaller, more sensitive
int people_counter;
cv::HOGDescriptor myHOG;
string cascade_name_cpu;
CascadeClassifier cascade_cpu;
#ifdef GPU_CUDA
cv::gpu::HOGDescriptor myHOG_gpu;
string cascade_name_gpu;
CascadeClassifier_GPU cascade_gpu;
#endif
};
//ivs_people_detection.cpp
#include "ivs_common.h"
#include "ivs_people_detection.h"
PeopleDetection::PeopleDetection()
{
frame_count = 0;
region_num = 11;
detect_sensitivity = 0.5;//0~1 more smaller, more sensitive
bg_learn_rate = 0.001;
bg_history = 200;
cascade_name_cpu = "haarcascade_mcs_upperbody.xml";
#ifdef GPU_CUDA
mog2_gpu.history = 200;
cascade_name_gpu = "haarcascade_upperbody_gpu.xml";
#endif
}
PeopleDetection::~PeopleDetection() {}
void PeopleDetection::get_people_info(Mat &frame, vector found, OBJECT_INFO& object_data, Mat& regionframe, int roi_num)
{
// Draw positive classified windows
for (size_t i = 0; i < found.size(); i++)
{
Rect r = found[i];
//people_mask copy to fgmask, then contour gets rect. if the rect.area() > 0.5*r.area(), then r is people.
Mat people_pixel = regionframe(r);
float whitepixels = Fun_Count_Histo(people_pixel, 254, 1);//get numbers of white pixel
if(whitepixels > r.width*r.height*detect_sensitivity)
{
object_data.object_rect.push_back(r);
object_data.object_region = roi_num;
//rectangle(frame, r.tl(), r.br(), Scalar(0, 0, 255), 2);
people_counter++;
}
}
}
#ifdef GPU_CUDA
void PeopleDetection::people_detection_gpu(Mat &frame, CONFIG_ENV& Config_env)
{
vector roi_setting_vec;
vector object_info_vec;
Point roi_point;
for(int i = 0; i found;
GpuMat gpu_img;
people_counter = 0;
if(frame_count == 0){
preframe_rows = frame.rows;
preframe_cols = frame.cols;
// Create HOG descriptors and detectors here
myHOG_gpu.setSVMDetector(cv::gpu::HOGDescriptor::getDefaultPeopleDetector());
}
d_frame.upload(frame);
//mog2背景构建
bg_subtraction_mog2_gpu(bg_learn_rate);
if (frame_count >= mog2_gpu.history)
{
if(frame_count == mog2_gpu.history)
cout << "People Background OK !"< 0)
{
if(Config_env.detection_region > 0)
{
for(int a=1; a0)
{
Mat regionmask = Mat::zeros(frame.size(), CV_8UC1);
Mat regionframe;
get_roi_mask(frame, roi_setting_vec[a], regionmask, a);
fgmask_merge.copyTo(regionframe,regionmask);
get_people_info(frame, found, object_info_vec[a], regionframe, a);
}
}
}
else
{
get_people_info(frame, found, object_info_vec[0], fgmask_merge, 0);
}
}
Config_env.num_object = people_counter;
for(int i=0; i 100000000)
frame_count = 10000;
//imshow("video demo", frame);
}
void PeopleDetection::upperbody_detection_gpu(Mat &frame, CONFIG_ENV& Config_env)
{
vector roi_setting_vec;
vector object_info_vec;
Point roi_point;
for(int i = 0; i found;
GpuMat gpu_img;
people_counter = 0;
if(frame_count == 0){
preframe_rows = frame.rows;
preframe_cols = frame.cols;
if (!cascade_gpu.load(cascade_name_gpu))
{
cout << "ERROR: Could not load cascade classifier "<= mog2_gpu.history)
{
if(frame_count == mog2_gpu.history)
cout << "People Background OK !"<();
for(int i = 0; i < detections_number; ++i)
{
found.push_back(people[i]);
//rectangle(frame, found[i].tl(), found[i].br(), Scalar(0, 255, 0), 2);
}
if(found.size() > 0)
{
if(Config_env.detection_region > 0)
{
for(int a=1; a0)
{
Mat regionmask = Mat::zeros(frame.size(), CV_8UC1);
Mat regionframe;
get_roi_mask(frame, roi_setting_vec[a], regionmask, a);
fgmask_merge.copyTo(regionframe,regionmask);
get_people_info(frame, found, object_info_vec[a], regionframe, a);
}
}
}
else
{
get_people_info(frame, found, object_info_vec[0], fgmask_merge, 0);
}
}
Config_env.num_object = people_counter;
for(int i=0; i 100000000)
frame_count = 10000;
//imshow("video demo", frame);
}
#endif
void PeopleDetection::people_detection(Mat &frame, CONFIG_ENV& Config_env)
{
vector roi_setting_vec;
vector object_info_vec;
Point roi_point;
for(int i = 0; i found;
people_counter = 0;
Mat frame_gray;
if(frame_count == 0){
preframe_rows = frame.rows;
preframe_cols = frame.cols;
// Create HOG descriptors and detectors here
myHOG.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
}
if(frame_count < bg_history)
{
//mog2背景构建
mog2(frame, fgmask, -1);
}
else
{
if(frame_count == bg_history)
cout << "People Background OK !"< 0)
{
if(Config_env.detection_region > 0)
{
for(int a=1; a0)
{
Mat regionmask = Mat::zeros(frame.size(), CV_8UC1);
Mat regionframe;
get_roi_mask(frame, roi_setting_vec[a], regionmask, a);
fgmask_merge.copyTo(regionframe,regionmask);
get_people_info(frame, found, object_info_vec[a], regionframe, a);
}
}
}
else
{
get_people_info(frame, found, object_info_vec[0], fgmask_merge, 0);
}
}
Config_env.num_object = people_counter;
for(int i=0; i 100000000)
frame_count = 10000;
//imshow("video demo", frame);
}
void PeopleDetection::upperbody_detection(Mat &frame, CONFIG_ENV& Config_env)
{
vector roi_setting_vec;
vector object_info_vec;
Point roi_point;
for(int i = 0; i found;
people_counter = 0;
Mat frame_gray;
if(frame_count == 0){
preframe_rows = frame.rows;
preframe_cols = frame.cols;
if (!cascade_cpu.load(cascade_name_cpu))
{
cout << "ERROR: Could not load cascade classifier "< 0)
{
if(Config_env.detection_region > 0)
{
for(int a=1; a0)
{
Mat regionmask = Mat::zeros(frame.size(), CV_8UC1);
Mat regionframe;
get_roi_mask(frame, roi_setting_vec[a], regionmask, a);
fgmask_merge.copyTo(regionframe,regionmask);
get_people_info(frame, found, object_info_vec[a], regionframe, a);
}
}
}
else
{
get_people_info(frame, found, object_info_vec[0], fgmask_merge, 0);
}
}
Config_env.num_object = people_counter;
for(int i=0; i 100000000)
frame_count = 10000;
//imshow("video demo", frame);
}