Face recognition using Open CV in Java

Image for post
Image for post

In this operation we use the opencv java to build the application using spring. So in this blog you can see the face detection was successfully done by the application.

First, We look How to import the libraries To The application

First, obtain a fresh release of OpenCV from download page and extract it under a simple location like . I am using version 2.4.6, but the steps are more or less the same for other versions.

Now, we will define OpenCV as a user library in Eclipse, so we can reuse the configuration for any project. Launch Eclipse and select Window –> Preferences from the menu.

Image for post
Image for post

Navigate under Java –> Build Path –> User Libraries and click New….

Image for post
Image for post

Enter a name, e.g. OpenCV-2.4.6, for your new library.

Image for post
Image for post

Now select your new user library and click Add External JARs….

Image for post
Image for post

Browse through and select opencv-246.jar. After adding the jar, extend the opencv-246.jar and select Native library location and press Edit....

Image for post
Image for post

Select External Folder… and browse to select the folder . If you have a 32-bit system you need to select the x86 folder instead of x64.

Image for post
Image for post

Your user library configuration should look like this:

Image for post
Image for post

Testing the configuration on a new Java project

Now start creating a new Java project.

Image for post
Image for post

On the Java Settings step, under Libraries tab, select Add Library… and select OpenCV-2.4.6, then click Finish.

Image for post
Image for post
Image for post
Image for post

Libraries should look like this:

Image for post
Image for post

Now you have created and configured a new Java project it is time to test it.

Test the Face-detector

Now get the code for the Facedetector.class

package com.ocr.ocrexample;

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static java.lang.System.loadLibrary;
import static org.opencv.imgproc.Imgproc.rectangle;



class DetectFaceDemo {

public static void run(MultipartFile file) throws IOException {

loadLibrary( Core.NATIVE_LIBRARY_NAME );
CascadeClassifier faceDetector = new CascadeClassifier();
faceDetector.load( "C:\\Users\\Desktop\\ocv\\.idea\\haarcascade_frontalface_alt.xml" );

// Input image

com.ocr.ocrexample.FileUploadController.convert(file);
Mat image = Imgcodecs.imread(String.valueOf(file));
// Detecting faces
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale( image, faceDetections );

// Creating a rectangular box showing faces detected
for (Rect rect : faceDetections.toArray()) {
rectangle( image, new Point( rect.x, rect.y ), new Point( rect.width + rect.x,
rect.height + rect.y ), new Scalar( 0, 255, 0 ) );
}

// Saving the output image
String filename = "Ouput.jpg";
System.out.println("Face Detected Successfully ");
Imgcodecs.imwrite( "D:\\" + filename, image );


}
}

From this code you can get the output as a green color box that indicate the face.

Output

Image for post
Image for post
Output

You can get the face recognition from the open cv java.

Former Software Engineer @axiatadigitallabs |SLIIT | Electrical and Electronic Engineering | Contact me — ashan.lakmal1995@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store