1. 程式人生 > >基於openCV實現人臉檢測

基於openCV實現人臉檢測

openCV的人臉識別主要通過Haar分類器實現,當然,這是在已有訓練資料的基礎上。openCV安裝在 opencv/opencv/sources/data/haarcascades_cuda(或haarcascades)中存在預先訓練好的物體檢測器(xml格式),包括正臉、側臉、眼睛、微笑、上半身、下半身、全身等。

openCV的的Haar分類器是一個監督分類器首先對影象進行直方圖均衡化並歸一化到同樣大小,然後標記裡面是否包含要監測的物體。它首先由Paul Viola和Michael Jones設計,稱為Viola Jones檢測器。Viola Jones分類器在級聯的每個節點中使用AdaBoost來學習一個高檢測率低拒絕率的多層樹分類器。它使用了以下一些新的特徵

1. 使用類Haar輸入特徵:對矩形影象區域的和或者差進行閾值化

 2. 積分影象技術加速了矩形區域的45°旋轉的值的計算,用來加速類Haar輸入特徵的計算。

3. 使用統計boosting來建立兩類問題(人臉和非人臉)的分類器節點(高通過率,低拒絕率)

4. 把弱分類器節點組成篩選式級聯。即,第一組分類器最優,能通過包含物體的影象區域,同時允許一些不包含物體通過的影象通過;第二組分類器次優分類器,也是有較低的拒絕率;以此類推。也就是說,對於每個boosting分類器,只要有人臉都能檢測到,同時拒絕一小部分非人臉,並將其傳給下一個分類器,是為低拒絕率。以此類推,最後一個分類器將幾乎所有的非人臉都拒絕掉,只剩下人臉區域。只要影象區域通過了整個級聯,則認為裡面有物體。

此技術雖然適用於人臉檢測,但不限於人臉檢測,還可用於其他物體的檢測,如汽車、飛機等的正面、側面、後面檢測。在檢測時,先匯入訓練好的引數檔案,其中haarcascade_frontalface_alt2.xml對正面臉的識別效果較好,haarcascade_profileface.xml對側臉的檢測效果較好。當然,如果要達到更高的分類精度,可以收集更多的資料進行訓練,這是後話。

以下程式碼基本實現了正臉、眼睛、微笑、側臉的識別,若要新增其他功能,可以自行調整。

// faceDetector.h
// This is just the face, eye, smile, profile detector from OpenCV's samples/c directory
//
/* *************** License:**************************
   Jul. 18, 2016
   Author: Liuph
   Right to use this code in any way you want without warranty, support or any guarantee of it working.   

   OTHER OPENCV SITES:
   * The source code is on sourceforge at:
     http://sourceforge.net/projects/opencvlibrary/
   * The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
     http://opencvlibrary.sourceforge.net/
   * An active user group is at:
     http://tech.groups.yahoo.com/group/OpenCV/
   * The minutes of weekly OpenCV development meetings are at:
     http://pr.willowgarage.com/wiki/OpenCV
   ************************************************** */

#include "cv.h"
#include "highgui.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <iostream>
using namespace std;


static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
static CvHaarClassifierCascade* nested_cascade = 0;
static CvHaarClassifierCascade* smile_cascade = 0;
static CvHaarClassifierCascade* profile = 0;
int use_nested_cascade = 0;

void detect_and_draw( IplImage* image );


/* The path that stores the trained parameter files.
   After openCv is installed, the file path is 
   "opencv/opencv/sources/data/haarcascades_cuda" or "opencv/opencv/sources/data/haarcascades" */
const char* cascade_name =
    "../faceDetect/haarcascade_frontalface_alt2.xml";
const char* nested_cascade_name =
    "../faceDetect/haarcascade_eye_tree_eyeglasses.xml";
const char* smile_cascade_name = 
	"../faceDetect/haarcascade_smile.xml";
const char* profile_name = 
	"../faceDetect/haarcascade_profileface.xml";
double scale = 1;

