AnandMR / javacv

Automatically exported from code.google.com/p/javacv
0 stars 0 forks source link

Unable to Save Detected face using JavaCV #56

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi to everyone.
I am working on JavaCV face detection and recognition.
I am able to detect faces but unable to find a way to store in desired .jpg 
format . Like openCv where we have cvSaveImage() method I can not find the 
similar method in JavaCv.. Please somebody tell me what i am missing ? Please 
help.
following is my code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package facedetect;

/**
 *
 * @author User
 */
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import java.awt.Image;
import java.nio.Buffer;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_calib3d.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;

public class Demo {
      public Buffer buf = null;
    public static void main(String[] args) throws Exception {
        String classifierName = "C:\\haarclass\\haarcascade_frontalface_alt.xml";

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

        CvHaarClassifierCascade classifier = new CvHaarClassifierCascade(cvLoad("C:\\haarclass\\haarcascade_frontalface_alt.xml"));
        if (classifier.isNull()) {
            System.err.println("Error loading classifier file \"" + classifierName + "\".");
            System.exit(1);
        }
        CanvasFrame frame = new CanvasFrame("Face Detection");

        FrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();
        IplImage grabbedImage = grabber.grab();

        int width  = grabbedImage.width();
        int height = grabbedImage.height();
        IplImage grayImage    = IplImage.create(width, height, IPL_DEPTH_8U, 1);

        CvMemStorage storage = CvMemStorage.create();

        CvPoint hatPoints = new CvPoint(3);
int counter=0;
     while (frame.isVisible() && (grabbedImage = grabber.grab()) != null && counter==0)
     {

            cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
            CvSeq faces = cvHaarDetectObjects(grayImage, classifier, storage,
                1.1, 3, CV_HAAR_DO_CANNY_PRUNING);
            int total = faces.total();
            for (int i = 0; i < total; i++)
            {
                CvRect r = new CvRect(cvGetSeqElem(faces, i));
                int x = r.x(), y = r.y(), w = r.width(), h = r.height();
                cvRectangle(grabbedImage, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0);
                cvRectangle(grabbedImage, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.BLUE, 1, CV_AA, 0);
                    // To access the elements of a native array, use the `position()` method
                hatPoints.position(0).x(x-w/10);    hatPoints.y(y-h/10);
                hatPoints.position(1).x(x+w*11/10); hatPoints.y(y-h/10);
                hatPoints.position(2).x(x+w/2);     hatPoints.y(y-h/2);
                cvFillConvexPoly(grabbedImage, hatPoints.position(0), 3, CvScalar.GREEN, CV_AA, 0);

            }
           //cvSave("D:\\img1.bmp", grabbedImage);
           // cvSaveImage("D:\\img1.jpg", grabbedImage);
            counter=1;
            cvThreshold(grayImage, grayImage, 64, 255, CV_THRESH_BINARY);

            frame.showImage(grabbedImage);

            cvClearMemStorage(storage);
        }

        grabber.stop();
        frame.dispose();
    }
}

Original issue reported on code.google.com by gauravsa...@gmail.com on 15 Mar 2011 at 5:12

GoogleCodeExporter commented 9 years ago
Hi,
 I am working on something very similar for a school project, which is due way too soon for my comfort. We might be able to help each other, I hope, since there is not really enough examples of JavaCV use on the web.  About your specific question, there is indeed a function cvSaveImage.  Just call:

cvSaveImage(FileName, Image);

where FileName is a string
and Image is an IplImage.

I'm not sure that it will recognize your filename string. here is what one of 
mine looks like:

  String capFileName = "data/capture" + capCounter + ".jpg";
  cvSaveImage(capFileName, grayImage);

I am storing a series of captures here in the data directory of my NetBeans 
project.  I'm not sure about the D:\\ in your filename, I don't think you can 
use backslashes. I would try to just do:

cvSaveImage("img1.bmp",grabbedImage) and see if it saves it in your current 
directory.

If it still won't work, you are probably missing an import.  I had the same 
problem so I copied the imports from the example on the JavaCV home page:

import static com.googlecode.javacv.cpp.opencv_core.*; 
import static com.googlecode.javacv.cpp.opencv_imgproc.*; 
import static com.googlecode.javacv.cpp.opencv_highgui.*;

I think it requires the highgui.* one, which I see you don't have in your 
listing. I haven't quite figured out how to find out which imports I need by 
looking at the JavaCV source, so I basically use them all. 

Let me know if you are interested in communicating about our projects.
grcunning at gmail dot com
Thanks
Gary Cunningham
Penn State University

Original comment by grcunn...@gmail.com on 15 Mar 2011 at 6:07

