Opencv realizes face detection of pictures and video streams (cameras)

This article will implement opencv face detection, starting with the simplest image detection face.


In opencv, there are mainly  Haar features and LBP features for face detection.

Opencv comes with a trained classifier. There are three folders "lbpcascades", "haarcascades", and "hogcascades" in the data directory of the source code, which represent three different types of "haar", "hog" and "lbp" respectively. The classifier trained on the features . The "haar" feature is mainly used for face detection, the "hog" feature is mainly used for pedestrian detection, and the "lbp" feature is mainly used for face recognition. For example, under "haarcascades" are a large number of training files for different targets, as follows:


The main purpose of each file can also be seen from the file name .

face detector (default): haarcascade_frontalface_default.xml 
face detector (fast Harr): haarcascade_frontalface_alt2.xml 
face detector (side view): haarcascade_profileface.xml 
eye detector (left eye): haarcascade_lefteye_2splits.xml 
eye detection Detector (Right  Eye): haarcascade_righteye_2splits.xml
Mouth Detector: haarcascade_mcs_mouth.xml 
Nose Detector: haarcascade_mcs_nose.xml 
Body Detector 
: haarcascade_fullbody.xml Face Detector (Fast LBP): lbpcascade_frontalface.xml

In this article, we will use "haarcascade_frontalface_alt2.xml" to perform face detection on the above image.


First, apply the " CascadeClassifier" instantiation

CascadeClassifier faceCascade;

Load the classifier :

bool CascadeClassifier::load( const String& filename )

Face Detection:

void CascadeClassifier::detectMultiScale( InputArray image,
                      CV_OUT std::vector<Rect>& objects,
                      double scaleFactor,
                      int minNeighbors, int flags, int
                      Size minSize,
                      Size maxSize )

The parameters are as follows:

  • InputArray image: The image to be detected (grayscale)
  • vector<Rect>& objects: Save the detected face position coordinate sequence
  • double scaleFactor: the ratio of each image zoom
  • int minNeighbors: how many times each face must be detected to be considered a real face
  • int flags: decides whether to scale the classifier for detection, or scale the image
  • Size minSize : Indicates the minimum size of the face
  • Size maxSize : Indicates the maximum size of the face


1. Face detection in pictures

Code:

#include<opencv2/objdetect/objdetect.hpp>  
#include<opencv2/highgui/highgui.hpp>  
#include<opencv2/imgproc/imgproc.hpp>  
  
using namespace cv;  
using namespace std;
  
CascadeClassifier faceCascade;  
  
intmain()  
{  
    faceCascade.load("/root/library/opencv/opencv-3.4.0/data/haarcascades/haarcascade_frontalface_alt2.xml"); //load the classifier
  
    Mat img = imread("myImage.JPEG"); // load image
    Mat imgGray;  
    vector<Rect> faces;  
  
    if(img.empty())     
    {  
      return 1;  
    }  
  
    if(img.channels() ==3)  
    {  
       cvtColor(img, imgGray, CV_RGB2GRAY); // Convert RGB to grayscale
    }  
    else  
    {  
       imgGray = img; // do not convert
    }  
  
    faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0)); // detect faces
  
    if(faces.size()>0)  
    {  
       for(int i =0; i<faces.size(); i++)  
       {  
           rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 255, 0), 1, 8);    // 框出人脸
       }  
    }  
  
    imshow("FacesOfPrettyGirl", img); // show picture
  
    waitKey(0);  
    return 0;  
}  

Compile and run:



2. Face detection in video (camera)

    The face detection in the video is similar to the picture, the same process.

    The video can be regarded as a frame of pictures, and the above-mentioned picture detection can be performed on each frame, and the cycle is continuously repeated.

Code:

#include<opencv2/objdetect/objdetect.hpp>  
#include<opencv2/highgui/highgui.hpp>  
#include<opencv2/imgproc/imgproc.hpp>  
#include <iostream>

using namespace cv;  
using namespace std;
  
CascadeClassifier faceCascade;  
  
intmain()  
{  
    faceCascade.load("/root/library/opencv/opencv-3.4.0/data/haarcascades/haarcascade_frontalface_alt2.xml");

	VideoCapture capture;
	capture.open(1); // open the camera
// capture.open("video.avi"); // open the video
	if(!capture.isOpened())
	{
	  cout << "open camera failed. " << endl;
	  return -1;
	}

    Mat img, imgGray;
    vector<Rect> faces;
	while(1)
	{
		capture >> img; // read image to img
		if(img.empty())	
		{
			continue;
		}

		if(img.channels() == 3)	
		{  
		   cvtColor(img, imgGray, CV_RGB2GRAY);  
		}  
		else  
		{  
		   imgGray = img;  
		}  

	    faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0)); // detect faces
		
		if(faces.size()>0)	
		{  
		   for(int i =0; i<faces.size(); i++)  
		   {  
			   rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 255, 0), 1, 8);	
		   }  
		}  
		
		imshow("CamerFace", img); // show
		
		if(waitKey(1) > 0) // delay ms to wait for the key to exit
		{
			break;
		}
	}
  
    return 0;  
}  

Compile and run:


The video is a bit stuck, and it will be optimized later.


Optimization reference:

Blog: [ Optimization of face detection in opencv video stream (camera) ]


Note: Refer to the blog   http://blog.csdn.net/lsq2902101015/article/details/47057081         http://blog.csdn.net/chaipp0607/article/details/54234663

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325523152&siteId=291194637