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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Trending

  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Jan. 28, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Member Spotlight: Shai Almong

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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