Open In App

Image Processing in Java – Face Detection

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisites:

Face Detection using OpenCV

In the Introductory Set on Image Processing, BufferedImage class of Java was used for processing images the applications of BufferedImage class are limited to some operations only, i.e, we can modify the R, G, B values of the given input image and produce the modified image. For complex image processing such as face/object detection, the OpenCV library is used which we will use in this article. 

At first, we need to set up OpenCV for Java, we recommend using eclipse for the same since it is easy to use and set up. Now let us understand some of the methods required for face detection. 
 

  1. CascadeClassifier(): This class is used to load the trained cascaded set of faces which we will be using to detect faces for any input image.
  2. Imcodecs.imread()/Imcodecs.imwrite() : These methods are used to read and write images as Mat objects which are rendered by OpenCV.
  3. Imgproc.rectangle() : Used to generate rectangle box outlining faces detected, it takes four arguments – input_image, top_left_point, bottom_right_point, color_of_border.

Implementation:

Note: The front face cascade file can be downloaded from a local git repository. 

The input image is as follows:

Example:

Java




// Java Program to Demonstrate Face Detection
// Using OpenCV Library
  
// Importing package module
package ocv;
  
// Importing required classes
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
  
// Main class
// FaceDetector
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // For proper execution of native libraries
        // Core.NATIVE_LIBRARY_NAME must be loaded before
        // calling any of the opencv methods
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  
        // Face detector creation by loading source cascade
        // xml file using CascadeClassifier and must be
        // placed in same directory of the source java file
  
        // File is available here on git as mentioned above
        // prior to code
        CascadeClassifier faceDetector
            = new CascadeClassifier();
        faceDetector.load(
            "haarcascade_frontalface_alt.xml");
  
        // Reading the input image
        Mat image = Imgcodecs.imread("E:\\input.jpg");
  
        // Detecting faces
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image,
                                      faceDetections);
  
        // Creating a rectangular box which represents for
        // faces detected
        for (Rect rect : faceDetections.toArray()) {
            Imgproc.rectangle(
                image, new Point(rect.x, rect.y),
                new Point(rect.x + rect.width,
                          rect.y + rect.height),
                new Scalar(0, 255, 0));
        }
  
        // Saving the output image
        String filename = "Ouput.jpg";
  
        Imgcodecs.imwrite("E:\\" + filename, image);
  
        // Display message for successful execution of
        // program
        System.out.print("Face Detected");
    }
}


Output:

Face Detected

The output of the program is shown below  my pic before and after face detection

Output Explanation:

  • It loads the native OpenCV library to use Java API. An instance of CascadeClassifier is created, passing it the name of the file from which the classifier is loaded.
  • Next, the detectMultiScale method is used on the classifier passing it the given image and MatOfRect object.
  • MatOfRect is responsible to do face detections after processing.
  • The process is iterated for doing all the face detections and marking the image with rectangles and at the end, the image is saved as an “output.png” file.


Last Updated : 13 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads