Home

Awesome

<h1 align="center"><b>FLAME Head Tracker</b></h1> <div align="center"> <a href="https://opensource.org/licenses/MIT"> <img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"> </a> </div> <div align="center"> <b><img src="./assets/demo.gif" alt="drawing" width="600"/></b> <br> (First two example videos were from IMavatar: <a href="https://github.com/zhengyuf/IMavatar">https://github.com/zhengyuf/IMavatar</a>) </div>

Author: Peizhi Yan
Date Updated: 11-20-2024
Version: 2.0

Supported Features:


πŸ“–Table of Contents

πŸ“ƒ Todos

<details> <summary><b>Todo List</b></summary> </details>

🧸 Citation

This code was originally used for "Gaussian Deja-vu" (accepted for WACV 2025 in Round 1). Please consider citing our work if you find this code useful.

@article{yan2024gaussian,
  title={Gaussian Deja-vu: Creating Controllable 3D Gaussian Head-Avatars with Enhanced Generalization and Personalization Abilities},
  author={Yan, Peizhi and Ward, Rabab and Tang, Qiang and Du, Shan},
  journal={arXiv preprint arXiv:2409.16147},
  year={2024}
}

πŸ¦– Usage

Return to top

<details> <summary><b>Single-Image-Based Reconstruction</b></summary>

Please follow the example in: ./Example_single-image-reconstruction.ipynb

from tracker_base import Tracker

tracker_cfg = {
    'mediapipe_face_landmarker_v2_path': './models/face_landmarker_v2_with_blendshapes.task',
    'flame_model_path': './models/FLAME2020/generic_model.pkl',
    'flame_lmk_embedding_path': './models/landmark_embedding.npy',
    'tex_space_path': './models/FLAME_albedo_from_BFM.npz',
    'face_parsing_model_path': './utils/face_parsing/79999_iter.pth',
    'uv_coord_mapping_file_path': './models/uv2vert_256.npy',
    'template_mesh_file_path': './models/head_template.obj',
    'result_img_size': 512,
    'device': device,
}

tracker = Tracker(tracker_cfg)

ret_dict = tracker.load_image_and_run(img_path, realign=True, photometric_fitting=False)

The result ret_dict contains the following data:

Example Reconstruction Result (realign=True):

Example Reconstruction Result (realign=False):

</details> <details> <summary><b>Monocular Video-Based Tracking</b></summary>

Please follow the example in: ./Example_video-reconstruction.ipynb

from tracker_video import track_video

tracker_cfg = {
    'mediapipe_face_landmarker_v2_path': './models/face_landmarker_v2_with_blendshapes.task',
    'flame_model_path': './models/FLAME2020/generic_model.pkl',
    'flame_lmk_embedding_path': './models/landmark_embedding.npy',
    'tex_space_path': './models/FLAME_albedo_from_BFM.npz',
    'face_parsing_model_path': './utils/face_parsing/79999_iter.pth',
    'uv_coord_mapping_file_path': './models/uv2vert_256.npy',
    'template_mesh_file_path': './models/head_template.obj',
    'result_img_size': 512,
    'device': device,
    ## following are used for video tracking
    'original_fps': 60,       # input video fps
    'subsample_fps': 30,      # subsample fps
    'video_path': './assets/IMG_2647.MOV',  # example video
    'save_path': './output',  # tracking result save path
    'use_kalman_filter': False, # whether to use Kalman filter
    'kalman_filter_measurement_noise_factor': 1e-5, # measurement noise level in Kalman filter 
    'kalman_filter_process_noise_factor': 1e-5,     # process noise level in Kalman filter 
}

## Note that, the first frame will take longer time to process
track_video(tracker_cfg)

The results will be saved to the save_path. The reconstruction result of each frame will be saved to the corresponding [frame_id].npy file.

</details>

πŸ–₯️ Environment Setup

Return to top

<details> <summary><b>Details</b></summary>

Prerequisites:

Step 1: Create a conda environment.

conda create --name tracker -y python=3.10
conda activate tracker

