How to Detect the Age or Gender of People within an Image using Node.js

Useful metadata contained within a photo isn’t limited to just EXIF and resolution. With advanced machine learning techniques in photo recognition, we can identify other interesting information about the contents of a photo regarding the subjects within it. Our Age & Gender detection APIs will supply an approximation of the age or gender of a photo’s subjects respectively, and provide a “ClassificationConfidence” score indicating the degree to which the API feels the operation was successful. Below, we’ll walk through how you can use either API in Node.js by structuring your call with the ready-to-run code snippets below.

First things first, let’s install the Node.js SDK for either API using the following command:

npm install cloudmersive-image-api-client --save

Next, let’s include the Cloudmersive Image API Client & a code snippet to capture your Cloudmersive API key (you can get one by registering a free account on our website):

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');
var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';

Now, let’s get to the good stuff.

To detect the Age of subjects within a photo, using the following function:

var apiInstance = new CloudmersiveImageApiClient.FaceApi();var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.faceDetectAge(imageFile, callback);

To detect the Gender of subjects within a photo, use this one instead:

var apiInstance = new CloudmersiveImageApiClient.FaceApi();var imageFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.faceDetectGender(imageFile, callback);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

436 Followers

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.