Face Detection using javacv

1,080 views
Skip to first unread message

Meaw

unread,
Sep 19, 2013, 9:27:46 PM9/19/13
to jav...@googlegroups.com
Hi, I have run this code and it is only detect the frontal faces, I am doing my final year research project and I want to detect the different position of the face to detect.
The code I have use is...

package UWU_CST_09_0039.UniversityProject;

import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacpp.Loader;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;


public class FaceDetection
{
  private static final int SCALE = 2;    
  // scaling factor to reduce size of input image

  // cascade definition for face detection
  private static final String CASCADE_FILE = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";

  private static final String OUT_FILE = "images\\markedFacesImage.jpg";


  public static void main(String[] args)
  {
     
      args[0]="images\\StitchImage.jpg";
      if (args.length != 1) {
      System.out.println("Usage: run FaceDetection <input-file>");
      return;
      }

      System.out.println("Starting OpenCV...");

     // preload the opencv_objdetect module to work around a known bug
     Loader.load(opencv_objdetect.class);

     // load an image
     System.out.println("Loading image from " + args[0]);
     IplImage origImg = cvLoadImage(args[0]);

     // convert to grayscale
     IplImage grayImg = IplImage.create(origImg.width(), origImg.height(), IPL_DEPTH_8U, 1);
     cvCvtColor(origImg, grayImg, CV_BGR2GRAY); 

     // scale the grayscale (to speed up face detection)
     IplImage smallImg = IplImage.create(grayImg.width()/SCALE,
                                        grayImg.height()/SCALE, IPL_DEPTH_8U, 1);
     cvResize(grayImg, smallImg, CV_INTER_LINEAR);

     // equalize the small grayscale
     IplImage equImg = IplImage.create(smallImg.width(),
                                      smallImg.height(), IPL_DEPTH_8U, 1);
     cvEqualizeHist(smallImg, equImg);

     // create temp storage, used during object detection
     CvMemStorage storage = CvMemStorage.create();

   
     CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));
     System.out.println("Detecting faces...");
    
     CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING);
     cvClearMemStorage(storage);

     // iterate over the faces and draw yellow rectangles around them
     int total = faces.total();
     System.out.println("Found " + total + " face(s)");
    
     for (int i = 0; i < total; i++) {
        CvRect r = new CvRect(cvGetSeqElem(faces, i));
        // cvSetImageROI(equImg,r);
        cvRectangle(origImg, cvPoint( r.x()*SCALE, r.y()*SCALE ),    // undo the scaling
                    cvPoint( (r.x() + r.width())*SCALE, (r.y() + r.height())*SCALE ),
                    CvScalar.YELLOW, 6, CV_AA, 0);
     }

    if (total > 0) {
      System.out.println("Saving marked-faces version of " + args[0] + " in " + OUT_FILE);
      cvSaveImage(OUT_FILE, origImg);
    
     
      final IplImage image = cvLoadImage(OUT_FILE);
     
     
      // Create image window named "My Image".
      //
      // Note that you need to indicate to CanvasFrame not to apply gamma correction,
      // by setting gamma to 1, otherwise the image will not look correct.
      final CanvasFrame canvas = new CanvasFrame("My Image", 1);
     
     
      // Request closing of the application when the image window is closed.
      canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
             
      // Show image on window.
      canvas.showImage(image);
      
    }
  }  // end of main()
}


I have found this code with the link..           http://code.google.com/p/javacv/wiki/Windows7AndOpenCV

and Is that can detect the various position of a face? And it cannot be run as an java application.It require Run as configuration...
Can you please consider about this issue please....Thank you

Meaw

unread,
Sep 21, 2013, 9:19:07 AM9/21/13
to jav...@googlegroups.com
the error run as configure is ok now...that was an error with the code..but it gives this error..


OpenCV Error: Null pointer (Null storage pointer) in unknown function, file ..\..\..\src\opencv\modules\objdetect\src\haar.cpp, line 1517
Exception in thread "main" java.lang.RuntimeException: ..\..\..\src\opencv\modules\objdetect\src\haar.cpp:1517: error: (-27) Null storage pointer

    at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method)
    at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:244)
    at detection.detectEyes(detection.java:130)
    at detection.main(detection.java:170)


 detection.main(detection.java:170)-------------------> is       detectEyes(img,r);
  detection.detectEyes(detection.java:130)--------------------> is      eyes = cvHaarDetectObjects( img,cascade_e, storage, 1.1, 3,CV_HAAR_DO_CANNY_PRUNING  );

whether I have set the haarCascade path correctly to the position...
Can you please consider this please..thank you

Samuel Audet

unread,
Sep 21, 2013, 9:37:25 AM9/21/13
to jav...@googlegroups.com
On 09/20/2013 10:27 AM, Meaw wrote:
> Hi, I have run this code and it is only detect the frontal faces, I am
> doing my final year research project and I want to detect the different
> position of the face to detect.
> The code I have use is...

You'll need to find a dictionary for detecting side views of faces. Last
time I checked, OpenCV didn't come with one...

Samuel

Meaw

unread,
Sep 27, 2013, 6:04:30 AM9/27/13
to jav...@googlegroups.com
Sorry,It really mean can't we detect the several position of faces using javacv?  I have use javacv for my final year research project.
I really wanted to detect several position of faces there..Can you please give me any solution.what can I do for this? thank you

Samuel Audet

unread,
Sep 28, 2013, 10:39:53 PM9/28/13
to jav...@googlegroups.com
On 09/27/2013 07:04 PM, Meaw wrote:
> Sorry,It really mean can't we detect the several position of faces using
> javacv? I have use javacv for my final year research project.
> I really wanted to detect several position of faces there..Can you
> please give me any solution.what can I do for this? thank you

Well, there is a dictionary for profile (side view) faces here:
http://alereimondo.no-ip.org/OpenCV/34
You could try it out.

Samuel

Meaw

unread,
Sep 28, 2013, 11:23:44 PM9/28/13
to jav...@googlegroups.com


Thank you, I have try it..But an image like this.. I wanted to detect faces...It doesn't work correctly..I want really count the correct number of faces..

Samuel Audet

unread,
Sep 29, 2013, 12:27:09 AM9/29/13
to jav...@googlegroups.com
On 09/29/2013 12:23 PM, Meaw wrote:
> Thank you, I have try it..But an image like this.. I wanted to detect
> faces...It doesn't work correctly..I want really count the correct
> number of faces..

Yeah, this isn't going to happen... The precision that we can achieve
with known methods isn't going to be 100%.

Samuel

Meaw

unread,
Sep 29, 2013, 12:54:40 AM9/29/13
to jav...@googlegroups.com
yeah, Is there no any way to find the correct number of faces here? like zooming and detect faces like something?
Can javacv use to zoom images and then count the number of faces?
Thank you....

Meaw

unread,
Oct 5, 2013, 9:47:41 AM10/5/13
to jav...@googlegroups.com
Can you please give me an idea for this issue please....thank you

Samuel Audet

unread,
Oct 5, 2013, 10:07:35 AM10/5/13
to jav...@googlegroups.com
On 10/05/2013 10:47 PM, Meaw wrote:
> Can you please give me an idea for this issue please....thank you

I don't think there is much we can do unfortunately...

Meaw

unread,
Oct 5, 2013, 10:16:16 AM10/5/13
to jav...@googlegroups.com
Haar cascade trainer is not work here?I was trying and there was no clear idea about the trainer..the thing is in here instead of detect frontal faces it detects the background things also....but I wanted to get the real count of faces...
Reply all
Reply to author
Forward
0 new messages