# 3D Face Reconstruction

## 1. Getting a token

[Getting a token](https://docs.metahumansdk.io/service-apis-for-creating-virtual-avatars/getting-started/getting-a-token)

## 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="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2FrdaPVAom7WGsLCoifcML%2Fimage.png?alt=media&#x26;token=9a12e770-95f1-4b36-a823-c0105ba8f386" alt=""><figcaption><p>image.png</p></figcaption></figure> <figure><img src="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2FykbNVxly7Ba6ti0rWsHp%2FScreen%20Shot%202023-04-07%20at%2016.47.39.png?alt=media&#x26;token=8879038d-b7e0-474b-869f-d18e8e0f43d1" alt=""><figcaption><p>head.glb</p></figcaption></figure></div>

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