Identifying faces

Identifying faces within an image is useful in many situations. It can potentially classify an image as one containing people or find people in an image for further processing. We will use OpenCV 3.1 (http://opencv.org/opencv-3-1.html) for our examples.

OpenCV (http://opencv.org/) is an open source computer vision library that supports several programming languages, including Java. It supports a number of techniques, including machine learning algorithms, to perform computer vision tasks. The library supports such operations as face detection, tracking camera movements, extracting 3D models, and removing red eye from images. In this section, we will demonstrate face detection.

Using OpenCV to detect faces

The example that follows was adapted from http://docs.opencv.org/trunk/d9/d52/tutorial_java_dev_intro.html. Start by loading the native libraries added to your system when OpenCV was installed. On Windows, this requires that appropriate DLL files are available:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 

We used a base string to specify the location of needed OpenCV files. Using an absolute path works better with many methods:

String base = "PathToResources"; 

The CascadeClassifier class is used for object classification. In this case, we will use it for face detection. An XML file is used to initialize the class. In the following code, we use the lbpcascade_frontalface.xml file, which provides information to assist in the identification of objects. In the OpenCV download are several files, as listed here, that can be used for specific face recognition scenarios:

  • lbpcascade_frontalcatface.xml
  • lbpcascade_frontalface.xml
  • lbpcascade_frontalprofileface.xml
  • lbpcascade_silverware.xml

The following statement initializes the class to detect faces:

CascadeClassifier faceDetector =  
        new CascadeClassifier(base +  
            "/lbpcascade_frontalface.xml"); 

The image to be processed is loaded, as shown here:

Mat image = Imgcodecs.imread(base + "/images.jpg"); 

For this example, we used the following image:

Using OpenCV to detect faces

To find this image, perform a Google search using the term people. Select the Images category and then filter for Labeled for reuse. The image has the label: Closeup portrait of a group of business people laughing by Lynda Sanchez.

When faces are detected, the location within the image is stored in a MatOfRect instance. This class is intended to hold vectors and matrixes for any faces found:

MatOfRect faceVectors = new MatOfRect(); 

At this point, we are ready to detect faces. The detectMultiScale method performs this task. The image and the MatOfRect instance to hold the locations of any images are passed to the method:

faceDetector.detectMultiScale(image, faceVectors); 

The next statement shows how many faces were detected:

out.println(faceVectors.toArray().length + " faces found"); 

We need to use this information to augment the image. This process will draw boxes around each face found, as shown next. To do this, the Imgproc class' rectangle method is used. The method is called once for each face detected. It is passed the image to be modified and the points represented the boundaries of the face:

for (Rect rect : faceVectors.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)); 
}

The last step writes this image to a file using the Imgcodecs class' imwrite method:

Imgcodecs.imwrite("faceDetection.png", image); 

As shown in the following image, it was able to identify four images:

Using OpenCV to detect faces

Using different configuration files will work better for other facial profiles.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.206.254