1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include "cv.h" // open cv general include file
#include "highgui.h" // open cv GUI include file
#include <stdio.h>
using namespace std;
// Create memory for calculations
static CvMemStorage* storage = 0;
// Create a new Haar classifier
static CvHaarClassifierCascade* cascade1 = 0;
static CvHaarClassifierCascade* cascade2 = 0;
static CvHaarClassifierCascade* cascade3 = 0;
static CvHaarClassifierCascade* cascade4 = 0;
static CvHaarClassifierCascade* cascade5 = 0;
void detect_and_draw (IplImage* img, int face)
{
// Structure of the rectangle
CvRect* haarDetect; // data for the object area
// Clear the memory storage which was used before
cvClearMemStorage(storage);
// Find whether the cascade is loaded, to find the faces. If yes, then:
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* objects1 = cvHaarDetectObjects(img, cascade1, storage, 1.1, 5, 0, cvSize( 40, 50));
CvSeq* objects2 = cvHaarDetectObjects(img, cascade2, storage, 1.1, 5, 0, cvSize( 40, 50));
CvSeq* objects3 = cvHaarDetectObjects(img, cascade3, storage, 1.1, 5, 0, cvSize( 40, 50));
CvSeq* objects4 = cvHaarDetectObjects(img, cascade4, storage, 1.1, 5, 0, cvSize( 40, 50));
CvSeq* objects5 = cvHaarDetectObjects(img, cascade5, storage, 1.1, 5, 0, cvSize( 40, 50));
int n1 = objects1?objects1->total:0;
int n2 = objects2?objects2->total:0;
int n3 = objects3?objects3->total:0;
int n4 = objects4?objects4->total:0;
int n5 = objects5?objects5->total:0;
printf("n1=%d | n2=%d | n3=%d | n4=%d | n5=%d\n", n1, n2, n3, n4, n5);
printf("---------------------------------\n");
// Loop the number of faces found
if (n5 == 0)
{
if (n4 == 0)
{
if (n3 == 0)
{
if (n2 == 0)
{
for (int i=0; i<n1; i++)
{
// Create a new rectangle for drawing the face
haarDetect = (CvRect*)cvGetSeqElem(objects1, i);
// Draw the rectangle in the input image
cvRectangle(img, cvPoint(haarDetect->x, haarDetect->y), cvPoint(haarDetect->x + haarDetect->width, haarDetect->y + haarDetect->height), CV_RGB(255, 0, 0));
}
}
if (n2 != 0)
{
for (int i=0; i<n2; i++)
{
// Create a new rectangle for drawing the face
haarDetect = (CvRect*)cvGetSeqElem(objects2, i);
// Draw the rectangle in the input image
cvRectangle(img, cvPoint(haarDetect->x, haarDetect->y), cvPoint(haarDetect->x + haarDetect->width, haarDetect->y + haarDetect->height), CV_RGB(0, 0, 0));
}
}
}
if (n3 != 0)
{
for (int i=0; i<n3; i++)
{
// Create a new rectangle for drawing the face
haarDetect = (CvRect*)cvGetSeqElem(objects3, i);
// Draw the rectangle in the input image
cvRectangle(img, cvPoint(haarDetect->x, haarDetect->y), cvPoint(haarDetect->x + haarDetect->width, haarDetect->y + haarDetect->height), CV_RGB(255, 0, 255));
}
}
}
if (n4 != 0)
{
for (int i=0; i<n4; i++)
{
// Create a new rectangle for drawing the face
haarDetect = (CvRect*)cvGetSeqElem(objects4, i);
// Draw the rectangle in the input image
cvRectangle(img, cvPoint(haarDetect->x, haarDetect->y), cvPoint(haarDetect->x + haarDetect->width, haarDetect->y + haarDetect->height), CV_RGB(0, 0, 255));
}
}
}
if (n5 != 0)
{
for (int i=0; i<n5; i++)
{
// Create a new rectangle for drawing the face
haarDetect = (CvRect*)cvGetSeqElem(objects5, i);
// Draw the rectangle in the input image
cvRectangle(img, cvPoint(haarDetect->x, haarDetect->y), cvPoint(haarDetect->x + haarDetect->width, haarDetect->y + haarDetect->height), CV_RGB(0, 255, 0));
}
}
if (n1==1 || n2==1 || n3==1 || n4==1 || n5==1)
{
face=1;
printf("face=%d\n", face);
}
else
face = 0;
}
int main(int argc, char** argv)
{
// Images from shot
IplImage* image = NULL; // image object
// Load the HaarClassifierCascade
cascade1 = (CvHaarClassifierCascade*)cvLoad("haarcascades/haarcascade_frontalface_alt.xml");
cascade2 = (CvHaarClassifierCascade*)cvLoad("haarcascades/haarcascade_frontalface_alt2.xml");
cascade3 = (CvHaarClassifierCascade*)cvLoad("haarcascades/haarcascade_frontalface_alt_tree.xml");
cascade4 = (CvHaarClassifierCascade*)cvLoad("haarcascades/haarcascade_frontalface_default.xml");
cascade5 = (CvHaarClassifierCascade*)cvLoad("haarcascades/haarcascade_profileface.xml");
// Allocate the memory storage
storage = cvCreateMemStorage(0);
// Variable for the main program
int i;
int imageId = 0;
int face = 0;
// Create window object (use flag=0 to allow resize, 1 to auto fix size)
cvNamedWindow("PROJECT AFRICA", 1);
cvNamedWindow("PROJECT AFRICA : Face detect", 1);
// Delay for GUI Window
char key;
int EVENT_LOOP_DELAY = 40;
printf(">>>> face detection : %s\n",argv[1]);
for (i=1; i<argc; i++)
{
// Create the input image
image = cvLoadImage(argv[i], CV_LOAD_IMAGE_COLOR);
imageId++;
printf("---------------------------------\n");
printf("image file %i : %s\n", i, argv[i]);
// Call the function to detect and draw the face
detect_and_draw(image, face);
// Display icon object in window
cvShowImage("Face detect", image);
// Start event processing loop
key = cvWaitKey(EVENT_LOOP_DELAY);
if (key == 'x')
break;
}
printf("<<<< face detection : %s\n",argv[1]);
// Release the image
cvReleaseImage(&image);
// Destroy the window
cvDestroyAllWindows();
// return 0 to indicate successfull execution of the program
return 0;
} |
Partager