Creating a Face Detector with Next.js

Eugene Musebe


This article demonstrates how to apply a face detection transformation on custom images using cloudinary SDK.


Check the sandbox demo on Codesandbox.

You can also get the project GitHub repo using Github.


Entry-level javascript and React/Nextjs knowledge.


Cloudinary refers to an end-to-end image and video management solution for websites and mobile applications. This involves services such as storage, uploads, manipulations e.t.c What we will be achieving today is using Cloudinary React SDK to achieve a face transformation feature for a custom imageCloudinary. This project will also store the processed image online using the same SDK.

Setting Up the Sample Project

Begin with creating a Nextjs project through your terminal using npx create-next-app facerecognition.

Head to your project: cd facerecognition

We will include all our frontend code in the index component. Before you start ensure to include cloudinary-react in your project dependencies: npm install cloudinary-react.

Next, we will need the following elements:

1- Image => To define an image tag with
2- Transformation => Allows us to define our transformation on the parent element.

To transform an image using cloudinary-react SDK, we need to first upload it to cloudinary media servers. There are three ways to achieve this: upload widget, upload endpoint, and jQuery SDK with Blueimp File Upload adapter. We will go with the upload widget option. First, Use this link to create your own cloudinary account and log into it. Click on the Media Library option at the navigation bar. We will use the following sample image for our transformation.

Proceed by clicking the upload button and select browse from the popup menu to select the image from your local directory.

Once selected the image will be available in your cloudinary console media library.

Proceed to the pages/ index directory and begin by importing the respective elements:

4 import { Image, Transformation } from 'cloudinary-react';

Paste the following code in your return statement.

1return (
2 <div className="container">
3 <h1>Create a Nextjs face detector with Cloudinary</h1>
4 <div className="row">
5 <div className="column">
6 <img width="550" height="400" src="" />
7 </div>
8 <div className="column">
9 <Image
10 id="image"
11 cloudName="dogjmmett"
12 secure={true}
13 upload_preset="my_unsigned_preset"
14 publicId="officelady"
15 >
16 <Transformation width="200" height="200" gravity="face" crop="thumb" />
17 </Image><br />
18 <button >Upload</button>
19 </div>
20 </div>
21 </div >
22 )

The code above involves a heading tag and a row containing two columns. One will contain the original image and the second will contain the processed image.

Remember to include your cloud name from the cloudinary dashboard to your image tag as well as the title of the tour image component from your media library. For instance, our name is dogjmmett as shown here:

and the publicId will be the image file name on your media library as shown:


With our transformation complete, we need to be able to upload the transformed image i.e for future references. To achieve this we will have to code our upload configuration in the nextjs backend. Create a file in your project root directory and paste the following code:


Use the environment variables from your cloudinary dashboard to fill in the blanks above

Restart your project using npm run dev and

In the pages/api folder, create a new file named upload.js and begin by configuring the environment keys and libraries.

1var cloudinary = require("cloudinary").v2;
4 cloud_name: process.env.CLOUDINARY_NAME,
5 api_key: process.env.CLOUDINARY_API_KEY,
6 api_secret: process.env.CLOUDINARY_API_SECRET,

Create a handler function to execute the POST request. The function will receive media file data and post it to the cloudinary website, Capture the media file's cloudinary link and send it back as a response.

1export default async function handler(req, res) {
2 if (req.method === "POST") {
3 let url = ""
4 try {
5 let fileStr =;
6 const uploadedResponse = await cloudinary.uploader.upload_large(
7 fileStr,
8 {
9 resource_type: "video",
10 chunk_size: 6000000,
11 }
12 );
13 url = uploadedResponse.url
14 } catch (error) {
15 res.status(500).json({ error: "Something wrong" });
16 }
18 res.status(200).json({data: url});
19 }

The code above concludes our backend as well as the project itself. Ensure to test out the article to enjoy the experience.

Happy coding!

Eugene Musebe

Software Developer

I’m a full-stack software developer, content creator, and tech community builder based in Nairobi, Kenya. I am addicted to learning new technologies and loves working with like-minded people.