Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello guys, i've been using opencv and visual studio to try a face detection program and it works. My question is, whether the front and side faces (facial profile) can be detected simultaneously (at one time in one program)? i already called the lbpcascade_profileface.xml, and then i used the haarcascade_profileface.xml but the program doesn't run properly. and sometimes even the frontal face is undetectable. any suggest please? thank you
Posted

You should know that the outline of profile faces is not easy to detect with haar like features or LBP features ,thus I suggest you use HOG plus SVM for profile faces, below is a code I think should atleast work but uses haar cascade classifiers.

C++
#include "opencv2/opencv.hpp"

using namespace cv;

CvHaarClassifierCascade *cascade,*cascade_profile; 

CvMemStorage *storage; 

void detectFaces( IplImage *img ); 

 int main( ) 
 { 
	 CvCapture *capture; IplImage *frame = 0; 
	 int key; 
	 char *filename = "haarcascade_frontalface_alt.xml"; 
         char *filename_profile = "haarcascade_profileface.xml"; 

	  capture = cvCreateCameraCapture(0);

	  storage = cvCreateMemStorage(0);

	  cascade = (CvHaarClassifierCascade*)cvLoad( filename,storage, 0, 0 );
          cascade_profile = (CvHaarClassifierCascade*)cvLoad( filename_profile,storage, 0, 0 );

	 assert( cascade && storage && capture && cascade_profile); 
	 cvNamedWindow( "video", 1 ); 
	 while( capture ) 
	 { 
		 frame = cvQueryFrame( capture ); 
		 if( !frame ) 
		 { 
			 fprintf( stderr, "Cannot query frame!\n" ); 
			 break; 
		 } 
		
		 detectFaces( frame ); 
                 detectFacesProfile( frame ); 
                 cvShowImage( "video", frame );
                 key = cvWaitKey( 25 ); 
	 } 
	 
	 cvReleaseCapture( &capture ); 
	 cvDestroyWindow( "video" ); 
	 
	 cvReleaseHaarClassifierCascade( &cascade ); 
         cvReleaseHaarClassifierCascade( &cascade_profile ); 
	 cvReleaseMemStorage( &storage ); 
	 return 0; 
 } 
 
 void detectFaces( IplImage *img ) 
 { 
	 int i; 
	 CvSeq *faces = cvHaarDetectObjects(img,cascade,storage,1.15,3,0,cvSize( 20, 20 ) ); 
	 for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) 
	 { 
		 CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
		 cvRectangle( img,cvPoint( r->x, r->y ),cvPoint( r->x + r->width, r->y + r->height ),CV_RGB( 255, 0, 0 ), 1, 8, 0 ); 
	 } 

	 cvClearMemStorage( storage );
 }

void detectFacesProfile( IplImage *img ) 
 { 
	 int i; 
	 CvSeq *faces = cvHaarDetectObjects(img,cascade_profile,storage,1.15,3,0,cvSize( 20, 20 ) ); 
	 for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) 
	 { 
		 CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
		 cvRectangle( img,cvPoint( r->x, r->y ),cvPoint( r->x + r->width, r->y + r->height ),CV_RGB( 255, 0, 0 ), 1, 8, 0 ); 
	 } 

	 cvClearMemStorage( storage );
 }
 
Share this answer
 
v2
private Bitmap DetectFace(Bitmap faceImage)
        {
            var image = new Image<Bgr, byte>(faceImage);
            var gray = image.Convert<Gray, Byte>();
            var haarCascadeFilePath = _httpContext.Server.MapPath("~/haarcascade_profileface.xml");
            var face = new CascadeClassifier(haarCascadeFilePath);

            Rectangle[] facesDetected = face.DetectMultiScale(gray, 1.1, 2 , new Size(20,20));
            Total_faces = facesDetected.Length;
            Image<Gray, byte> result = null;
            if (Total_faces == 0)
            {
                var haarCascadeFilePath_tree = _httpContext.Server.MapPath("~/haarcascade_frontalface_alt_tree.xml");
                var face_tree = new CascadeClassifier(haarCascadeFilePath_tree);
                Rectangle[] facesDetected_tree = face_tree.DetectMultiScale(gray, 1.1, 2, new Size(20, 20));
                Total_faces = facesDetected_tree.Length;
                if (Total_faces == 0)
                {

                }else { 
                image.Draw(facesDetected_tree[0], new Bgr(Color.Blue), 2);
                result = image.Copy(facesDetected_tree[0]).Convert<Gray, byte>();

                if (result != null)
                {
                    result = result.Resize(100, 100, Inter.Cubic);

                    return result.Bitmap;
                }
                }
            }
            else
            {
                image.Draw(facesDetected[0], new Bgr(Color.Blue), 2);
                result = image.Copy(facesDetected[0]).Convert<Gray, byte>();

                if (result != null)
                {
                    result = result.Resize(100, 100, Inter.Cubic);

                    return result.Bitmap;
                }
            }
            

            return null;
        }
 
Share this answer
 
Comments
Richard Deeming 11-Sep-18 14:25pm    
The question is tagged "C++".

Does the code in your solution look anything like C++ to you?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900