Hi all.
I'm trying to use c code with opencv in face
detection and counting, but I cannot build the source.
I am trying to compile my project and I am
having a lot of problems with a line in the xutility file.
the error message show that it error with xutility file.
Please help me, how to solve this problem?
this is my code
// Include header files
#include "stdafx.h"
#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>
#include <fstream>
#include <vector>
using namespace std;
#ifdef _EiC
#define WIN32
#endif
int countfaces=0;
int numFaces = 0;
int k=0 ;
int list=0;
char filelist[512][512];
int timeCount = 0;
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
void detect_and_draw( IplImage* image );
void WriteInDB();
int found_face(IplImage* img,CvPoint pt1,CvPoint pt2);
int load_DB(char * filename);
const char* cascade_name = "C:\\Program Files\\OpenCV\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml";
// BEGIN NEW CODE
#define WRITEVIDEO
char* outputVideo = "c:\\face_counting1_tracked.avi";
//int faceCount = 0;
int posBuffer = 100;
int persistDuration = 10; //faces can drop out for 10 frames
int timestamp = 0;
float sameFaceDistThreshold = 30; //pixel distance
CvPoint facePositions[100];
int facePositionsTimestamp[100];
float distance( CvPoint a, CvPoint b ) {
float dist = sqrt(float ( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) );
return dist;
}
void expirePositions()
{
for (int i = 0; i < posBuffer; i++)
{
if (facePositionsTimestamp[i] <= (timestamp - persistDuration)) //if a tracked pos is older than three frames
{
facePositions[i] = cvPoint(999,999);
}
}
}
void updateCounter(CvPoint center)
{
bool newFace = true;
for(int i = 0; i < posBuffer; i++)
{
if (distance(center, facePositions[i]) < sameFaceDistThreshold)
{
facePositions[i] = center;
facePositionsTimestamp[i] = timestamp;
newFace = false;
break;
}
}
if(newFace)
{
//push out oldest tracker
for(int i = 1; i < posBuffer; i++)
{
facePositions[i] = facePositions[i - 1];
}
//put new tracked position on top of stack
facePositions[0] = center;
facePositionsTimestamp[0] = timestamp;
countfaces++;
}
}
void drawCounter(IplImage* image) {
// Create Font
char buffer[5];
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, .5, .5, 0, 1);
cvPutText(image, "Faces:", cvPoint(20, 20), &font, CV_RGB(0,255,0));
cvPutText(image, itoa(countfaces, buffer, 10), cvPoint(80, 20), &font, CV_RGB(0,255,0));
}
#ifdef WRITEVIDEO
CvVideoWriter* videoWriter = cvCreateVideoWriter(outputVideo, -1, 30, cvSize(240, 180));
#endif
//END NEW CODE
int main( int argc, char** argv )
{
CvCapture* capture = 0;
IplImage *frame, *frame_copy = 0;
int optlen = strlen("--cascade=");
const char* input_name;
if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
{
cascade_name = argv[1] + optlen;
input_name = argc > 2 ? argv[2] : 0;
}
else
{
cascade_name = "C:\\Program Files\\OpenCV\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml";
input_name = argc > 1 ? argv[1] : 0;
}
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>\" [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
capture = cvCaptureFromAVI( "c:\\face_counting1.avi" );
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( 30 ) >= 0 )
break;
}
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
}
else
{
if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0'))
cvNamedWindow( "result", 1 );
const char* filename = input_name ? input_name : (char*)"lena.jpg";
IplImage* image = cvLoadImage( filename, 1 );
if( image )
{
detect_and_draw( image );
cvWaitKey(0);
cvReleaseImage( &image );
}
else
{
/* assume it is a text file containing the
list of the image filenames to be processed - one per line */
FILE* f = fopen( filename, "rt" );
if( f )
{
char buf[1000+1];
while( fgets( buf, 1000, f ) )
{
int len = (int)strlen(buf);
while( len > 0 && isspace(buf[len-1]) )
len--;
buf[len] = '\0';
image = cvLoadImage( buf, 1 );
if( image )
{
detect_and_draw( image );
cvWaitKey(0);
cvReleaseImage( &image );
}
}
fclose(f);
}
}
}
cvDestroyWindow("result");
#ifdef WRITEVIDEO
cvReleaseVideoWriter(&videoWriter);
#endif
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}}
};
double scale = 1.3;
IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
cvRound (img->height/scale)),
8, 1 );
CvPoint pt1, pt2;
int i;
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_DO_CANNY_PRUNING*/,
cvSize(30, 30) );
t = (double)cvGetTickCount() - t;
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
if (faces)
{
//To save the detected faces into separate images, here's a quick and dirty code:
char filename[6];
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
/* CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
CvPoint center;
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, colors[i%8], 3, 8, 0 );*/
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
CvPoint center;
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, CV_RGB(255,0,0), 3, 8, 0 );
//update counter
updateCounter(center);
int y=found_face(img,pt1,pt2);
if(y==0)
countfaces++;
}//end for
printf("Number of detected faces: %d\t",countfaces);
}//end if
//delete old track positions from facePositions array
expirePositions();
timestamp++;
//draw counter
drawCounter(img);
#ifdef WRITEVIDEO
cvWriteFrame(videoWriter, img);
#endif
cvShowImage( "result", img );
cvDestroyWindow("Result");
cvReleaseImage( &gray );
cvReleaseImage( &small_img );
}//end if
} //end void
int found_face(IplImage* img,CvPoint pt1,CvPoint pt2)
{
/*if (faces)
{*/
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
int i=0;
char filename[512];
for( i = 0; i < (faces ? faces->total : 0); i++ )
{//int scale = 1, i=0;
//i=iface;
//char filename[512];
/* extract the rectanlges only */
// CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
//IplImage* gray_img = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );
IplImage* clone = cvCreateImage (cvSize(img->width, img->height),
IPL_DEPTH_8U, img->nChannels );
IplImage* gray = cvCreateImage (cvSize(img->width, img->height),
IPL_DEPTH_8U, 1 );
cvCopy (img, clone, 0);
cvNamedWindow ("ROI", CV_WINDOW_AUTOSIZE);
cvCvtColor( clone, gray, CV_RGB2GRAY );
face_rect.x = pt1.x;
face_rect.y = pt1.y;
face_rect.width = abs(pt1.x - pt2.x);
face_rect.height = abs(pt1.y - pt2.y);
cvSetImageROI ( gray, face_rect);
//// * rectangle = cvGetImageROI ( clone );
face_rect = cvGetImageROI ( gray );
cvShowImage ("ROI", gray);
k++;
char *name=0;
name=(char*) calloc(512, 1);
sprintf(name, "Image%d.pgm", k);
cvSaveImage(name, gray);
////////////////
for(int j=0;j<512;j++)
filelist[list][j]=name[j];
list++;
WriteInDB();
//int found=SIFT("result.txt",name);
cvResetImageROI( gray );
//return found;
return 0;
// }//end if
}//end for
}//end void
void WriteInDB()
{
ofstream myfile;
myfile.open ("result.txt");
for(int i=0;i<512;i++)
{
if(strcmp(filelist[i],"")!=0)
myfile << filelist[i]<<"\n";
}
myfile.close();
}
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 13 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files\microsoft visual studio 9.0\vc\include\xutility 766
Error 18 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files\microsoft visual studio 9.0\vc\include\xutility 768
Error 23 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files\microsoft visual studio 9.0\vc\include\xutility 769
Error 10 error C2868: 'std::iterator_traits<_Iter>::value_type' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility 765
Error 25 error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility 769
Error 20 error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility 768
Error 5 error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility 764
Error 15 error C2868: 'std::iterator_traits<_Iter>::difference_type' : illegal syntax for using-declaration; expected qualified-name c:\program files\microsoft visual studio 9.0\vc\include\xutility 766
Error 9 error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>' c:\program files\microsoft visual studio 9.0\vc\include\xutility 765
Error 24 error C2602: 'std::iterator_traits<_Iter>::reference' is not a member of a base class of 'std::iterator_traits<_Iter>' c:\program files\microsoft visual studio 9.0\vc\include\xutility 769
Error 19 error C2602: 'std::iterator_traits<_Iter>::pointer' is not a member of a base class of 'std::iterator_traits<_Iter>' c:\program files\microsoft visual studio 9.0\vc\include\xutility 768
Error 4 error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>' c:\program files\microsoft visual studio 9.0\vc\include\xutility 764
Error 14 error C2602: 'std::iterator_traits<_Iter>::difference_type' is not a member of a base class of 'std::iterator_traits<_Iter>' c:\program files\microsoft visual studio 9.0\vc\include\xutility 766
Error 7 error C2146: syntax error : missing ';' before identifier 'value_type' c:\program files\microsoft visual studio 9.0\vc\include\xutility 765
Error 22 error C2146: syntax error : missing ';' before identifier 'reference' c:\program files\microsoft visual studio 9.0\vc\include\xutility 769
Error 17 error C2146: syntax error : missing ';' before identifier 'pointer' c:\program files\microsoft visual studio 9.0\vc\include\xutility 768
Error 2 error C2146: syntax error : missing ';' before identifier 'iterator_category' c:\program files\microsoft visual studio 9.0\vc\include\xutility 764
Error 12 error C2146: syntax error : missing ';' before identifier 'difference_type' c:\program files\microsoft visual studio 9.0\vc\include\xutility 766
Error 6 error C2039: 'value_type' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility 765
Error 21 error C2039: 'reference' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility 769
Error 16 error C2039: 'pointer' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility 768
Error 1 error C2039: 'iterator_category' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility 764
Error 11 error C2039: 'difference_type' : is not a member of 'CvPoint' c:\program files\microsoft visual studio 9.0\vc\include\xutility 766