Dlib人脸检测(C++)

1.时间检测类

#ifndef TIMESTRAMP_H
#define TIMESTRAMP_H

#include <stdio.h>
#include <iostream>

#ifndef _WIN32
#include <sys/time.h>
#else
#include <windows.h>
#endif

using namespace std;

#define TIME_DEBUG(str) \
	cout <<  "[" << __DATE__ "] data:" << str << endl

#ifndef _WIN32
class timestramp{
public:
    timestramp()
    {
         gettimeofday(&tpstart,NULL);
    }

    ~timestramp()
    {
         gettimeofday(&tpend,NULL);
         //timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;//注意,秒的读数和微秒的读数都应计算在内
         timeuse = (1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec)/1000;
         printf("used time:%fms\n",timeuse);
    }
private:
     struct timeval tpstart,tpend;
     double timeuse;
};

#else
class timestramp{
private:
    LARGE_INTEGER m_litmp;
    LONGLONG QPart2;
    LONGLONG QPart1;
    double dfMinus, dfFreq, dfTim;
public:
    timestramp(){
        QueryPerformanceFrequency(&m_litmp);
        dfFreq = (double)m_litmp.QuadPart;
        QueryPerformanceCounter(&m_litmp);
        QPart1 = m_litmp.QuadPart;
    }

    ~timestramp(){
        QueryPerformanceCounter(&m_litmp);
        QPart2 = m_litmp.QuadPart;
        dfMinus = (double)(QPart2 - QPart1);
        dfTim = dfMinus / dfFreq * 1000;

        //显示时间
        std::string msg4 = "time:", msg3, msg5 = "ms";
		char strTime[20] = "";
		sprintf(strTime, "%.6lf", dfTim);
		msg3 = strTime;
		msg4 += msg3;
		msg4 += msg5;
		TIME_DEBUG(msg4.c_str());
    }
};
#endif

#endif // TIMESTRAMP_H

2.人脸检测

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>

#include "timestramp.h"

#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\opencv.hpp>

using namespace dlib;
using namespace std;

//using namespace cv;

//resize image
cv::Mat MyResizeImage(cv::Mat pSrc, double dScale)
{
	cv::Size sSize = cv::Size(pSrc.cols*dScale, pSrc.rows*dScale);
	cv::Mat pDes = cv::Mat(sSize, CV_32S);
	resize(pSrc, pDes, sSize);
	return pDes;
}

// ----------------------------------------------------------------------------------------

int main(int argc, char** argv)
{  
    try
    {
        if (argc == 1)
        {
            cout << "Call this program like this:" << endl;
            cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
            cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
            cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
            return 0;
        }

        frontal_face_detector detector = get_frontal_face_detector();
        shape_predictor sp;
        deserialize(argv[1]) >> sp;


        image_window win, win_faces;
		cv::VideoCapture cap("E:\\code\\alcode\\dlib-19.15\\examples\\Webcam_pose\\video.mp4");
		if (!cap.isOpened())
		{
			cerr << "Unable to connect to camera" << endl;
			return 1;
		}
#if 1
		while (1)
		{
			timestramp tp;
			// Grab a frame
			cv::Mat temp;
			if (!cap.read(temp))
			{
                cap.set(CV_CAP_PROP_POS_FRAMES, 0);
				continue;
				//break;
			}

			cv::Mat pDes = MyResizeImage(temp, 0.4);

			//cout << "processing image " << argv[i] << endl;
	        //cv_image<bgr_pixel> cimg(temp);
			array2d<rgb_pixel> img;
			dlib::assign_image(img, dlib::cv_image<dlib::bgr_pixel>(pDes));
			
			//load_image(img, argv[i]);
			// Make the image larger so we can detect small faces.
			pyramid_up(img);

			// Now tell the face detector to give us a list of bounding boxes
			// around all the faces in the image.
			std::vector<rectangle> dets = detector(img);
			cout << "Number of faces detected: " << dets.size() << endl;

			// Now we will go ask the shape_predictor to tell us the pose of
			// each face we detected.
			std::vector<full_object_detection> shapes;
			for (unsigned long j = 0; j < dets.size(); ++j)
			{
				full_object_detection shape = sp(img, dets[j]);
				cout << "number of parts: " << shape.num_parts() << endl;
				cout << "pixel position of first part:  " << shape.part(0) << endl;
				cout << "pixel position of second part: " << shape.part(1) << endl;
				// You get the idea, you can get all the face part locations if
				// you want them.  Here we just store them in shapes so we can
				// put them on the screen.
				shapes.push_back(shape);
			}

			// Now let's view our face poses on the screen.
			win.clear_overlay();
			win.set_image(img);
			win.add_overlay(render_face_detections(shapes));
#if 0
			// We can also extract copies of each face that are cropped, rotated upright,
			// and scaled to a standard size as shown here:
			dlib::array<array2d<rgb_pixel> > face_chips;
			extract_image_chips(img, get_face_chip_details(shapes), face_chips);
			win_faces.set_image(tile_images(face_chips));

			cout << "Hit enter to process the next image..." << endl;
#endif
		}
#endif

    }
    catch (exception& e)
    {
        cout << "\nexception thrown!" << endl;
        cout << e.what() << endl;
    }
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

telllong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值