写在前面的话:文章内容来源于但不限于网络、书籍、个人心得体会等,意在总结和方便各位同行快速参考,共同学习进步,记录自己的问题。错误在所难免,有请各位批评斧正。如有侵权,烦请第一时间通知,我会立即删除相关内容,万分感谢!
人脸检测...
//ivs_dlib_facedetection.h
#pragma once
#include "ivs.h"
#include
#include
#include
using namespace cv;
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace dlib;
using namespace std;
//struct CONFIG_ENV
//{
// //�ϰ찻��set 1, ���e������set 0
// int detection_region;
// //��X�����ϰ쪺�I�Ӽ�(�C�Ӱϰ�̦h20���I)
// int num_region_point[11];
// //��X�����ϰ쪺�I�y��(x,y)
// int region_point[11][20][2];
// //IJ�oĵ�����ܤ֫���ɶ� sec
// int stay_time[11];
// //1�N��IJ�o 0�N�������SIJ�o
// int alert[11];
// //�z��IJ�o����U���e�� pixel
// int min_object_width[11];
// //�z��IJ�o����U������ pixel
// int min_object_height[11];
// //�z��IJ�o����W���e�� pixel
// int max_object_width[11];
// //�z��IJ�o����W������ pixel
// int max_object_height[11];
// //�����쪫�?Ӽ�
// int num_object;
// //�����쪫�?y�и�T(x,y,width,height,region)
// int object_info[64][5];
//};
struct face_info
{
double score;
int weight_index;//face direction
int left;
int top;
int right;
int bottom;
int width;
int height;
int center_x;
int center_y;
int nomatch_times;
int match_times;
int id;
};
// ----------------------------------------------------------------------------------------
template using con5d = con;
template using con5 = con;
template using downsampler = relu>>>>>>>>;
template using rcon5 = relu>>;
using net_type = loss_mmod>>>>>>>;
// ----------------------------------------------------------------------------------------
class IVS_FACEDETECTION_DLIB
{
public:
IVS_FACEDETECTION_DLIB();
~IVS_FACEDETECTION_DLIB();
void run_facedetection(matrix& img , std::vector& faces);
void run_facedetection(matrix& img, std::vector& faces);
void run_dnn_facedetection(matrix& img , std::vector& faces);
void run_facedetection_nvr(cv::Mat& frame, CONFIG_ENV& config_env);
private:
int region_num;
int tracking(face_info &face1 , face_info &face2);
frontal_face_detector detector;
net_type net;
};
//ivs_dlib_facedetection.cpp
#include "ivs_dlib_facedetection.h"
IVS_FACEDETECTION_DLIB::IVS_FACEDETECTION_DLIB()
{
detector = get_frontal_face_detector();
deserialize("mmod_human_face_detector.dat") >> net;
region_num = SET_REGION_A;
}
IVS_FACEDETECTION_DLIB::~IVS_FACEDETECTION_DLIB() {}
//std::vector new_faces_list;
//std::vector faces_list;
void IVS_FACEDETECTION_DLIB::run_facedetection_nvr(cv::Mat& frame, CONFIG_ENV& config_env)
{
if(frame.channels() == 4)
cv::cvtColor(frame, frame, CV_BGRA2BGR);
matrix img;
int face_number = 0;
if (config_env.detection_region == 0)
{//full Screen
std::vector faces_list;
matrix img;
assign_image(img, cv_image(frame));
run_dnn_facedetection(img, faces_list);
for (int k = 0; k < faces_list.size(); k++)
{
config_env.object_info[face_number][0] = faces_list[k].left;
config_env.object_info[face_number][1] = faces_list[k].top;
config_env.object_info[face_number][2] = faces_list[k].width;
config_env.object_info[face_number][3] = faces_list[k].height;
config_env.object_info[face_number][4] = 0;
face_number++;
}
}
else
{//multi roi
for (int i = 1; i < region_num; i++)
{
if (config_env.num_region_point[i] != 0)
{
std::vector faces_list;
faces_list.clear();
Rect set_rect;
set_rect.x = config_env.region_point[i][0][0];
set_rect.y = config_env.region_point[i][0][1];
set_rect.width = abs(config_env.region_point[i][2][0] - config_env.region_point[i][0][0]);
set_rect.height = abs(config_env.region_point[i][2][1] - config_env.region_point[i][0][1]);
cv::Mat roi_mat = frame(set_rect);
matrix roi_img;
assign_image(roi_img, cv_image(roi_mat));
run_dnn_facedetection(roi_img, faces_list);
for (int k = 0; k < faces_list.size(); k++)
{
faces_list[k].left = faces_list[k].left + set_rect.x;
faces_list[k].top = faces_list[k].top + set_rect.y;
faces_list[k].right = faces_list[k].right + set_rect.x;
faces_list[k].bottom = faces_list[k].bottom + set_rect.y;
config_env.object_info[face_number][0] = faces_list[k].left;
config_env.object_info[face_number][1] = faces_list[k].top;
config_env.object_info[face_number][2] = faces_list[k].width;
config_env.object_info[face_number][3] = faces_list[k].height;
config_env.object_info[face_number][4] = i;
face_number++;
}
}
}
}
config_env.num_object = face_number;
}
void IVS_FACEDETECTION_DLIB::run_facedetection(matrix& img, std::vector& faces)
{
faces.clear();
std::vectorf_dets;
detector(img, f_dets, 0);
for (int i = 0; i < f_dets.size(); i++)
{
face_info face;
face.score = f_dets[i].detection_confidence * 100;
dlib::rectangle rect = f_dets[i].rect.get_rect();
face.left = rect.left();
face.top = rect.top();
face.right = rect.right();
face.bottom = rect.bottom();
face.center_x = round((face.left + face.right) / 2);
face.center_y = round((face.top + face.bottom) / 2);
face.width = abs(face.right - face.left);
face.height = abs(face.bottom - face.top);
face.weight_index = f_dets[i].weight_index;
faces.push_back(face);
}
}
void IVS_FACEDETECTION_DLIB::run_facedetection(matrix& img, std::vector& faces)
{
detector(img, faces, 0);
}
void IVS_FACEDETECTION_DLIB::run_dnn_facedetection(matrix& img , std::vector& faces)
{
auto dets = net(img);
faces.clear();
for (auto&& d : dets)
{
face_info face;
face.score = d.detection_confidence*100;
face.left = d.rect.left();
face.top = d.rect.top();
face.right = d.rect.right();
face.bottom = d.rect.bottom();
face.center_x = round((face.left + face.right) / 2);
face.center_y = round((face.top + face.bottom) / 2);
face.width = abs(face.right - face.left);
face.height = abs(face.bottom - face.top);
faces.push_back(face);
}
}
int IVS_FACEDETECTION_DLIB::tracking(face_info &new_face, face_info &old_face)
{
if (new_face.center_x >= old_face.left && new_face.center_x <= old_face.right)
if (new_face.center_y >= old_face.top && new_face.center_y <= old_face.bottom)
return 1;
return 0;
}