int faceDetector(const char* imageName, int nNested, int nSmile, int nProfile)
{
    CvCapture* capture = 0;
    IplImage *frame, *frame_copy = 0;
    IplImage *image = 0;
    const char* scale_opt = "--scale=";
    int scale_opt_len = (int)strlen(scale_opt);
    const char* cascade_opt = "--cascade=";
    int cascade_opt_len = (int)strlen(cascade_opt);
    const char* nested_cascade_opt = "--nested-cascade";
    int nested_cascade_opt_len = (int)strlen(nested_cascade_opt);
	const char* smile_cascade_opt = "--smile-cascade";
	int smile_cascade_opt_len = (int)strlen(smile_cascade_opt);
	const char* profile_opt = "--profile";
	int profile_opt_len = (int)strlen(profile_opt);
    int i;
    const char* input_name = 0;


	int opt_num = 7;
	char** opts = new char*[7];
	opts[0] = "compile_opencv.exe";
	opts[1] = "--scale=1";
	opts[2] = "--cascade=1";
	if (nNested == 1)
		opts[3] = "--nested-cascade=1";
	else
		opts[3] = "--nested-cascade=0";
	if (nSmile == 1)
		opts[4] = "--smile-cascade=1";
	else
		opts[4] = "--smile-cascade=0";
	if (nProfile == 1)
		opts[5] = "--profile=1";
	else
		opts[5] = "--profile=0";
	opts[6] = (char*)imageName;
	


    for( i = 1; i < opt_num; i++ )
    {
        if( strncmp( opts[i], cascade_opt, cascade_opt_len) == 0)
        {
			cout<<"cascade: "<<cascade_name<<endl;
		}
        else if( strncmp( opts[i], nested_cascade_opt, nested_cascade_opt_len ) == 0)
        {
            if( opts[i][nested_cascade_opt_len + 1] == '1')
			{
				cout<<"nested: "<<nested_cascade_name<<endl;
				nested_cascade = (CvHaarClassifierCascade*)cvLoad( nested_cascade_name, 0, 0, 0 );
			}
            if( !nested_cascade )
                fprintf( stderr, "WARNING: Could not load classifier cascade for nested objects\n" );
        }
        else if( strncmp( opts[i], scale_opt, scale_opt_len ) == 0 )
        {
			cout<< "scale: "<< scale<<endl;
            if( !sscanf( opts[i] + scale_opt_len, "%lf", &scale ) || scale < 1 )
                scale = 1;
        }
		else if (strncmp( opts[i], smile_cascade_opt, smile_cascade_opt_len ) == 0)
		{
			if( opts[i][smile_cascade_opt_len + 1] == '1')
			{
				cout<<"smile: "<<smile_cascade_name<<endl;
				smile_cascade = (CvHaarClassifierCascade*)cvLoad( smile_cascade_name, 0, 0, 0 );
			}
			if( !smile_cascade )
				fprintf( stderr, "WARNING: Could not load classifier cascade for smile objects\n" );
		}
		else if (strncmp( opts[i], profile_opt, profile_opt_len ) == 0)
		{
			if( opts[i][profile_opt_len + 1] == '1')
			{
				cout<<"profile: "<<profile_name<<endl;
				profile = (CvHaarClassifierCascade*)cvLoad( profile_name, 0, 0, 0 );
			}
			if( !profile )
				fprintf( stderr, "WARNING: Could not load classifier cascade for profile objects\n" );
		}
        else if( opts[i][0] == '-' )
        {
            fprintf( stderr, "WARNING: Unknown option %s\n", opts[i] );
        }
        else
		{
            input_name = imageName;
			printf("input_name: %s\n", imageName);
		}
    }

    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: facedetect [--cascade=\"<cascade_path>\"]\n"
        "   [--nested-cascade[=\"nested_cascade_path\"]]\n"
        "   [--scale[=<image scale>\n"
        "   [filename|camera_index]\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
    
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else if( input_name )
    {
        image = cvLoadImage( input_name, 1 );
        if( !image )
            capture = cvCaptureFromAVI( input_name );
    }
    else
        image = cvLoadImage( "../lena.jpg", 1 );

    cvNamedWindow( "result", 1 );

    if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            else
                cvFlip( frame, frame_copy, 0 );
            
            detect_and_draw( frame_copy );

            if( cvWaitKey( 10 ) >= 0 )
                goto _cleanup_;
        }

        cvWaitKey(0);
_cleanup_:
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {
        if( image )
        {
            detect_and_draw( image );
            cvWaitKey(0);
            cvReleaseImage( &image );
        }
        else if( input_name )
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            FILE* f = fopen( input_name, "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf), c;
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    printf( "file %s\n", buf ); 
                    image = cvLoadImage( buf, 1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        c = cvWaitKey(0);
                        if( c == 27 || c == 'q' || c == 'Q' )
                            break;
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }
    }
    
    cvDestroyWindow("result");

    return 0;
}

void detect_and_draw( IplImage* img )
{
    static CvScalar colors[] = 
    {
        {{0,0,255}},
        {{0,128,255}},
        {{0,255,255}},
        {{0,255,0}},
        {{255,128,0}},
        {{255,255,0}},
        {{255,0,0}},
        {{255,0,255}}
    };

    IplImage *gray, *small_img;
    int i, j;

    gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
    small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
                         cvRound (img->height/scale)), 8, 1 );

    cvCvtColor( img, gray, CV_BGR2GRAY );
    cvResize( gray, small_img, CV_INTER_LINEAR );
    cvEqualizeHist( small_img, small_img );
    cvClearMemStorage( storage );

    if( cascade )
    {
        double t = (double)cvGetTickCount();
        CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
                                            1.1, 2, 0
                                            //|CV_HAAR_FIND_BIGGEST_OBJECT
                                            //|CV_HAAR_DO_ROUGH_SEARCH
                                            |CV_HAAR_DO_CANNY_PRUNING
                                            //|CV_HAAR_SCALE_IMAGE
                                            ,
                                            cvSize(30, 30) );
        t = (double)cvGetTickCount() - t;
        printf( "faces detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (faces ? faces->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
            CvMat small_img_roi;
            CvSeq* nested_objects;
			CvSeq* smile_objects;
            CvPoint center;
            CvScalar color = colors[i%8];
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            cvCircle( img, center, radius, color, 3, 8, 0 );

			//eye
            if( nested_cascade != 0)
            {
				cvGetSubRect( small_img, &small_img_roi, *r );
				nested_objects = cvHaarDetectObjects( &small_img_roi, nested_cascade, storage,
					1.1, 2, 0
					//|CV_HAAR_FIND_BIGGEST_OBJECT
					//|CV_HAAR_DO_ROUGH_SEARCH
					//|CV_HAAR_DO_CANNY_PRUNING
					//|CV_HAAR_SCALE_IMAGE
					,
					cvSize(0, 0) );
				for( j = 0; j < (nested_objects ? nested_objects->total : 0); j++ )
				{
					CvRect* nr = (CvRect*)cvGetSeqElem( nested_objects, j );
					center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
					center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
					radius = cvRound((nr->width + nr->height)*0.25*scale);
					cvCircle( img, center, radius, color, 3, 8, 0 );
				}
			}
			//smile
			if (smile_cascade != 0)
			{
				cvGetSubRect( small_img, &small_img_roi, *r );
				smile_objects = cvHaarDetectObjects( &small_img_roi, smile_cascade, storage,
					1.1, 2, 0
					//|CV_HAAR_FIND_BIGGEST_OBJECT
					//|CV_HAAR_DO_ROUGH_SEARCH
					//|CV_HAAR_DO_CANNY_PRUNING
					//|CV_HAAR_SCALE_IMAGE
					,
					cvSize(0, 0) );
				for( j = 0; j < (smile_objects ? smile_objects->total : 0); j++ )
				{
					CvRect* nr = (CvRect*)cvGetSeqElem( smile_objects, j );
					center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
					center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
					radius = cvRound((nr->width + nr->height)*0.25*scale);
					cvCircle( img, center, radius, color, 3, 8, 0 );
				}
			}
        }
    }

	if( profile )
	{
		double t = (double)cvGetTickCount();
		CvSeq* faces = cvHaarDetectObjects( small_img, profile, storage,
			1.1, 2, 0
			//|CV_HAAR_FIND_BIGGEST_OBJECT
			//|CV_HAAR_DO_ROUGH_SEARCH
			|CV_HAAR_DO_CANNY_PRUNING
			//|CV_HAAR_SCALE_IMAGE
			,
			cvSize(30, 30) );
		t = (double)cvGetTickCount() - t;
		printf( "profile faces detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
		for( i = 0; i < (faces ? faces->total : 0); i++ )
		{
			CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
			CvMat small_img_roi;
			CvSeq* nested_objects;
			CvSeq* smile_objects;
			CvPoint center;
			CvScalar color = colors[(7-i)%8];
			int radius;
			center.x = cvRound((r->x + r->width*0.5)*scale);
			center.y = cvRound((r->y + r->height*0.5)*scale);
			radius = cvRound((r->width + r->height)*0.25*scale);
			cvCircle( img, center, radius, color, 3, 8, 0 );

			//eye
			if( nested_cascade != 0)
			{
				cvGetSubRect( small_img, &small_img_roi, *r );
				nested_objects = cvHaarDetectObjects( &small_img_roi, nested_cascade, storage,
					1.1, 2, 0
					//|CV_HAAR_FIND_BIGGEST_OBJECT
					//|CV_HAAR_DO_ROUGH_SEARCH
					//|CV_HAAR_DO_CANNY_PRUNING
					//|CV_HAAR_SCALE_IMAGE
					,
					cvSize(0, 0) );
				for( j = 0; j < (nested_objects ? nested_objects->total : 0); j++ )
				{
					CvRect* nr = (CvRect*)cvGetSeqElem( nested_objects, j );
					center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
					center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
					radius = cvRound((nr->width + nr->height)*0.25*scale);
					cvCircle( img, center, radius, color, 3, 8, 0 );
				}
			}
			//smile
			if (smile_cascade != 0)
			{
				cvGetSubRect( small_img, &small_img_roi, *r );
				smile_objects = cvHaarDetectObjects( &small_img_roi, smile_cascade, storage,
					1.1, 2, 0
					//|CV_HAAR_FIND_BIGGEST_OBJECT
					//|CV_HAAR_DO_ROUGH_SEARCH
					//|CV_HAAR_DO_CANNY_PRUNING
					//|CV_HAAR_SCALE_IMAGE
					,
					cvSize(0, 0) );
				for( j = 0; j < (smile_objects ? smile_objects->total : 0); j++ )
				{
					CvRect* nr = (CvRect*)cvGetSeqElem( smile_objects, j );
					center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
					center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
					radius = cvRound((nr->width + nr->height)*0.25*scale);
					cvCircle( img, center, radius, color, 3, 8, 0 );
				}
			}
		}
	}

    cvShowImage( "result", img );
    cvReleaseImage( &gray );
    cvReleaseImage( &small_img );
}
//main.cpp
//openCV配置
//附加包含目錄: include, include/opencv, include/opencv2
//附加庫目錄: lib 
//附加依賴項: debug:-->  opencv_calib3d243d.lib;...;
//			release:--> opencv_calib3d243.lib;...;

