# Restoring Faces

Source: https://docs.prodia.com/guides/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](https://docs.prodia.com/llms/assets/8b7a295861f5-facerestore-input.jpg)

### Project Setup

```bash
# Create a project directory.
mkdir prodia-restoring-faces
cd prodia-restoring-faces
```

### prodia-js

Install Node (if not already installed):

### macOS

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

### Linux

```bash
apt install node
# Close the current terminal and open a new one so that node is available.
```

### Windows

```bash
winget install -e --id OpenJS.NodeJS.LTS
# Close the current terminal and open a new one so that node is available.
```

Create project skeleton:

```bash
# Requires node --version >= 18
# Initialize the project with npm.
npm init -y

# Install the prodia-js library.
npm install prodia --save
```

### requests

Install Python (if not already installed):

### macOS

```bash
brew install python
# Close the current terminal and open a new one so that python is available.
```

### Linux

```bash
apt install python3 python3-venv python-is-python3
# Close the current terminal and open a new one so that python is available.
```

### Windows

```bash
winget install -e --id Python.Python.3.12
# Close the current terminal and open a new one so that python is available.
```

```bash
# Requires python --version >= 3.12
python -m venv venv
source venv/bin/activate
pip install requests
```

### curl

Install curl (if not already installed):

### macOS

```bash
brew install curl
# Close the current terminal and open a new one so that curl is available.
```

### Linux

```bash
apt install curl
# Close the current terminal and open a new one so that curl is available.
```

### Windows

```bash
# NOTE: Windows 10 and up have curl installed by default and this can be
# skipped.
winget install -e --id cURL.cURL
# Close the current terminal and open a new one so that curl is available.
```

```bash
# Export your token so it can be used by the main code.
export PRODIA_TOKEN=your-token-here
```

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

### prodia-js

```js title="main.js"
const { createProdia } = require("prodia/v2");

const prodia = createProdia({
    token: process.env.PRODIA_TOKEN // get it from environment
});
```

### requests

Create the following `main.py`

```python title="main.py"
from requests.adapters import HTTPAdapter, Retry
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})
```

### curl

```bash title="main.sh"
set -euo pipefail
```

You're now ready to make some API calls!

### Restore a face

### prodia-js

```javascript
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));
})();
```

```bash
node main.js
```

### requests

```python
from requests.adapters import HTTPAdapter, Retry
from io import BytesIO
import json
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})

try:
    with open('facerestore-input.jpg', 'rb') as f:
        input_image = f.read()
except FileNotFoundError:
    res = requests.get('https://docs.prodia.com/facerestore-input.jpg')
    input_image = BytesIO(res.content)
    with open('facerestore-input.jpg', 'wb') as f:
        f.write(res.content)
except Exception as e:
    raise e

headers = {
    'Accept': 'image/jpeg',
}

job = {
    'type': 'inference.facerestore.v1',
}

files = [
    ('job', ('job.json', BytesIO(json.dumps(job).encode('utf-8')), 'application/json')),
    ('input', ('facerestore-input.jpg', input_image, 'image/jpeg')),
]

res = session.post(prodia_url, headers=headers, files=files)
print(f"Request ID: {res.headers['x-request-id']}")
print(f"Status: {res.status_code}")

if res.status_code != 200:
    print(res.text)
    sys.exit(1)

with open('restored.jpg', 'wb') as f:
    f.write(res.content)
```

```bash
python main.py
```

### curl

```bash
set -euo pipefail

cat <<EOF > job.json
{
  "type": "inference.facerestore.v1"
}
EOF

if [[ ! -f facerestore-input.jpg ]]; then
  curl -Lo facerestore-input.jpg 'https://docs.prodia.com/facerestore-input.jpg'
fi

curl -sSf --retry 3 \
  -H "Authorization: Bearer $PRODIA_TOKEN" \
  -H 'Accept: image/jpeg' \
  -F job=@job.json \
  -F input=@facerestore-input.jpg \
  --output restored.jpg \
  https://inference.prodia.com/v2/job
```

```bash
bash main.sh
```

### macOS

```bash
open restored.jpg
```

### Linux

```bash
xdg-open restored.jpg
```

### Windows

```bash
start restored.jpg
```

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

![facerestore-output.jpg](https://docs.prodia.com/llms/assets/61961f641df6-facerestore-output.jpg)

### Restore and upscale

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

### prodia-js

```javascript
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));
})();
```

```bash
node main.js
```

### requests

```python
from requests.adapters import HTTPAdapter, Retry
from io import BytesIO
import json
import os
import requests
import sys


prodia_token = os.getenv('PRODIA_TOKEN')
prodia_url = 'https://inference.prodia.com/v2/job'

session = requests.Session()
retries = Retry(allowed_methods=None, status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)
session.mount('http://', HTTPAdapter(max_retries=retries))
session.mount('https://', HTTPAdapter(max_retries=retries))
session.headers.update({'Authorization': f"Bearer {prodia_token}"})

try:
    with open('facerestore-input.jpg', 'rb') as f:
        input_image = f.read()
except FileNotFoundError:
    res = requests.get('https://docs.prodia.com/facerestore-input.jpg')
    input_image = BytesIO(res.content)
    with open('facerestore-input.jpg', 'wb') as f:
        f.write(res.content)
except Exception as e:
    raise e

headers = {
    'Accept': 'image/jpeg',
}

job = {
    'type': 'inference.facerestore.upscale.v1',
    'config': {
        'upscale': 4,
    },
}

files = [
    ('job', ('job.json', BytesIO(json.dumps(job).encode('utf-8')), 'application/json')),
    ('input', ('facerestore-input.jpg', input_image, 'image/jpeg')),
]

res = session.post(prodia_url, headers=headers, files=files)
print(f"Request ID: {res.headers['x-request-id']}")
print(f"Status: {res.status_code}")

if res.status_code != 200:
    print(res.text)
    sys.exit(1)

with open('restored-upscaled.jpg', 'wb') as f:
    f.write(res.content)
```

```bash
python main.py
```

### curl

```bash
set -euo pipefail

cat <<EOF > job.json
{
  "type": "inference.facerestore.upscale.v1",
  "config": {
    "upscale": 4
  }
}
EOF

if [[ ! -f facerestore-input.jpg ]]; then
  curl -Lo facerestore-input.jpg 'https://docs.prodia.com/facerestore-input.jpg'
fi

curl -sSf --retry 3 \
  -H "Authorization: Bearer $PRODIA_TOKEN" \
  -H 'Accept: image/jpeg' \
  -F job=@job.json \
  -F input=@facerestore-input.jpg \
  --output restored-upscaled.jpg \
  https://inference.prodia.com/v2/job
```

```bash
bash main.sh
```

### macOS

```bash
open restored-upscaled.jpg
```

### Linux

```bash
xdg-open restored-upscaled.jpg
```

### Windows

```bash
start restored-upscaled.jpg
```

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

![facerestore-upscale-output.jpg](https://docs.prodia.com/llms/assets/94964dcf0924-facerestore-upscale-output.jpg)

### Parameters

| Parameter | Type   | Values  | Default | Description                                        |
| --------- | ------ | ------- | ------- | -------------------------------------------------- |
| `upscale` | number | 2, 4, 8 | 2       | Upscale factor (only for `facerestore.upscale.v1`) |

### Input requirements

| Constraint         | Value           |
| ------------------ | --------------- |
| Accepted formats   | PNG, JPEG, WebP |
| Minimum dimensions | 128 x 128       |
| Maximum dimensions | 2048 x 2048     |
| Maximum file size  | 10 MB           |
