lbpcascade_frontalface.xml与dlib库从摄像头中取最大的一张人脸进行人眼检测的c++程序...

以下是使用 lbpcascade_frontalface.xml 和 dlib 库从摄像头中取出最大的一张人脸进行人眼检测的 C++ 程序示例:

```c++ #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 <dlib/opencv.h> #include <opencv2/opencv.hpp>

using namespace dlib; using namespace std;

int main() { try { // 初始化人脸检测器 frontal_face_detector detector = get_frontal_face_detector();

// 初始化人眼检测器
    shape_predictor sp;
    deserialize("shape_predictor_68_face_landmarks.dat") >> sp;

    // 打开摄像头
    cv::VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cerr << "Unable to connect to camera" << endl;
        return 1;
    }

    // 循环读取摄像头中的每一帧
    while (1)
    {
        cv::Mat temp;
        cap >> temp;
        cv_image<bgr_pixel> cimg(temp);

        // 检测人脸
        std::vector<rectangle> faces = detector(cimg);
        if(faces.size()==0) continue;
        int max_x = 0, max_y = 0, max_area = 0;
        for (int i = 0; i < faces.size(); i++)
        {
            if(faces[i].area()>max_area)
            {
                max_x = faces[i].left();
                max_y = faces[i].top();
                max_area = faces[i].area();
            }
        }
        rect max_face = rect(max_x, max_y, max_x + faces[0].width(), max_y + faces[0].height());
        // 检测人眼
        full_object_detection shape = sp(cimg, max_face);
        // 在图像中画出人眼
        for (int i = 36; i <= 41; i++)
        {
            cv::circle(temp, cv::Point(shape.part(i).x(), shape.part(i).y()), 2, cv::Scalar(0, 255, 0), -1);
        }
        cv::imshow("Face", temp);
        if (cv::waitKey
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值