# 3D Hair 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: The quality of the photo must be a good resolution.
{% endhint %}

{% hint style="warning" %}
Note: Hair should be clearly visible.
{% endhint %}

{% hint style="warning" %}
Note: So far, we are unable to work well with ponytails and pigtails.
{% endhint %}

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

## 3. Reconstruct 3D Hair

Make a request by sending your chosen photo. Make sure you are using the mode you really need. `'card'` mode is used by default.

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

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

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

{% endtab %}

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

```python
import requests

token = "ENTER_YOUR_TOKEN"
image_path = "ENTER_IMAGE_PATH"

if __name__ == "__main__":
    service_url = "https://api.metahumansdk.io/hair_recon"
    service_headers = {"accept": "application/json"}
    files = {
        "token": (None, token),
        "image_bytes": ("image", open(image_path, "rb"), "image/png"),
        "mode": (None, "card")  # card or volume
    }
    response_config = requests.post(service_url + "/run_pipeline", headers=service_headers, files=files, stream=True)
    with open("hair.glb", "wb") as f:
        f.write(response_config.content)

```

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

The expected result is the `hair.glb` file.&#x20;

## 4. Check GLB model

Check saved file in GLB viewer (for example in Blender).

### Examples of results

Open in Blender

<div align="center"><figure><img src="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2F1UxYgRGoTwoYy3tJ4oqi%2Fyoung-beautiful-woman-pink-warm-sweater-natural-look-smiling-portrait-isolated-long-hair.png?alt=media&#x26;token=d394a031-950a-4957-b14a-b30f1859a77c" alt="" width="563"><figcaption><p>Original photo</p></figcaption></figure></div>

<div><figure><img src="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2F2ZzgTbMiRdzn7LDBtNcA%2Fnatural_look_hair_card_raw.png?alt=media&#x26;token=c6547144-bb75-40f0-bc05-295e3983a33d" alt="" width="375"><figcaption><p>Reconstructed hair mesh in <code>card</code> mode</p></figcaption></figure> <figure><img src="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2FoXz3mIKKjNXgrot8cNo6%2Fnatural_look_hair_stylized_raw.png?alt=media&#x26;token=68fd2a14-ce3c-42b2-9cee-36dc8d8e8923" alt="" width="375"><figcaption><p>Reconstructed hair mesh in <code>volume</code> mode</p></figcaption></figure></div>

### Example rendering

Rendering in Blender

<div><figure><img src="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2F9MyHEP25fIcDNMX12lFF%2Fnatural_look_hair_card_render.png?alt=media&#x26;token=074aebe4-cd9b-49ef-96bd-16ffc4295088" alt="" width="375"><figcaption><p>Rendered hair mesh in <code>card</code> mode</p></figcaption></figure> <figure><img src="https://1042198524-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6orDwlXzPvb3WASo4u11%2Fuploads%2F6J7IDHVXzKENt2TKRIng%2Fnatural_look_hair_stylized_render.png?alt=media&#x26;token=e599da59-6272-4382-a7bc-bbcc75905dfd" alt="" width="375"><figcaption><p>Rendered hair mesh in <code>volume</code> mode</p></figcaption></figure></div>

## 5. Visualization with head

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

{% hint style="danger" %}
GLB Constructor currently supports only hair i&#x6E;***`volume`*** mode
{% endhint %}

{% hint style="info" %}
At this step it is assumed that you have already received `uv.png` and `head_config.json`from 3D Face Reconstruction tab.
{% endhint %}

{% 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 'custom_models=@hair.glb;type=image/png' \
    -F 'add_facs=true' > head.glb
```

{% endtab %}

{% tab title="python" %}

```python
import io
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())
    with open('hair.glb', 'rb') as out:
        custom_hair = out.read()

    files = {
        'token': (None, token),
        'head_uv': ('head_model', head_uv, 'image/png'),
        'head_config': ('head_config', json.dumps(head_config), 'application/json'),
        'custom_models': ('custom_models', io.BytesIO(custom_hair), '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` .

Result:

<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%2F9EqrQP18kena3CTGq4ww%2Fspaces_6orDwlXzPvb3WASo4.png?alt=media&#x26;token=f9df3087-bcb5-459a-b759-02c7b24eb327" alt=""><figcaption><p>head.glb</p></figcaption></figure></div>
