3DDFA_V2

repository·master·Indexed 25 days ago

https://github.com/cleardusk/3ddfa_v2

A framework for fast, accurate, and stable 3D dense face alignment. It provides tools for 2D/3D landmark detection, head pose estimation, depth estimation, and UV texture mapping, optimized for high-speed CPU inference using ONNX runtime. The project includes FaceBoxes for face detection and Sim3DR for 3D rendering.

Tokens
2.3K
Snippets
9
Records
18
Agent score
79%

What's inside 3DDFA_V2

  1. Download pre-converted ONNX models

    master

    The 3DDFA_v2 project requires pre-trained model weights. You can download the pre-converted ONNX models via Google Drive or Baidu Drive using the links and passwords provided below.

    | Model | Link | 
    | :-: | :-: | 
    | `mb1_120x120.onnx` | [Google Drive](https://drive.google.com/file/d/1YpO1KfXvJHRmCBkErNa62dHm-CUjsoIk/view?usp=sharing) or [Baidu Drive](https://pan.baidu.com/s/1qpQBd5KOS0-5lD6jZKXZ-Q) (Password: cqbx) | 
    | `mb05_120x120.onnx` | [Google Drive](https://drive.google.com/file/d/1orJFiZPshmp7jmCx_D0tvIEtPYtnFvHS/view?usp=sharing) or [Baidu Drive](https://pan.baidu.com/s/1sRaBOA5wHu6PFS1Qd-TBFA) (Password: 8qst) | 
    | `resnet22.onnx` | [Google Drive](https://drive.google.com/file/d/1rRyrd7Ar-QYTi1hRHOYHspT8PTyXQ5ds/view?usp=sharing) or [Baidu Drive](https://pan.baidu.com/s/1Nzkw7Ie_5trKvi1JYxymJA) (Password: 1op6) | 
    | `resnet22.pth` | [Google Drive](https://drive.google.com/file/d/1dh7JZgkj1IaO4ZcSuBOBZl2suT9EPedV/view?usp=sharing) or [Baidu Drive](https://pan.baidu.com/s/1IS7ncVxhw0f955ySg67Y4A) (Password: lv1a) |
  2. Install and Setup 3DDFA_V2

    master

    To use 3DDFA_V2, clone the repository and build the required Cython components (NMS, Sim3DR, and the faster mesh render).

    Requirements:

    • Python 3
    • Major dependencies: PyTorch, numpy, opencv-python, and onnxruntime.
    • On macOS, if using the --onnx flag for acceleration, install libomp via brew install libomp.

    Installation Steps:

    1. Clone the repo:
    git clone https://github.com/cleardusk/3DDFA_V2.git
    cd 3DDFA_V2
    1. Build components:
    sh ./build.sh
    git clone https://github.com/cleardusk/3DDFA_V2.git
    cd 3DDFA_V2
    sh ./build.sh
  3. Optimize CPU Latency with ONNX and Threads

    master

    To achieve maximum performance on CPU, use the --onnx flag. You can further control performance by setting the number of OpenMP threads using the OMP_NUM_THREADS environment variable.

    Example of setting threads before running:

    export OMP_NUM_THREADS=4
    python3 demo.py -f examples/inputs/emma.jpg --onnx
  4. Run 3DDFA_V2 Demos

    master

    The project provides several scripts for different input types. Use the --onnx flag to enable ONNX runtime acceleration, which significantly reduces CPU latency.

    • Still Images: Use demo.py. You can specify output modes using the -o flag.
    • Videos: Use demo_video.py for standard video processing or demo_video_smooth.py for smoother tracking by looking ahead frames.
    • Webcam: Use demo_webcam_smooth.py for real-time webcam tracking.
  5. Build the CPU version of NMS for FaceBoxes

    master

    To use FaceBoxes, you may need to build the CPU version of the Non-Maximum Suppression (NMS) extension. You can do this using the provided build script or the shell script.

    # Option 1: Using the build script
    cd utils
    python3 build.py build_ext --inplace
    
    # Option 2: Using the shell script
    sh ./build_cpu_nms.sh
  6. Configure Model Backbones

    master

    3DDFA_V2 supports different model configurations via the -c or --config flag.

    • Default: MobileNet_V1 (120x120 input, ~3.27M params).
    • Fast/Small: MobileNet x0.5 (120x120 input, ~0.85M params).

    Config files are located in the configs/ directory (e.g., configs/mb1_120x120.yml or configs/mb05_120x120.yml).

  7. Initialize FaceBoxes and TDDFA

    master

    To run the 3D Dense Face Alignment, you need to load a configuration file and initialize both the FaceBoxes detector and the TDDFA model. It is recommended to use the onnx flag for improved speed. If using ONNX, you may need to set specific environment variables for thread management.

    import yaml
    import os
    from FaceBoxes.FaceBoxes_ONNX import FaceBoxes_ONNX
    from TDDFA_ONNX import TDDFA_ONNX
    from TDDFA import TDDFA
    from FaceBoxes import FaceBoxes
    
    # Load config
    cfg = yaml.load(open('configs/mb1_120x120.yml'), Loader=yaml.SafeLoader)
    
    # Recommended: Use ONNX for speed
    onnx_flag = True
    if onnx_flag:
        os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
        os.environ['OMP_NUM_THREADS'] = '4'
        face_boxes = FaceBoxes_ONNX()
        tddfa = TDDFA_ONNX(**cfg)
    else:
        tddfa = TDDFA(gpu_mode=False, **cfg)
        face_boxes = FaceBoxes()