#include #include "cv.h" // opencv #include "cvaux.h" // opencv #include #include #include #include "il.h" // image loading saving #include "windows.h" // openfilenamea #include "ParamIO.h" // xml saving #define ORIG_WIN_SIZE 24 // ---------------------------------------- char shortName[100]; char xmlName[100]; char detectImageName[100]; char handyStr[100]; ParamIO outXml; // our xml file static CvHidHaarClassifierCascade* hid_cascade = 0; // our opencv cascade // ---------------------------------------- train the default face detector int InitFaceDetect( const char* classifier_cascade_path ) { CvHaarClassifierCascade* cascade = cvLoadHaarClassifierCascade( classifier_cascade_path && strlen(classifier_cascade_path) > 0 ? classifier_cascade_path : "", cvSize( ORIG_WIN_SIZE, ORIG_WIN_SIZE )); if( !cascade ) return 0; hid_cascade = cvCreateHidHaarClassifierCascade( cascade ); cvReleaseHaarClassifierCascade( &cascade ); return 1; } // ---------------------------------------- find faces, draw them in an image, save xml void DetectAndDrawFaces( IplImage* img ) { if( hid_cascade && img ){ int i; CvMemStorage* storage = cvCreateMemStorage(); cvClearMemStorage( storage ); if( hid_cascade ){ CvSeq* faces = cvHaarDetectObjects( img, hid_cascade, storage, 1.1, 4, 1 ); int numFaces = (faces ? faces->total : 0); outXml.write ("params:face_data:num_faces", numFaces); for( i = 0; i < (faces ? faces->total : 0); i++ ){ CvRect r = *(CvRect*)cvGetSeqElem( faces, i ); sprintf(handyStr,"params:face_data:pt_%i:x", i); outXml.write (handyStr, r.x); sprintf(handyStr,"params:face_data:pt_%i:y", i); outXml.write (handyStr, r.y); sprintf(handyStr,"params:face_data:pt_%i:w", i); outXml.write (handyStr, r.width); sprintf(handyStr,"params:face_data:pt_%i:h", i); outXml.write (handyStr, r.height); cvRectangle( img, cvPoint(r.x,r.y), cvPoint((r.x+r.width),(r.y+r.height)), CV_RGB(0,255,0), 2 ); } } } } //----------------------------------------- int main(int argc, char* argv[]){ ilInit(); memset(shortName, 0, 100); bool saveResults = true; char FileName[1000]=""; char FileShortName[200]="a"; IplImage* image; int w = 0; int h = 0; FILE *infile; OPENFILENAME file; //----------------------------------------- choose the file to process char directory[300]; GetCurrentDirectory(MAX_PATH, directory); bool goodLoad; memset(&file,0,sizeof(OPENFILENAME)); file.lStructSize = sizeof(OPENFILENAME); file.Flags = OFN_HIDEREADONLY; file.lpstrFile = FileName; file.nMaxFile=1000; file.nMaxFileTitle = 200; file.lpstrFileTitle = FileShortName; goodLoad=GetOpenFileNameA(&file); // choose a file via "GetOpenFileNameA", could be passed in as well int len = strlen(file.lpstrFileTitle); len -= 4; // - ".jpg" if (len <= 0) printf("prob with short name \n"); memcpy(shortName, file.lpstrFileTitle, len); sprintf(xmlName, "%s.xml", shortName); sprintf(detectImageName, "%s_detect.jpg", shortName); unsigned int imageID; ilGenImages(1, &imageID); ilBindImage(imageID); //----------------------------------------- load the image file via devil if (ilLoadImage(FileName)){ w = ilGetInteger(IL_IMAGE_WIDTH); h = ilGetInteger(IL_IMAGE_HEIGHT); int bits = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL); if (bits != 24){ ilConvertImage(IL_RGB,IL_UNSIGNED_BYTE); } ILubyte *Data = ilGetData(); image = cvCreateImage( cvSize( w, h), IPL_DEPTH_8U, 3); cvSetImageData(image,Data,w*3); cvSetImageROI( image, cvRect(0,0,w,h)); InitFaceDetect(""); DetectAndDrawFaces(image); } else { printf("image load doesn't work! check image please \n"); return 0; } //----------------------------------------- save a "detect" image if (saveResults == true){ uchar* data = 0; int step = 0; CvSize size; cvGetRawData( image, &data, &step, &size ); ILubyte *Data2 = ilGetData(); memcpy(Data2, data, w * h * 3); ilEnable(IL_FILE_OVERWRITE); ilSaveImage(detectImageName); } //----------------------------------------- save xml outXml.writeFile(xmlName); // finally, save to file. return 0; } //-----------------------------------------