#include<string>
#include <opencv2\opencv.hpp>

#include "CV2_compile.h"
#include "CV_compile.h"

#include "face_detector.h"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	const char* imagename = "../lena.jpg";
	faceDetector(imagename,1,0,0);

	return 0;
}

調整主函式中faceDetect(const char* imageName, int nNested, int nSmile, int nProfile)函式中的引數,分別表示影象檔名,是否檢測眼睛,是否檢測微笑,是否檢測側臉。以檢測正臉、眼睛為例:

再來看一張合影

===========================================================================================

華麗麗的分割線

===========================================================================================

如果對分類器的引數不滿意,或者說想識別其他的物體例如車、人、飛機、蘋果等等等等,只需要選擇適當的樣本訓練,獲取該物體的各個方面的引數,訓練過程可以通過openCV的haartraining實現(參考haartraining參考文件,opencv/apps/traincascade),主要包括個步驟:

1. 收集打算學習的物體資料集(如正面人臉圖,側面汽車圖等, 1000~10000個正樣本為宜),把它們儲存在一個或多個目錄下面。

2. 使用createsamples來建立正樣本的向量輸出檔案,通過這個檔案可以重複訓練過程,使用同一個向量輸出檔案嘗試各種引數。

3. 獲取負樣本,即不包含該物體的影象。

4. 訓練。命令列實現。

  後更。