溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

基于openCV實(shí)現(xiàn)人臉檢測

發(fā)布時(shí)間:2020-10-16 23:26:04 來源:腳本之家 閱讀:265 作者:liuph_ 欄目:編程語言

openCV的人臉識(shí)別主要通過Haar分類器實(shí)現(xiàn),當(dāng)然,這是在已有訓(xùn)練數(shù)據(jù)的基礎(chǔ)上。openCV安裝在 opencv/opencv/sources/data/haarcascades_cuda(或haarcascades)中存在預(yù)先訓(xùn)練好的物體檢測器(xml格式),包括正臉、側(cè)臉、眼睛、微笑、上半身、下半身、全身等。

openCV的的Haar分類器是一個(gè)監(jiān)督分類器,首先對(duì)圖像進(jìn)行直方圖均衡化并歸一化到同樣大小,然后標(biāo)記里面是否包含要監(jiān)測的物體。它首先由Paul Viola和Michael Jones設(shè)計(jì),稱為Viola Jones檢測器。Viola Jones分類器在級(jí)聯(lián)的每個(gè)節(jié)點(diǎn)中使用AdaBoost來學(xué)習(xí)一個(gè)高檢測率低拒絕率的多層樹分類器。它使用了以下一些新的特征:

1. 使用類Haar輸入特征:對(duì)矩形圖像區(qū)域的和或者差進(jìn)行閾值化。 
2. 積分圖像技術(shù)加速了矩形區(qū)域的45°旋轉(zhuǎn)的值的計(jì)算,用來加速類Haar輸入特征的計(jì)算。
3. 使用統(tǒng)計(jì)boosting來創(chuàng)建兩類問題(人臉和非人臉)的分類器節(jié)點(diǎn)(高通過率,低拒絕率)
4. 把弱分類器節(jié)點(diǎn)組成篩選式級(jí)聯(lián)。即,第一組分類器最優(yōu),能通過包含物體的圖像區(qū)域,同時(shí)允許一些不包含物體通過的圖像通過;第二組分

類器次優(yōu)分類器,也是有較低的拒絕率;以此類推。也就是說,對(duì)于每個(gè)boosting分類器,只要有人臉都能檢測到,同時(shí)拒絕一小部分非人臉,并將其傳給下一個(gè)分類器,是為低拒絕率。以此類推,最后一個(gè)分類器將幾乎所有的非人臉都拒絕掉,只剩下人臉區(qū)域。只要圖像區(qū)域通過了整個(gè)級(jí)聯(lián),則認(rèn)為里面有物體。

此技術(shù)雖然適用于人臉檢測,但不限于人臉檢測,還可用于其他物體的檢測,如汽車、飛機(jī)等的正面、側(cè)面、后面檢測。在檢測時(shí),先導(dǎo)入訓(xùn)練好的參數(shù)文件,其中haarcascade_frontalface_alt2.xml對(duì)正面臉的識(shí)別效果較好haarcascade_profileface.xml對(duì)側(cè)臉的檢測效果較好。當(dāng)然,如果要達(dá)到更高的分類精度,可以收集更多的數(shù)據(jù)進(jìn)行訓(xùn)練,這是后話。

以下代碼基本實(shí)現(xiàn)了正臉、眼睛、微笑、側(cè)臉的識(shí)別,若要添加其他功能,可以自行調(diào)整。

// 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  
//附加依賴項(xiàng): 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; 
} 

調(diào)整主函數(shù)中faceDetect(const char* imageName, int nNested, int nSmile, int nProfile)函數(shù)中的參數(shù),分別表示圖像文件名,是否檢測眼睛,是否檢測微笑,是否檢測側(cè)臉。以檢測正臉、眼睛為例:

基于openCV實(shí)現(xiàn)人臉檢測

再來看一張合影。

基于openCV實(shí)現(xiàn)人臉檢測

========華麗麗的分割線==========

如果對(duì)分類器的參數(shù)不滿意,或者說想識(shí)別其他的物體例如車、人、飛機(jī)、蘋果等等等等,只需要選擇適當(dāng)?shù)臉颖居?xùn)練,獲取該物體的各個(gè)方面的參數(shù),訓(xùn)練過程可以通過openCV的haartraining實(shí)現(xiàn)(參考haartraining參考文檔,opencv/apps/traincascade),主要包括個(gè)步驟:

1. 收集打算學(xué)習(xí)的物體數(shù)據(jù)集(如正面人臉圖,側(cè)面汽車圖等, 1000~10000個(gè)正樣本為宜),把它們存儲(chǔ)在一個(gè)或多個(gè)目錄下面。
2. 使用createsamples來建立正樣本的向量輸出文件,通過這個(gè)文件可以重復(fù)訓(xùn)練過程,使用同一個(gè)向量輸出文件嘗試各種參數(shù)。
3. 獲取負(fù)樣本,即不包含該物體的圖像。
4. 訓(xùn)練。命令行實(shí)現(xiàn)。

 以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI