1

I am totally new to OpenCV. I am able to detect the face from webCam. I am little bit confused how to save detected face, how to save and recognized if again that person come again in front of camera.

Detection code

private void detectAndDisplay(Mat frame)
{
    MatOfRect faces = new MatOfRect();
    Mat grayFrame = new Mat();

    // convert the frame in gray scale
    Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
    // equalize the frame histogram to improve the result
    Imgproc.equalizeHist(grayFrame, grayFrame);

    // compute minimum face size (20% of the frame height, in our case)
    if (this.absoluteFaceSize == 0)
    {
        int height = grayFrame.rows();
        if (Math.round(height * 0.2f) > 0)
        {
            this.absoluteFaceSize = Math.round(height * 0.2f);
        }
    }

    // detect faces
    this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
            new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());

    // each rectangle in faces is a face: draw them!
    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++)
        Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);

}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Night Programmer
  • 341
  • 5
  • 25
  • 1
    There are 2 different concepts known as Face Detection and Face Recognition. Both problems have completely different solutions. To solve the first one you train on all available facial images, but for the second one you at least need 10-20 sample images of that particular person to train your detector, else it would have very less accuracy. – ZdaR Mar 13 '18 at 06:23
  • Yes I know both are totally different . But I need a help to how to save detected image in local system as .jpg or .png – Night Programmer Mar 13 '18 at 06:25

2 Answers2

2

If you want to save detected face image, maybe you can try something like this

for (int i = 0; i < facesArray.length; i++)
{   
     Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);
     Rect rect(facesArray[i].tl().x, facesArray[i].tl().y, facesArray[i].br().x - facesArray[i].tl().x, facesArray[i].br().y - facesArray[i].tl().y);
     Mat cropFace = frame(rect);
     imwrite("./face"+i+".jpg", cropFace);
}
Renee
  • 31
  • 4
  • what is cropFace – Night Programmer Mar 13 '18 at 10:32
  • 1
    cropFace is a region of interest from original image, it creates a reference , not a copy. Any change to cropFace will change the original image. – Renee Mar 13 '18 at 11:32
  • 2
    cropFace is a Mat , you can reference this link https://stackoverflow.com/questions/8267191/how-to-crop-a-cvmat-in-opencv – Renee Mar 13 '18 at 12:15
  • what is **imwrite**, i got error there "The method imwrite(String, Mat) is undefined for the type FaceDetectionController" – Night Programmer Mar 14 '18 at 07:42
  • maybe you have to add "import org.opencv.imgcodecs.Imgcodecs" and modify to "Imgcodecs.imwrite("./face.jpg", cropFace);" – Renee Mar 14 '18 at 08:11
0

I got solution

      for (Rect rect : facesArray) {
            Imgproc.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                    new Scalar(0, 255, 0)); // frame is Mat
            rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);

            Mat image_roi = new Mat(frame,rectCrop);
             Imgcodecs.imwrite("./face"+ i +".jpg",image_roi);
             i++;
        }

Now , I can cropped multiple faces

Night Programmer
  • 341
  • 5
  • 25