[JavaCV] How to detect Face objects in an image?

1,707 views
Skip to first unread message

Issamou

unread,
Apr 8, 2012, 12:43:23 PM4/8/12
to javacv
Dear all,

Using a camera to perform the task of Surveillance, I am planning to
make my camera save a video frame whenever it detects a face object in
its stream, the video frame is then cropped to isolate the face only.
Is there anyone who can kindly provide me with tutorials/methods or
specific libraries to perform such a task :)? Thanks a lot!

Regards,
--Issam

Samuel Audet

unread,
Apr 14, 2012, 10:47:11 PM4/14/12
to jav...@googlegroups.com
Hello,

Simply calling cvSetImageROI() on the input image will crop it...

Samuel

siubie

unread,
Apr 22, 2012, 1:17:53 AM4/22/12
to javacv
Hi hello samuel, i am new to java and i try this code to detect face
using haarscade classifier, where is should put the cvSetImageROI to
crop only the face ?

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cv;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
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.*;
/**
*
* @author siubie
*/
public class deteksi {
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 =
"haarcascade_frontalface_alt.xml";

public static void detektor(String gambar){
if (gambar.length() < 0) {
System.out.println("No 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 " + gambar);
IplImage origImg = cvLoadImage(gambar);

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

// instantiate a classifier cascade for face detection
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));
cvRectangle(origImg, cvPoint(r.x() * SCALE, r.y() *
SCALE), // undo the scaling
cvPoint((r.x() + r.width()) * SCALE, (r.y() +
r.height()) * SCALE),
CvScalar.BLUE, 6, CV_AA, 0);
}

if (total > 0) {
System.out.println("Saving marked-faces version of " +
gambar + " in " + gambar);
cvSaveImage(gambar, origImg);
}

}
}

On Apr 15, 9:47 am, Samuel Audet <samuel.au...@gmail.com> wrote:
> Hello,
>
> Simply calling cvSetImageROI() on the input image willcropit...
>
> Samuel
>
> On 2012-04-09 01:43, Issamou wrote:
>
>
>
>
>
>
>
> > Dear all,
>
> > Using a camera to perform the task of Surveillance, I am planning to
> > make my camera save a video frame whenever it detects afaceobject in

Samuel Audet

unread,
Apr 22, 2012, 8:31:59 AM4/22/12
to jav...@googlegroups.com
On 2012-04-22 14:17, siubie wrote:
> Hi hello samuel, i am new to java and i try this code to detect face
> using haarscade classifier, where is should put the cvSetImageROI to
> crop only the face ?

After


CvRect r = new CvRect(cvGetSeqElem(faces, i));

we can call something like
cvSetImageROI(origImg, r);
and call
cvSaveImage(origImg, ...);
or
cvCopy(origImg, ...);
or whatever

Samuel

Putra Prima

unread,
Apr 29, 2012, 7:15:27 AM4/29/12
to jav...@googlegroups.com
yap i got it thx a lot samuel :)
--
Putra Prima A, Senator of Cre4ctive

Born Small Grow Fast Get Strong and Never Fall

Alluri ravi teja

unread,
Aug 20, 2013, 3:51:26 AM8/20/13
to jav...@googlegroups.com, putra...@gmail.com
can share me the code of cropping the image of the detected face.

Samuel Audet

unread,
Aug 31, 2013, 8:55:56 PM8/31/13
to jav...@googlegroups.com


On 08/20/2013 04:51 PM, Alluri ravi teja wrote:
Reply all
Reply to author
Forward
0 new messages