In OpenIMAJ there are some pre-built command-line tools for detecting and comparing faces within images. These are found in the org.openimaj.tools.faces
package within the tools
sub-project.
To run the tools you need to have the OpenIMAJ FaceTools JAR on your classpath.
To generate the JAR go into the face tools sub-project and use the Maven assembly plugin:
cd tools/FaceTools mvn assembly:assembly
The generated JAR is in the target directory. The generated FaceTools JAR is executable and will automatically run the FaceDetectorTool.
The face detector tool will take one or more images and output the bounding boxes of all the faces within the images. It uses the Haar Cascades method of face detection using the haarcascade-frontalface_alt.xml
cascade (have a look at the code to see how it works).
To run the tool use a command similar to the following:
java -cp FaceTools.jar org.openimaj.tools.faces.FaceDetectorTool /home/demo/image1.jpg /home/demo/image2.jpg
Running this command will output a list of the faces within the two demo images similar to the following:
/home/demo/image1.jpg:194.0,190.0,114.0,114.0 /home/demo/image2.jpg:365.0,229.0,103.0,103.0 /home/demo/image2.jpg:453.0,56.0,129.0,129.0 /home/demo/image2.jpg:144.0,82.0,147.0,147.0
The output is of the form:
filename:x,y,width,height
The units are given in pixels.
The tool has the option to display the images with boxes drawn over the detected faces. Use the -d
switch (or --display
) to activate this.
The face similarity tool will compare all the detected faces in all the given images with each other and output a distance matrix.
To run the tool use a command similar to the following:
java -cp FaceTools.jar org.openimaj.tools.faces.FaceSimilarityTool /home/demo/image1.jpg /home/demo/image2.jpg
Running this command will output something similar to:
:0 :1 :2 :0 /home/demo/image1.jpg:0: 0.00, 44.25, 40.22, 49.29 /home/demo/image2.jpg:1: 44.25, 0.00, 38.93, 41.26 /home/demo/image2.jpg:2: 40.22, 38.93, 0.00, 48.53 /home/demo/image2.jpg:0: 49.29, 41.26, 48.53, 0.00
Faces are numbered in the images and the order of the columns is the same as the order of the rows; so you will notice the zero distance down the diagonal. The distance matrix is symmetric because the distance from face1 to face2 will be the same as the distance from face2 to face1.
If you need to disambiguate which faces have been found to be most similar, the similarity tool is also able to output the matching bounding boxes of the detected faces. To activate this use the -bb
switch and the bounding boxes will be printed out before the matrix. The output is similar to the detector tool, except that all the faces are numbered.
For programmatically detecting faces, OpenIMAJ has a class called FacePipeline
which deals with the detection process for you. So, for example, to detect faces in your own image you can just call the FacePipeline
's extractFaces
method which takes an FImage
:
FImage faceImage = ImageUtilities.readF( "myImage.jpg" ); FacePipeline pipeline = new FacePipeline(); LocalFeatureList<FacialDescriptor> faces = pipeline.extractFaces( faceImage );
The resulting feature list contains FacialDescriptor
s which allow you to get at the keypoint locations (eyes, nose, mouth) as well as the overall bounding box of the face.
Anonymous