GoogleCodeExporter commented 9 years ago
Hi Gary ..thanks for your reply ....yes I was missing imports only..but I still 
not able to get the only face image.Current image also includes my 
background(like wall , my neck  etc)..To recognize face as per my logic i need 
only face image to store. 
Yes I will be happy to communicate and work as a team on this with you.
I am working on a project with Face and Fingerprint recognition.
My email id is gauravsagargupta@gmail.com

Original comment by gauravsa...@gmail.com on 15 Mar 2011 at 11:31

GoogleCodeExporter commented 9 years ago
Hi Gary
Thanks for your reply mail
Yes I was missing some imports only while saving.
"D:\\imagename.jpg" works fine.
But I am still not able to extract and save exact face image only. Now I can 
save complete image captured by webcam but that face part inside the Rectangle 
is what I wanted to save and process.

Following is my code in blue for face image save for which i get 
Runtime Error with my jdk1.6.0_24\bin\java.exe "This application has requested 
the Runtime to terminate it in an unusual way.Please contact the application's 
support team for more information."

while (frame.isVisible() && (grabbedImage = grabber.grab()) != null && 
counter==0)
     {

            cvCvtColor(grabbedImage, grayImage, CV_BGR2GRAY);
            CvSeq faces = cvHaarDetectObjects(grayImage, classifier, storage,
                1.1, 3, CV_HAAR_DO_CANNY_PRUNING);
            int total = faces.total();
            for (int i = 0; i < total; i++)
            {
                CvRect r = new CvRect(cvGetSeqElem(faces, i));
                int x = r.x(), y = r.y(), w = r.width(), h = r.height();

                 CvSize facesize= new CvSize(w*h);
                IplImage onlyface= cvCreateImageHeader(facesize, IPL_DEPTH_8U, grabbedImage.nChannels());
                cvGetRectSubPix(grabbedImage, onlyface, new CvPoint2D32f((r.x()+(facesize.width()/2)),(r.y()+(facesize.height()/2))));
                cvSaveImage("D:\\face.jpg", onlyface);

                cvRectangle(grabbedImage, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0);
                cvRectangle(grabbedImage, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.BLUE, 1, CV_AA, 0);

                // To access the elements of a native array, use the `position()` method
                hatPoints.position(0).x(x-w/10);    hatPoints.y(y-h/10);
                hatPoints.position(1).x(x+w*11/10); hatPoints.y(y-h/10);
                hatPoints.position(2).x(x+w/2);     hatPoints.y(y-h/2);
                cvFillConvexPoly(grabbedImage, hatPoints.position(0), 3, CvScalar.GREEN, CV_AA, 0);

            }

            cvSaveImage("D:\\PhotoGallery.jpg", grabbedImage);

            cvThreshold(grayImage, grayImage, 64, 255, CV_THRESH_BINARY);

            frame.showImage(grabbedImage);

Awaiting your response

Original comment by gauravsa...@gmail.com on 16 Mar 2011 at 12:29

GoogleCodeExporter commented 9 years ago
CvSize facesize= new CvSize(w*h); -> this allocates an array of uninitialized 
CvSize and that's probably not what you want to do..

Original comment by samuel.a...@gmail.com on 20 Mar 2011 at 5:51

GoogleCodeExporter commented 9 years ago
hi Guys!!
I have the same  issue for saving the images of the framegrabber in to an image 
type .

since cvsaveimage()is not returning an image i used cvretrieve().using 
cvCapture object.
here is my code:
public static BufferedImage faceDetectionInVideo() throws IOException {
        System.out.println("inside of CVCapture");
        IplImage image1;
        BufferedImage bufferedImage = null;

        CvCapture capture = cvCreateFileCapture("D:\\l\\OpenCV\\obama.mp4");

        int i = 1;
        int frame_counter = 1;
        while (true) {
            image1 = cvQueryFrame(capture);
            if (image1 == null)
                break;

            IplImage grayImg = cvCreateImage(cvGetSize(image1), IPL_DEPTH_8U, 1);
            cvCvtColor(image1, grayImg, CV_BGR2GRAY);

            if ((frame_counter % 500) == 0) {

                cvShowImage("Result", grayImg);
                image1 = cvRetrieveFrame(capture);// iplImage type
                bufferedImage = image1.getBufferedImage();

                // cvSaveImage(i + "D://sample images//teja.jpg", grayImg);

            }

            int key = cvWaitKey(1);
            if (key == 10) {
                break;
            }

            frame_counter++;
            i++;

        }
        return bufferedImage;
    }

my proble is that it is returning me the last frame of the video .

do help me in getting all the frames of the video .

Original comment by alluri.r...@gmail.com on 25 Sep 2013 at 7:33