# 3D Face Reconstruction

## 1. Getting a token

[Getting a token](/service-apis-for-creating-virtual-avatars/getting-started/getting-a-token.md)

## 2. Choice of photo

Choose a photo of a person.

{% hint style="warning" %}
Note: Please send a photo of one person.
{% endhint %}

{% hint style="warning" %}
Note: The quality of the photo must be a fairly good resolution.
{% endhint %}

{% hint style="warning" %}
Note: The face should not be a full face and not covered by foreign objects.
{% endhint %}

{% hint style="warning" %}
Note: Lighting should not have hard drops and shadows.
{% endhint %}

{% hint style="warning" %}
Note: The photo should have neutral lighting. Otherwise the skin color will be unrealistic.
{% endhint %}

{% hint style="warning" %}
Note: The correct result is issued for persons over 18 years of age. Children may have problems with gender determination.
{% endhint %}

{% hint style="warning" %}
Note: The best result will be with neutral facial emotions.
{% endhint %}

{% hint style="info" %}
Need a successful image? [Download a sample](https://www.freepik.com/download-file/9631091)
{% endhint %}

## 3. Reconstruct 3D Face

Make a request by sending your chosen photo.

{% tabs %}
{% tab title="curl" %}

```bash
token="ENTER_YOUR_TOKEN"
image_path="ENTER_IMAGE_PATH"

curl -X 'POST' 'https://api.metahumansdk.io/face_recon/run_pipeline' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F "token=$token" \
  -F "image_bytes=@$image_path;type=image/png" > head_config.json
```

{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import requests
import json

token = "ENTER_YOUR_TOKEN"
image_path = "ENTER_IMAGE_PATH"

if __name__ == "__main__":
    service_url = "https://api.metahumansdk.io/face_recon"
    service_headers = {"accept": "application/json"}
    files = {
        "token": (None, token),
        "image_bytes": ("image", open(image_path, "rb"), "image/png")
    }
    response = requests.post(service_url + "/run_pipeline", headers=service_headers, files=files, timeout=60)
    if response.status_code == 200:
        with open("head_config.json", "w+") as f:
            json.dump(response.json(), f)

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Note: Do not forget to replace the details in the request with your own.
{% endhint %}

Expected output:

After successful execution of the code, you should receive the following type of json in `head_config.json` file:

```json
{
  "result": {
    "face": {
      "textureUrl": str,
      "blendShapes": list,
      "blinkCorrectionCoeff": double
    },
    "hair": {
      "color": hex color,
      "type": str
    },
    "skin": {
      "color": hex color
    },
    "accessories": list,
    "gender": str
  }
}
```

At the moment we have received information about the person. Now let's download the face texture.

## 4. Download UV face texture

The json we received in the last step contains the url where we can download the texture.

{% tabs %}
{% tab title="curl" %}

```bash
texture_url=$(cat head_config.json | python -c 'import sys, json; print(json.load(sys.stdin)["result"]["face"]["textureUrl"])')

curl -X 'GET' "https://api.metahumansdk.io/face_recon/${texture_url}" > uv.png
```

{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import requests
import json

if __name__ == "__main__":
    service_url = "https://api.metahumansdk.io/face_recon"
    service_headers = {"accept": "application/json"}
    with open("head_config.json", 'r') as f:
        config = json.load(f)
    response = requests.get(service_url + config["result"]["face"]["textureUrl"])
    assert (response.status_code == 200)
    with open("uv.png", "wb") as f:
        f.write(response.content)
```

{% endcode %}
{% endtab %}
{% endtabs %}

After the completed stages, you get a uv texture `uv.png` and information about the face `head_config.json`.&#x20;

## 5. Visualization

In order to get a 3D model of the head, we will use GLB Constructor:

{% tabs %}
{% tab title="curl" %}

```bash
token="ENTER_YOUR_TOKEN"

curl -X 'POST' 'https://api.metahumansdk.io/glb_const/assemble' \
    -H 'accept: application/json' \
    -H 'Content-Type: multipart/form-data' \
    -F "token=$token" \
    -F 'head_uv=@uv.png;type=image/png' \
    -F 'head_config=@head_config.json;type=application/json' \
    -F 'add_facs=true' > head.glb
```

{% endtab %}

{% tab title="python" %}

```python
import json
import requests

token = "ENTER_YOUR_TOKEN"

if __name__ == "__main__":
    service_url = "https://api.metahumansdk.io/glb_const"
    service_headers = {'accept': 'application/json'}
    
    with open('uv.png', 'rb') as out:
        head_uv = out.read()
    with open('head_config.json', 'r') as out:
        head_config = json.loads(out.read())

    files = {
        'token': (None, token),
        'head_uv': ('head_model', head_uv, 'image/png'),
        'head_config': ('head_config', json.dumps(head_config), 'application/json'),
        'add_facs': (None, True),
    }

    response = requests.post(service_url + "/assemble", headers=service_headers, files=files)
    assert (response.status_code == 200)

    with open("head.glb", "wb") as f:
        f.write(response.content)

```

{% endtab %}
{% endtabs %}

After the completed stages, you get a glb model  `head.glb` .

<div><figure><img src="/files/PImA6rDwLkeQK20FsZfN" alt=""><figcaption><p>image.png</p></figcaption></figure> <figure><img src="/files/RvwqECruARSdVDBw5l1a" alt=""><figcaption><p>head.glb</p></figcaption></figure></div>

To add hair to the head model please follow to [3D Hair Reconstruction](/service-apis-for-creating-virtual-avatars/getting-started/3d-hair-reconstruction.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.metahumansdk.io/service-apis-for-creating-virtual-avatars/getting-started/3d-face-reconstruction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
