在3.x版本人连检测,常用haar级联器检测,受光照、人脸位置影响比较大,识别率也不理想,DNN模块给出专用人脸模型,跟踪效果很好,抗干扰能力很强。在4版本之后DNN检测以后将会成为主流。
res10_300x300_ssd_iter_140000_fp16.caffemodel
deploy.prototxt
opencv_face_detector_uint8.pb
opencv_face_detector.pbtxt
# OpenCV's face detection network
opencv_fd:
model: "opencv_face_detector.caffemodel"
config: "opencv_face_detector.prototxt"
mean: [104, 177, 123]
scale: 1.0
width: 300
height: 300
rgb: false
sample: "object_detection"
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::dnn;
int main(void)
{
string bin_model = "/work/opencv_dnn/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel";
string protxt = "/work/opencv_dnn/face_detector/deploy.prototxt";
// load network model
Net net = readNetFromCaffe(protxt, bin_model);
// 设置计算后台
net.setPreferableBackend(DNN_BACKEND_OPENCV);
net.setPreferableTarget(DNN_TARGET_CPU);
namedWindow("检测画面",WINDOW_AUTOSIZE);
// 获取各层信息
vector<string> layer_names = net.getLayerNames();
for (int i = 0; i < layer_names.size(); i++) {
int id = net.getLayerId(layer_names[i]);
auto layer = net.getLayer(id);
printf("layer id : %d, type : %s, name : %s \n", id, layer->type.c_str(), layer->name.c_str());
}
VideoCapture capture(0);
Mat frame;
while (true) {
bool ret = capture.read(frame);
if (!ret) break;
flip(frame,frame,1);
// 构建输入
Mat blob = blobFromImage(frame, 1, Size(300, 300), Scalar(104, 177, 123), false, false);
net.setInput(blob, "data");
// 执行推理
Mat detection = net.forward("detection_out");
Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
float confidence_threshold = 0.5;
// 解析输出数据
for (int i = 0; i < detectionMat.rows; i++) {
float* curr_row = detectionMat.ptr<float>(i);
int image_id = (int)(*curr_row++);
size_t objIndex = (size_t)(*curr_row++);
float score = *curr_row++;
if (score > confidence_threshold) {
float tl_x = (*curr_row++) * frame.cols;
float tl_y = (*curr_row++) * frame.rows;
float br_x = (*curr_row++) * frame.cols;
float br_y = (*curr_row++) * frame.rows;
Rect box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
rectangle(frame, box, Scalar(0, 0, 255), 2, 8, 0);
putText(frame, "man face", box.tl(), FONT_HERSHEY_SIMPLEX, 1.5, Scalar(0, 255, 0), 3, 8);
}
}
// measure time consume
vector<double> layersTimings;
double freq = getTickFrequency() / 1000.0;
double time = net.getPerfProfile(layersTimings) / freq;
ostringstream ss;
ss << "FPS: " << 1000 / time << " ; time : " << time << " ms";
// show
putText(frame, ss.str(), Point(20, 20), FONT_HERSHEY_PLAIN, 1.0, Scalar(255, 0, 0), 2, 8);
imshow("检测画面", frame);
char c = waitKey(1);
if (c == 27) { // ESC
break;
}
}
// 释放资源
capture.release();
waitKey(0);
destroyAllWindows();
return 0;
}