Step 2: Install necessary libraries.

Nvidia CUDA compiler (11.7)

conda install -c "nvidia/label/cuda-11.7.1" cuda-toolkit ninja

# (Linux only) ----------
ln -s "$CONDA_PREFIX/lib" "$CONDA_PREFIX/lib64"  # to avoid error "/usr/bin/ld: cannot find -lcudart"

# Install NVCC (optional, if the NVCC is not installed successfully try this)
conda install -c conda-forge cudatoolkit=11.7 cudatoolkit-dev=11.7

After install, check NVCC version (should be 11.7):

nvcc --version

PyTorch (2.0 with CUDA)

pip install torch==2.0.1 torchvision --index-url https://download.pytorch.org/whl/cu117

Now let's test if PyTorch is able to access CUDA device, the result should be True:

python -c "import torch; print(torch.cuda.is_available())"

Some Python packages

pip install -r requirements.txt

Nvidia Differentiable Rasterization: nvdiffrast

Note that, we use nvdiffrast version 0.3.1, other versions may also work but not promised.

# Download the nvdiffrast from their official Github repo
git clone https://github.com/NVlabs/nvdiffrast

# Go to the downloaded directory
cd nvdiffrast

# Install the package
pip install .

# Change the directory back
cd ..

Pytorch3D

Note that, we use pytorch3d version 0.7.8, other versions may also work but not promised.

Installing pytorch3d may take a bit of time.

# Download Pytorch3D from their official Github repo
git clone https://github.com/facebookresearch/pytorch3d

# Go to the downloaded directory
cd pytorch3d

# Install the package
pip install .

# Change the directory back
cd ..

Troubleshoot

Note that the NVCC needs g++ < 12:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 50
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-11 50

If there is problem with nvdiffrast, check whether it is related to the EGL header file in the error message. If it is, install the EGL Development Libraries (for Ubuntu/Debian-based systems):

sudo apt-get update
sudo apt-get install libegl1-mesa-dev

Then, uninstall nvdiffrast and reinstall it.

Step 3: Download some necessary model files.

Because of copyright concerns, we cannot re-share any of the following model files. Please follow the instructions to download the necessary model file.

FLAME and DECA

Mediapipe

The final structure of ./models/ is:

./models
    β”œβ”€β”€ deca_model.tar
    β”œβ”€β”€ face_landmarker_v2_with_blendshapes.task
    β”œβ”€β”€ fixed_displacement_256.npy
    β”œβ”€β”€ FLAME2020
    β”‚Β Β  β”œβ”€β”€ female_model.pkl
    β”‚Β Β  β”œβ”€β”€ generic_model.pkl
    β”‚Β Β  β”œβ”€β”€ male_model.pkl
    β”‚Β Β  └── Readme.pdf
    β”œβ”€β”€ FLAME_albedo_from_BFM.npz
    β”œβ”€β”€ head_template.obj
    β”œβ”€β”€ landmark_embedding.npy
    β”œβ”€β”€ mean_texture.jpg
    β”œβ”€β”€ placeholder.txt
    β”œβ”€β”€ texture_data_256.npy
    β”œβ”€β”€ uv_face_eye_mask.png
    └── uv_face_mask.png
</details>

βš–οΈ Acknowledgements and Disclaimer

Return to top

Our code is mainly based on the following repositories:

We want to acknowledge the contributions of the authors of these repositories. We do not claim ownership of any code originating from these repositories, and any modifications we have made are solely for our specific use case. All original rights and attributions remain with the respective authors.

Disclaimer

Our code can be used for research purposes, provided that the terms of the licenses of any third-party code, models, or dependencies are followed. For commercial use, the parts of code we wrote are for free, but please be aware to get permissions from any third-party to use their code, models, or dependencies. We do not assume any responsibility for any issues, damages, or liabilities that may arise from the use of this code. Users are responsible for ensuring compliance with any legal requirements, including licensing terms and conditions, and for verifying that the code is suitable for their intended purposes.