Skip to content

Restoring Faces

We’re going to walk through how to take a low-quality face image and restore it using the face restoration endpoint. We’ll also show how to upscale the image at the same time.

We’ll be using a small, degraded portrait as our input image:

facerestore-input.jpg

Terminal window
# Create a project directory.
mkdir prodia-restoring-faces
cd prodia-restoring-faces

Install Node (if not already installed):

Terminal window
brew install node
# Close the current terminal and open a new one so that node is available.

Create project skeleton:

Terminal window
# Requires node --version >= 18
# Initialize the project with npm.
npm init -y
# Install the prodia-js library.
npm install prodia --save
Terminal window
# Export your token so it can be used by the main code.
export PRODIA_TOKEN=your-token-here

Your token is exported to an environment variable. If you close or switch your shell you’ll need to run export PRODIA_TOKEN=your-token-here again.

Create a main file for your project:

main.js
const { createProdia } = require("prodia/v2");
const prodia = createProdia({
token: process.env.PRODIA_TOKEN // get it from environment
});

You’re now ready to make some API calls!

main.js
const { createProdia } = require("prodia/v2");
const fs = require("node:fs/promises");
const prodia = createProdia({
token: process.env.PRODIA_TOKEN,
});
(async () => {
// get input image
const inputBuffer = await (await fetch("https://docs.prodia.com/facerestore-input.jpg")).arrayBuffer();
const job = await prodia.job({
type: "inference.facerestore.v1",
}, {
inputs: [ inputBuffer ]
});
const image = await job.arrayBuffer();
await fs.writeFile("restored.jpg", new Uint8Array(image));
})();
Terminal window
node main.js
Terminal window
open restored.jpg

The restored face has sharper features and clearer details compared to the input:

facerestore-output.jpg

The facerestore.upscale endpoint combines face restoration with upscaling. You can upscale by 2x, 4x, or 8x.

main.js
const { createProdia } = require("prodia/v2");
const fs = require("node:fs/promises");
const prodia = createProdia({
token: process.env.PRODIA_TOKEN,
});
(async () => {
// get input image
const inputBuffer = await (await fetch("https://docs.prodia.com/facerestore-input.jpg")).arrayBuffer();
const job = await prodia.job({
type: "inference.facerestore.upscale.v1",
config: {
upscale: 4,
},
}, {
inputs: [ inputBuffer ]
});
const image = await job.arrayBuffer();
await fs.writeFile("restored-upscaled.jpg", new Uint8Array(image));
})();
Terminal window
node main.js
Terminal window
open restored-upscaled.jpg

Our 128x128 input has been restored and upscaled to 512x512 with dramatically improved facial detail:

facerestore-upscale-output.jpg

ParameterTypeValuesDefaultDescription
upscalenumber2, 4, 82Upscale factor (only for facerestore.upscale.v1)
ConstraintValue
Accepted formatsPNG, JPEG, WebP
Minimum dimensions128 x 128
Maximum dimensions2048 x 2048
Maximum file size10 MB