Editor’s note: We found an interesting series of articlesLearn 30 New Technologies in 30 Days, ready for translation, one update a day, year-end gift package. Here is the twelfth day.


Today I’m going to learn how to use Java for face detection. Face detection helps to recognize faces on any digital image, and after doing some research, I found that OpenCV’s library could help me detect faces in images. However, I haven’t been able to find a complete beginner’s tutorial on using the OpenCV library through Java, so this article may be a resource for others to learn about.

What is OpenCV?

OpenCV(Open Source Computer Vision) is an Open Source Computer Vision algorithm library. It is written in C/C++ and is designed to take advantage of multicore. It provides C++, C, Python, and Java interfaces and supports all major operating system platforms, including Windows, Linux, Mac OS, iOS, and Android.


Making library

The code for today’s demo application is available on GitHub: day12-face-detection.


OpenCV entry

To start learning OpenCV, the first step is to go to its official website to download the latest version of OpenCV package that supports your current operating system. In this article, version 2.4.7 is used.

When the package is downloaded, unzip it using tar:

$tar XVF opencv - 2.4.7. Tar. Gz

Switch directory to opencv-2.4.7:

$CD opencv - 2.4.7

Build OpenCV jar

I spent a lot of time learning how to get a JAR file for OpenCV. The Java tutorial in the documentation assumes that OpenCV JAR files are OpenCV packages (including JAR files) in the build folder for Windows users, but not for Linux and Mac OS users. To build the OpenCV JAR, execute the following command:

$CD opencv-2.4.7 $mkdir build $CD build/ $cmak-g "Unix Makefiles" -d CMAKE_CXX_COMPILER=/usr/bin/ G ++ -d CMAKE_C_COMPILER=/usr/bin/gcc -D WITH_CUDA=ON .. $ make -j4 $ make install

The above command creates the OpenCV-247.jar file in the opencv-2.4.7/build/bin directory, which is the installation method for binding Java to native OpenCV.


Download Eclipse

If you don’t have Eclipse installed on your system, goThe Eclipse’s official websiteDownload the latest version. For now, the latest version of Eclipse is codenamed Kepler.

Installation of Eclipse is easy; you just need to unpack the downloaded package. If you’re on a Linux or Mac machine, open a command-line window and type:

$ tar -xzvf eclipse-jee-kepler-R-*.tar.gz 

In Windows, where you unzip it, there’s an Eclipse folder, so you can work directly with it, and of course you can create shortcuts to executing files to the desktop.


Add a user library

Open the Eclipse IDE, then go to the project workspace and go to the directoryWindows > Preferences > Java > Build Path > User LibrariesNext, select Add a new library.

I’m going to call this libraryOpenCV - 2.4.7“And click” OK.”

Click on theAdd External JarsAnd then addOpenCV - 2.4.7File.

chooseNative library location, and then click”Editor (Edit)“.

Click on theExternal Folder.

inOpencv 2.4.7 / build/libGiven in the folderThe library catalog (lib)The path.

Now, click OK, and we’ve added OpenCV as the User Library.


Create a new Java project

Step by stepFile > New > Other > Java ProjectNext, create a new Java project. When finished, right-click on the project to configure the build path.

Go to theLibrariesTAB, and then click”Add Library“.

Select the”User Library

Select the OpenCV-2.4.7 user library we added in the last step and click “Done”.

Finally, you’ll see that the OpenCV-2.4.7 user library has been included in the Java project.


Write the FaceDetector.

Create a class in the Java project created above and add the following code:

package com.shekhar.facedetection; 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.highgui.Highgui; import org.opencv.objdetect.CascadeClassifier; public class FaceDetector { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.out.println("\nRunning FaceDetector"); CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("haarcascade_frontalface_alt.xml").getPath()); Mat image = Highgui .imread(FaceDetector.class.getResource("shekhar.JPG").getPath()); MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); for (Rect rect : faceDetections.toArray()) { Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } String filename = "ouput.png"; System.out.println(String.format("Writing %s", filename)); Highgui.imwrite(filename, image); }}

The above code can:

  1. Load the native OpenCV library so that you can use it to call the Java API.
  2. Create an instance CascadeClassifier and pass it the filename of the loaded classifier.
  3. Next we’ll convert the image into a format that Java API can accept using the HighUI class, laying on top of OpenCV C++ ‘s n-dimensional dense array class.
  4. Then, call the detectMultiscale method on the classifier to pass it the image and the MatOfRect object. After this process, Matofrect will have a face check.
  5. We iterate over all the face checks and mark the images with rectangles.
  6. Finally, the image is written to the output.pngIn the file.

The output of this program is shown below, before and after face detection:

That’s all for today. Feedback is welcome.



Day 12: OpenCV–Face Detection for Java Developers Clean up SegmentFault