DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code

Trending

  • LLM Agents and Getting Started with Them
  • Designing API-First EMR Architectures in .NET: Enabling Modular Growth in Compliance-Driven Systems
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • S3 Vectors: How to Build a RAG Without a Vector Database
  1. DZone
  2. Coding
  3. Java
  4. How To Detect Faces in an Image in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Jan. 28, 21 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

Face 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:

Java
 




x


 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>



Next, we’re going to add a reference to the dependency:

Java
 




xxxxxxxxxx
1


 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>



Now that we’ve installed the package, we can move on to calling the face detection function with the following code:

Java
 




xxxxxxxxxx
1
25


 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.FaceApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
FaceApi apiInstance = new FaceApi();
17
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.
18
try {
19
    FaceLocateResponse result = apiInstance.faceLocate(imageFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling FaceApi#faceLocate");
23
    e.printStackTrace();
24
}



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!

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Rethinking Java CRUDs With Event Sourcing and CQRS Patterns
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook