Skip to content

Face Restoration and Enhancement with GFPGAN

Beginner
5 min

Restore and enhance faces in old, blurry, or low-resolution photos. GFPGAN reconstructs facial detail and can upscale the image at the same time. The whole flow is one hosted call on a single gfpgan client.

Uses: tencentarc/gfpgan, a cheap, hosted Replicate model in the Socaity catalog. It runs TencentARC's GFPGAN face-restoration network to repair facial detail and optionally upscale the result.

Step 1 - Install the model

GFPGAN is a catalog model. Install the SDK once, then install the model to make its client importable:

terminal
pip install socaity
socaity install tencentarc/gfpgan

Step 2 - Initialise gfpgan

Import gfpgan from its deep catalog path under socaity.sdk.replicate.tencentarc and instantiate it with your API key. The client holds the service binding and is reusable across calls.

python
import os
from socaity.sdk.replicate.tencentarc import gfpgan

# gfpgan(api_key: str | None = None). If you leave api_key=None,
# the client falls back to the SOCAITY_API_KEY environment variable.
restore = gfpgan(api_key=os.getenv("SOCAITY_API_KEY"))

Step 3 - Pass a face image

Wrap your local file in ImageFile so the SDK uploads it correctly. ImageFile().from_file("face.png") reads the file from disk and hands it to the img parameter.

python
from socaity import ImageFile

# Wrap a local file so the SDK uploads it correctly.
face = ImageFile().from_file("face.png")  # replace with your own face photo

Step 4 - Restore and save

Call the client with img and an optional scale factor. The call returns a job; .get_result() blocks until the worker finishes and returns a single output file. Call .save() to write it to disk.

python
from socaity import ImageFile

# img is required; scale upscales the output (2 doubles the resolution).
out = restore(
    img=ImageFile().from_file("face.png"),  # replace with your own face photo
    scale=2,
).get_result()                  # single output file, no [0] needed

out.save("restored.png")
print("Saved restored.png")

JavaScript Alternative

The JavaScript SDK cannot run models yet, so use the Python SDK above to restore faces.

Parameters

Key parameters on the predictions endpoint. Defaults apply when omitted.

ParameterTypeDefaultDescription
imgImageFile | strrequiredFace image to restore. Wrap a local path with ImageFile().from_file(...).
scalenumber2Upscale factor applied to the output. Higher values produce a larger image.
versionstrmodel defaultGFPGAN model version selector.

Tips

  • GFPGAN targets faces. For best results, feed it a photo where the face is clearly visible.
  • Use scale to upscale while you restore. A value of 2 doubles the output resolution.
  • Reuse one client across many images. The binding is cheap to hold and avoids re-instantiating per call.
  • Set SOCAITY_API_KEY in your environment so you never hardcode the key.

What You Built

  • Installed the hosted tencentarc/gfpgan catalog model.
  • Loaded a local face image with ImageFile().from_file.
  • Restored and upscaled the face in a single hosted call.
  • Saved the result with get_result().save().

Next steps