c++ 第三方人脸检测算法SDK调用

1 dlib

依赖:

dlib-19.18

opencv-3.4.2

#include "common.h"
#include "opencv_base.h"
#include "helper.h"
#include "run.h"

// ============= dlib include files =======================
#include <image_processing/frontal_face_detector.h>
#include <image_processing/render_face_detections.h>
#include <image_processing.h>
#include <gui_widgets.h>
#include <image_io.h>
#include <opencv.h>
#include <opencv/cv_image.h>

using namespace dlib;

TickMeter tm2;

int TEST_DLIB_FACE() {
    string video_path = "..\\videos\\bilibili\\1.mp4";
    assert(exist_file(video_path));
    VideoCapture cp;
    Mat frame, face_roi;
    frontal_face_detector detector = get_frontal_face_detector();
    array2d<rgb_pixel> img;
    std::vector<dlib::rectangle> dets;
    if (cp.open(video_path)) {
        int total_num = cp.get(CAP_PROP_FRAME_COUNT);
        //cout << "total frame count: " << total_num << endl;
        while (cp.read(frame))
        {
            dets.clear();
            tm2.reset();
            tm2.start();
            dlib::assign_image(img, cv_image<rgb_pixel>(frame));
            // Make the image larger so we can detect small faces.
            pyramid_up(img);
            dets = detector(img);
            tm2.stop();
            cout << "Number of faces detected: " << dets.size() << endl;
            cout << "face detect elapsed time: " << tm2.getTimeMilli() << endl;
            for (auto box : dets) {
                int x1 = box.left();
                int y1 = box.top();
                int w = box.width();
                int h = box.height();


                cv::rectangle(frame, Rect(x1, y1, w, h), Scalar(0, 0, 255), 1);
            }

            cv::imshow("src", frame);
            cv::waitKey(1);

        }
    }
    cp.release();
    frame.release();
    face_roi.release();
    delete &img;

    system("pause");
    return 0;
}

2 libfacedetectcnn

github: https://github.com/ShiqiYu/libfacedetection
依赖:opencv-3.4.2

#include "common.h"
#include "opencv_base.h"

#include "facedetectcnn.h"
//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000

int SGHHFTHR() {
	string image_path = "..\\images\\epoch029_idt_A.png";
	Mat image = imread(image_path);

	int * pResults = NULL;
	//pBuffer is used in the detection functions.
	//If you call functions in multiple threads, please create one buffer for each thread!
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	if (!pBuffer)
	{
		fprintf(stderr, "Can not alloc buffer.\n");
		return -1;
	}
	///////////////////////////////////////////
	// CNN face detection 
	// Best detection rate
	//////////////////////////////////////////
	//!!! The input image must be a BGR one (three-channel) instead of RGB
	//!!! DO NOT RELEASE pResults !!!
	pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);

	printf("%d faces detected.\n", (pResults ? *pResults : 0));
	//print the detection results
	for (int i = 0; i < (pResults ? *pResults : 0); i++)
	{
		short * p = ((short*)(pResults + 1)) + 142 * i;
		int x = p[0];
		int y = p[1];
		int w = p[2];
		int h = p[3];
		int confidence = p[4];
		int angle = p[5];

		printf("face_rect=[%d, %d, %d, %d], confidence=%d, angle=%d\n", x, y, w, h, confidence, angle);
		rectangle(image, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
	}


	imshow("src", image);
	waitKey(0);


	//release the buffer
	free(pBuffer);
	system("pause");
	return 0;
}

  

3 LFFD
github: https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB
依赖:
ncnn
opencv-3.4.2

#include "common.h"
#include "opencv_base.h"
#include "helper.h"
#include "ncnn_base.h"
#include "run.h"
#include "UltraFace.hpp"

//人脸检测  
#define LFFD_FACE_DET_MODEL_PARAM "..\\models\\face_detect\\LFFD\\version-slim\\slim_320.param"
#define LFFD_FACE_DET_MODEL_BIN "..\\models\\face_detect\\LFFD\\version-slim\\slim_320.bin"


int LFFD_FACE_DETECT() {
	string param_path = LFFD_FACE_DET_MODEL_PARAM, bin_path = LFFD_FACE_DET_MODEL_BIN;
	assert(exist_file(param_path));
	assert(exist_file(bin_path));
	UltraFace *ultraface = new UltraFace(bin_path, param_path, 320, 240, 2, 0.7); // config model input
	vector<FaceInfo> face_boxes;
	TickMeter tm;

	string video_path = "../videos/bilibili/1.mp4";
	assert(exist_file(video_path));

	VideoCapture cp;
	Mat frame, input_frame;
	char str_tmp[200];
	int frame_idx = 0;
	ncnn::Mat ncnn_img;
	if (cp.open(video_path)) {
		int total_num = cp.get(CAP_PROP_FRAME_COUNT);
		cout << "total frame count: " << total_num << endl;
		while (cp.read(frame))
		{
			cvtColor(frame, input_frame, COLOR_BGR2RGB);
			int src_w = frame.cols, src_h = frame.rows;
			tm.reset();
			tm.start();
			face_boxes.clear();
			ncnn_img = ncnn::Mat::from_pixels(input_frame.data, ncnn::Mat::PIXEL_RGB, src_w, src_h);
			ultraface->detect(ncnn_img, face_boxes);
			ncnn_img.release();
			tm.stop();
			cout << "face detect elapsed: " << tm.getTimeMilli() << " ms" << endl;
			printf("face num: %d \n", (int)face_boxes.size());
			for (auto box : face_boxes) {
				int x1 = box.x1;
				int y1 = box.y1;
				int w = box.x2 - x1;
				int h = box.y2 - y1;

				rectangle(frame, Rect(x1, y1, w, h), Scalar(255, 0, 0), 1);
			}

			imshow("src", frame);
			waitKey(40);
		}
	}

	destroyAllWindows();
	cp.release();
	frame.release();
	input_frame.release();
	delete ultraface;


	system("pause");
	return 0;
}

  

