【OpenCV人脸识别2】从视频中检测人脸

分为两步:

  1. 从视频中识别人脸和人的眼睛
  2. 从视频中检测人脸、眼睛、鼻子、嘴巴

 

1.从视频中识别人脸和人的眼睛

    关于视频的操作,主要如下:

    定义摄像头->打开摄像头->读取视频帧->转而为对图片的操作(一帧就相当于一幅图片)

VideoCapture capture; //定义摄像头捕捉 变量
Mat frame; 
capture.open(0); //打开摄像头
while (capture.read(frame)) //读取帧
{
//进行人脸检测
//显示
}

视频人脸检测的代码:

//face_detect_from_video.cpp 定义控制台应用程序的入口点。
//从视频中识别人脸和人的眼睛
#include "stdafx.h"
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
 
#include <stdio.h>
 
using namespace std;
using namespace cv;
 
/** Function Headers */
void detectAndDisplay(Mat frame);
 
/** Global variables */
String face_cascade_name, eyes_cascade_name;
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
 
/** @function main */
int main(int argc, const char** argv)
{
    face_cascade_name = "./xml/haarcascade_frontalface_alt.xml";
    eyes_cascade_name = "./xml/haarcascade_eye.xml";
    VideoCapture capture;
    Mat frame;
 
    //-- 1. Load the cascades
    if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading eyes cascade\n"); return -1; };
 
    //-- 2. Read the video stream
    capture.open(0); //打开摄像头
    if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }
 
    while (capture.read(frame)) //读取帧
    {
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }
 
        //-- 3. Apply the classifier to the frame
        detectAndDisplay(frame);
 
        if (waitKey(10) == 'k') { break; } // escape
    }
    return 0;
}
 
/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;
 
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);  //BGR 转化为灰度图
    equalizeHist(frame_gray, frame_gray);   //直方图均衡化
 
    //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(60, 60));
 
    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2); // 人脸中心坐标
        ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); // 椭圆
        Mat faceROI = frame_gray(faces[i]);
        std::vector<Rect> eyes;
 
        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
 
        for (size_t j = 0; j < eyes.size(); j++)
        {
            Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2); //眼睛的中心
            int radius = cvRound((eyes[j].width + eyes[j].height)*0.25); //取整
            circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}

    运行结果:

         丑拒(#^.^#)

 

2. 从视频中检测人脸、眼睛、鼻子、嘴巴

将上述第一部分的从视频中识别人脸和眼睛,再加上鼻子、嘴巴等的识别,可实现从视频中检测人脸特征。

代码如下:

//face_recog_from_video.cpp 定义控制台应用程序的入口点。
 
#include "stdafx.h"
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
 
#include <stdio.h>
#include<iostream>
using namespace std;
using namespace cv;
 
/** Function Headers */
void detectAndDisplay(Mat frame);
 
/** Global variables */
String face_cascade_name, eyes_cascade_name, nose_cascade_name , mouth_cascade_name;
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
CascadeClassifier nose_cascade;
CascadeClassifier mouth_cascade;
String window_name = "Capture - Face detection";
 
/** @function main */
int main(int argc, const char** argv)
{
    face_cascade_name = "./xml/haarcascade_frontalface_alt.xml";
    eyes_cascade_name = "./xml/haarcascade_eye.xml";
    nose_cascade_name = "./xml/haarcascade_mcs_nose.xml";
    mouth_cascade_name = "./xml/haarcascade_mcs_mouth.xml";
 
    VideoCapture capture;
    Mat frame;
 
    //-- 1. Load the cascades
    if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); return -1; };
    if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading eyes cascade\n"); return -1; };
    if (!nose_cascade.load(nose_cascade_name)) { printf("--(!)Error loading nose cascade\n"); return -1; };
    if (!mouth_cascade.load(mouth_cascade_name)) { printf("--(!)Error loading mouth cascade\n"); return -1; };
 
    //-- 2. Read the video stream
    capture.open(0); //打开摄像头
    if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }
 
    while (capture.read(frame)) //读取帧
    {
        if (frame.empty())
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }
 
        //-- 3. Apply the classifier to the frame
        detectAndDisplay(frame);
 
        if (waitKey(10) == 'k') { break; } // escape
    }
    return 0;
}
 
/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
    std::vector<Rect> faces;
    Mat frame_gray;
 
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);  //BGR 转化为灰度图
    equalizeHist(frame_gray, frame_gray);   //直方图均衡化
 
                                            //-- Detect faces
    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(60, 60));
 
    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2); // 人脸中心坐标
        ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); // 椭圆
        Mat faceROI = frame_gray(faces[i]);
        std::vector<Rect> eyes;
        std::vector<Rect> noses;
        std::vector<Rect> mouths;
 
        //-- In each face, detect eyes、nose、mouth
        eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        nose_cascade.detectMultiScale(faceROI, noses, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
        mouth_cascade.detectMultiScale(faceROI, mouths, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
 
        // eyes
        Point eye_center;
        for (size_t j = 0; j < eyes.size(); j++)
        {
            eye_center = Point(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2); //眼睛的中心
            if (eye_center.x>faces[i].x && eye_center.y > faces[i].y) // 确保眼睛在脸上,其实前边检测时,已经保证了这一点
            {
                int radius = cvRound((eyes[j].width + eyes[j].height)*0.25); //取整
                circle(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
            }
        }
        // nose
        Point nose_center;
        if (noses.size() > 0)
        {
            nose_center = Point(faces[i].x + noses[0].x + noses[0].width / 2, faces[i].y + noses[0].y + noses[0].height / 2); //鼻子的中心
            if (nose_center.y > eye_center.y) //确保鼻子在眼睛下边
            {
                rectangle(frame, Point(faces[i].x + noses[0].x, faces[i].y+ noses[0].y), Point(faces[i].x + noses[0].x + noses[0].width, faces[i].y + noses[0].y + noses[0].height), Scalar(0, 255, 0), 3, 8, 0); //Point(noses[0].x, noses[0].y), Point(noses[0].x + noses[0].width, noses[0].y + noses[0].height)
                //int radius = cvRound((noses[0].width + noses[0].height)*0.25); //取整
                //circle(frame, nose_center, radius, Scalar(0, 255,0), 4, 8, 0);
                std::cout << "nose!\n";
            }
        }
        // mouth
        if (mouths.size() > 0)
        {
            Point mouth_center(faces[i].x + mouths[0].x + mouths[0].width / 2, faces[i].y + mouths[0].y + mouths[0].height / 2); //嘴巴的中心
            if (mouth_center.y > nose_center.y) // 确保嘴巴在鼻子下边
            {
                int radius = cvRound((mouths[0].width + mouths[0].height)*0.25); //取整
                circle(frame, mouth_center, radius, Scalar(0, 0, 255), 4, 8, 0);
                std::cout << "mouth!\n";
            }
            
        }
    }
    //-- Show what you got
    imshow(window_name, frame);
}

    运行结果:

            丑拒(#^.^#)

    由结果可看出,较好的检测出来人脸及人脸特征,其中,粉色区域为face、蓝色为eye、绿色为nose、红色为mouth。    

 但多次试验会发现,误判的概率很高,所以模型与程序尚有较大改进空间。

 注意:要对眼睛嘴巴鼻子的位置进行限定,可一定程度上减少误判。




 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值