Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

OpenCV upper body and lower body detection.

Status
Not open for further replies.

Cheetos

Member level 3
Joined
May 26, 2011
Messages
57
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,700
Greetings, i am having problems with openCV face, upper body and lower body detection. in my code, i can only see the face detection, the rest it does not appear. what is wrong with my code?

This is my code:


#include <stdio.h>
#include "cv.h"
#include "highgui.h"
CvHaarClassifierCascade *cascade;
CvMemStorage *storage;
CvHaarClassifierCascade *upperbodyCascade;
CvMemStorage *upperbodyStorage;
CvHaarClassifierCascade *lowerbodyCascade;
CvMemStorage *lowerbodyStorage;
void detectFaces( IplImage *img );
void detectupperbody( IplImage *img );
void detectlowerbody( IplImage *img );
IplImage *bframe = 0;
IplImage *resframe=0;

int main( int argc, char** argv )
{
CvCapture *capture;
IplImage *frame;
int key;
char *filename = "haarcascade_frontalface_alt.xml";
char *upperbodyFilename ="haarcascade_fullbody.xml";
char *lowerbodyFilename ="haarcascade_lowerbody.xml";

/* load the classifier
note that I put the file in the same directory with
this code */
cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );
upperbodyCascade = ( CvHaarClassifierCascade* )cvLoad( upperbodyFilename, 0, 0, 0 );
lowerbodyCascade = ( CvHaarClassifierCascade* )cvLoad( lowerbodyFilename, 0, 0, 0 );


/* setup memory buffer; needed by the face detector */
storage = cvCreateMemStorage( 0 );
upperbodyStorage = cvCreateMemStorage( 0 );
lowerbodyStorage = cvCreateMemStorage( 0 );
/* initialize camera */
capture = cvCaptureFromCAM( 0 );
/* always check */
assert( cascade && storage && capture );
assert( upperbodyCascade && upperbodyStorage && capture );
assert( lowerbodyCascade && lowerbodyStorage && capture );

/* create a window */
cvNamedWindow( "video", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "upperbody", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "lowerbody",CV_WINDOW_AUTOSIZE );
while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
if( !frame ) break;
/* 'fix' frame */
cvFlip( frame, frame, 1 );
frame->origin = 0;
/* detect faces and display video */
detectFaces(frame);
detectupperbody(frame);// FUNCTION IRE
detectlowerbody(frame); // FUNCTION IRE
/* quit if user press 'q' */
key = cvWaitKey( 10 );
}
/* free memory */
cvReleaseCapture( &capture );
cvDestroyWindow( "video" );
cvDestroyWindow( "upperbody" );
cvDestroyWindow( "lowerbody" );
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
return 0;
}
void detectFaces( IplImage *img )
{
int i;
/* detect faces */
CvSeq *faces = cvHaarDetectObjects(
img,
cascade,
storage,
1.1,
3,
0 /*CV_HAAR_DO_CANNY_PRUNNING*/,
cvSize( 40, 40 ) );

/* for each face found, draw a red box */

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 );

}



/* display video */

cvShowImage( "video", img );
}

void detectupperbody( IplImage *img1 )
{
int i;

/* detect faces */
CvSeq *body = cvHaarDetectObjects(
img1,
upperbodyCascade,
upperbodyStorage,
1.2,
2,
1/*CV_HAAR_DO_CANNY_PRUNNING*/,
cvSize( 0, 0 ) );

/* for each face found, draw a red box */
for( i = 0 ; i < ( body ? body->total : 0 ) ; i++ ) {
CvRect *r = ( CvRect* )cvGetSeqElem( body, i );
cvRectangle( img1,
cvPoint( r->x, r->y ),
cvPoint( r->x + r->width, r->y + r->height ),
CV_RGB( 255, 255, 255 ), 1, 8, 0 );
}


/* display video */
cvShowImage( "upperbody", img1 );
//cvWaitKey(30);
}
void detectlowerbody( IplImage *img1 )
{
int i;

/* detect faces */
CvSeq *body = cvHaarDetectObjects(
img1,
lowerbodyCascade,
lowerbodyStorage,
1.2,
2,
1/*CV_HAAR_DO_CANNY_PRUNNING*/,
cvSize( 1/4, 1/4 ) );


for( i = 0 ; i < ( body ? body->total : 0 ) ; i++ ) {
CvRect *r = ( CvRect* )cvGetSeqElem( body, i );
cvRectangle( img1,
cvPoint( r->x, r->y ),
cvPoint( r->x + r->width, r->y + r->height ),
CV_RGB( 0, 255,0 ), 1, 8, 0 );
}

/* display video */
cvShowImage( "lowerbody", img1 );
//cvWaitKey(30);
}
 