4 mtcnn
github: https://github.com/cpuimage/MTCNN
依赖:
ncnn
opencv-3.4.2

#include "common.h"
#include "opencv_base.h"
#include "helper.h"
#include "ncnn_base.h"
#include "mtcnn.h"
#include "run.h"


int MTCNN_FACE_DETECT() {
	string model_path = "../models/face_detect/mtcnn";
	vector<Bbox> finalBbox;
	MTCNN *mtcnn = new MTCNN(model_path);
	int miniFace = 40;
	mtcnn->SetMinFace(miniFace);
	TickMeter tm;


	//string video_path = "../videos/bilibili/2.mp4";
	string video_path = "../images/expression_images/2-0.jpg";
	assert(exist_file(video_path));

	VideoCapture cp;
	Mat frame, input_frame;
	char str_tmp[200];
	int frame_idx = 0;
	ncnn::Mat ncnn_img;
	if (cp.open(video_path)) {
		int total_num = cp.get(CAP_PROP_FRAME_COUNT);
		cout << "total frame count: " << total_num << endl;
		while (cp.read(frame))
		{
			cvtColor(frame, input_frame, COLOR_BGR2RGB);
			int src_w = frame.cols, src_h = frame.rows;
			tm.reset();
			tm.start();
			ncnn_img = ncnn::Mat::from_pixels(input_frame.data, ncnn::Mat::PIXEL_RGB, src_w, src_h);
			mtcnn->detect(ncnn_img, finalBbox);
			ncnn_img.release();
			tm.stop();
			cout << "face emotion elapsed: " << tm.getTimeSec() << " s" << endl;
			printf("face num: %d \n", (int)finalBbox.size());
			for (auto box : finalBbox) {
				// face box
				int x1 = box.x1;
				int y1 = box.y1;
				int w = box.x2 - x1;
				int h = box.y2 - y1;
				rectangle(frame, Rect(x1, y1, w, h), Scalar(255, 0, 0), 1);

				//landmark 5
				for (size_t j = 0; j < 5; j ++)
				{
					float x = box.ppoint[j], y = box.ppoint[j + 5];
					circle(frame, Point2d(x, y), 3, Scalar(0, 255, 0), 2);
				}

			}

			imshow("src", frame);
			waitKey(0);
		}
	}

	destroyAllWindows();
	cp.release();
	frame.release();
	input_frame.release();
	delete mtcnn;


	system("pause");
	return 0;
}

  5 opencv

#define OPENCV_PATH "D:\\Program Files\\opencv-3.4.2"
#define OPENCV_MODEL_PATH  "D:\\Program Files\\opencv-3.4.2\\data\\haarcascades"

string video_path = "..\\videos\\bilibili\\4.mp4";
	assert(exist_file(video_path));
	string opencv_model_path = OPENCV_MODEL_PATH;
	string opencv_face_model = opencv_model_path + "\\haarcascade_frontalface_alt2.xml";
	assert(exist_file(opencv_face_model));

	CascadeClassifier detector = CascadeClassifier(opencv_face_model);
	assert(!detector.empty());

	TickMeter tm;
	VideoCapture cp;
	Mat frame, gray;
	vector<Rect> boxes;
	if (cp.open(video_path)) {
		int total_num = cp.get(CAP_PROP_FRAME_COUNT);
		//cout << "total frame count: " << total_num << endl;
		while (cp.read(frame))
		{
			boxes.clear();
			cvtColor(frame, gray, COLOR_BGR2GRAY);
			tm.reset();
			tm.start();
			detector.detectMultiScale(gray, boxes, 1.5, 3, 0);
			tm.stop();
			cout << "face detect elapsed time: " << tm.getTimeMilli() << endl;
			//cout << "检测到人脸个数:" << boxes.size() << endl;
			if (boxes.size() > 0) {
				for (auto box : boxes) {
					box.height *= 1.05;
					rectangle(frame, box, Scalar(0, 0, 255), 1);
				}
			}

			imshow("src", frame);
			waitKey(5);

		}
	}

	system("pause");
	return 0;

  

posted @ 2019-12-26 09:47  dangxusheng  阅读(681)  评论(0编辑  收藏  举报