How To Detect Faces in an Image in Java
Leverage innovative face detection technology to instantly locate the positions of faces in an image to assist with metadata entry, accessibility features, etc.
Join the DZone community and get the full member experience.
Join For FreeFace detection technology has come a long way over the past decade; it began with simple computer vision techniques and has progressed to sophisticated AI and other state-of-the-art technologies. The reason it has become such an integral innovation is due to the role it plays as the initial step in numerous facial recognition processes.
Face detection is often used interchangeably with facial recognition, but the two terms actually have two different definitions. Face detection is a specific function that finds and identifies human faces in images or videos, while facial recognition is a method of identifying or verifying the identity of an individual from their face.
In this article, we will provide a step-by-step tutorial on how to use our Face Detection API to instantly locate and identify the position of all faces within an image. This can be particularly helpful for websites to assist with metadata entry, captioning, and accessibility features.
Starting off, we will install the Maven SDK by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, we’re going to add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
Now that we’ve installed the package, we can move on to calling the face detection function with the following code:
xxxxxxxxxx
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.FaceApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
FaceApi apiInstance = new FaceApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
FaceLocateResponse result = apiInstance.faceLocate(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FaceApi#faceLocate");
e.printStackTrace();
}
The returned result will indicate how many faces were identified, and which quadrant of the photo they’re in. To ensure that the result is efficient and accurate, these parameters will need to be met:
- API key—to retrieve your personal API key, visit the Cloudmersive website to register for a free account; this will give you access to 800 monthly calls
- Image file—common file formats such as PNG and JPEG are supported
In conclusion, we hope you found the tutorial helpful and easy to follow. If you’d like to delve further into the facial recognition journey, we have several other functions that may interest you, such as face detection with landmarks, age detection, face cropping, and more!
Opinions expressed by DZone contributors are their own.
Comments