Hi! Thank you for a great and very helpful post! :>

I followed your post and used it for eye detection only. An debug error is occurring as I debug it. The prompted message suggests to retry. And so that's what I did. A breakpoint message would then be prompted. When I clicked the "continue" button it would then prompt (through the command prompt) "Assertion failed: cascade && storage && capture, file c:\users\sony vaio\documents\visual studio 2010\project\eyetracker\eyetracker\eyetracker.cpp, line 52"

Here is my code. I hope you could help me. :|


#include <stdafx.h>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>

CvHaarClassifierCascade *cascade;
CvMemStorage *storage;

void detecteyes( IplImage *img );

//int X;

//IplImage *resframe=0;

int main( int argc, char** argv ){
CvCapture *capture;
IplImage *frame;
int key;
char *filename = "haarcascade_eyes.xml";

// IplImage *bframe=0;


/* load the classifier
note that I put the file in the same directory with
this code */
// bframe = cvLoadImage("PICTURE.jpg",CV_LOAD_IMAGE_COLOR);
cascade = ( CvHaarClassifierCascade* )cvLoad( filename, 0, 0, 0 );


/* setup memory buffer; needed by the face detector */
storage = cvCreateMemStorage( 0 );


/* initialize camera */
capture = cvCaptureFromCAM(0);
/* always check */
assert(cascade && storage && capture );


/* create a window */
cvNamedWindow( "video", 1 );



while( key != 'q' ) {
/* get a frame */
frame = cvQueryFrame( capture );
/* always check */
// if( !frame ) break;
/* 'fix' frame */
// cvFlip( frame, frame, 1 );
// frame->origin = 0;
/* detect faces and display video */
detecteyes(frame);

/* quit if user press 'q' */
key = cvWaitKey( 50 );
}
/* free memory */
cvReleaseImage(&frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "video" );

cvReleaseHaarClassifierCascade( &cascade );

cvReleaseMemStorage( &storage );
return 0;
}

void detecteyes(IplImage* img)
{
int i,x;
x=0;

CvSeq *eyes = cvHaarDetectObjects(
img,
cascade,
storage,
1.1,
3,
0,
cvSize(10,10)


);

for( i=0; i < (eyes ? eyes->total : 0) ; i++){

CvRect *r = ( CvRect* )cvGetSeqElem( eyes, 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);

x=x+1;
printf("eyes detected %d",x);
}
cvShowImage( "video", img );
}
 

I did that but it did not make any difference. Is there a chance that there is a problem in the directory for xml files? I mean this has happened to me before when I was still trying to get a feed from a webcam. The problem to that though was in the location of the debug directory. Is there a chance that this may have a similar problem but with regards to using xml files?

Thank you for the reply.
 

i compiled your code, it gave no errors. I tried to run it without a webcam, i received the same error as you did.

There are two things you can do to solve this problem

1. Get a new webcam and try the code
2. Try modifying the number in this part of the code capture = cvCaptureFromCAM(0);
try playing with 0,1,2,3 and so on.
if you use 0, openCV will use the first webcam installed in your computer
1, for second 2, for third and so on.
 

I tried doing the things you said but unfortunately the problem still occurs. I tried putting a print function after the classifier loader to check whether or not it can load the .xml file. Through that I was able to justify that my program can now load the classifier. The problem still though is the assertion. When I run the program line per line, it showed an assertion failure, similar to the problem above. Now, I really don't have a clue anymore of what my problem is.

---------- Post added at 12:45 ---------- Previous post was at 12:39 ----------

I then tried to comment out first the assert line but breakpoints continue to prompt. When I clicked the break button, there was a message in the command prompt that states null array pointer is passed.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top