源码地址:https://github.com/microsoft/Deep3DFaceReconstruction
在服务器上的位置:/dd/xiongz/Deep3DFaceReconstruction

运行

  1. 首先激活环境:source activate deep3d
  2. 在程序目录下,把所有需要进行重建的人脸图片(pngjpg格式)放到./input文件夹,运行python get5landmarks.py,生成与图片同名且同路径的txt文件,内容是人脸的5个关键点。
  3. 然后运行python demo.py,生成重建的3D人脸obj文件及参数mat文件,位于./output文件夹内。

注:input和output内现有的文件为测试文件,可全部删除。

输出示例

运行python demo.py./input文件夹下的每个 jpg 和 png 文件都会对应输出obj文件,保存于./output
输出的内容
For each input test image, two output files can be obtained after running the demo code:

  • “xxx.mat” :
    • cropped_img: an RGB image after alignment, which is the input to the R-Net
    • recon_img: an RGBA reconstruction image aligned with the input image (only on Linux).
    • coeff: output coefficients of R-Net.
    • face_shape: vertex positions of 3D face in the world coordinate.
    • face_texture: vertex texture of 3D face, which excludes lighting effect.
    • face_color: vertex color of 3D face, which takes lighting into consideration.
    • lm_68p: 68 2D facial landmarks derived from the reconstructed 3D face. The landmarks are aligned with cropped_img.
    • lm_5p: 5 detected landmarks aligned with cropped_img.
  • “xxx_mesh.obj” : 3D face mesh in the world coordinate (best viewed in MeshLab).

image.png
37fca60e82c36faa6641298c7cc6e56.png
左图是输入的图片,右图是3D mesh。

安装时遇到的bug与解决

GnuTLS recv error (-110)

GnuTLS recv error (-110): The TLS connection was non-properly terminated.

  1. apt-get install gnutls-bin
  2. git config --global http.sslVerify false
  3. git config --global http.postBuffer 1048576000

bazel 安装

curl 超时,下载到本地上传服务器,curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg拆开运行。但是后面sudo apt update的时候报错,搜到解决方法是curl [https://bazel.build/bazel-release.pub.gpg](https://bazel.build/bazel-release.pub.gpg) | sudo apt-key add -,同样分开运行,但apt-key list里没有新增。
解决:文件上传到云存储,替换链接(也可能是我之前上传错了文件,文件是空的)。
18d8e1e70bab99d1ae48b4cd2422afe.png

bazel test …

  1. ERROR: Skipping ‘../..’: Bad target pattern ‘../..’: package name component contains only ‘.’ characters

image.png
因为是用的zsh,一些shells扩展了...,这里改成 bazel build "..."

  1. ERROR: An error occurred during the fetch of repository ‘com_google_googletest’:

image.png
最后成功的操作:

  1. bazel clean --expunge
  2. 报错 https://github.com/google/googletest/archive/master.zip 下载超时,修改/tf_mesh_renderer/WORKSPACE如下

    1. http_archive(
    2. name = "com_google_googletest",
    3. urls = ["https://github.com/google/googletest/archive/master.zip"],
    4. strip_prefix = "googletest-main",
    5. )
  3. bazel build "..." 之前这一步有2个fail:mesh_renderer_test 和 rasterize_triangles_test,按这里的答案 https://github.com/yiranran/Audio-driven-TalkingFace-HeadPose/issues/3 没有解决。按上面2步走没有报 failed 了。

不过,有一堆unsupported,比如
image.png

获取人脸5个关键点的代码

将图片放在 /input 文件夹下,需要自己实现获取人脸5个关键点,可用dlib或MTCNN来做。关键点位置保存为与图片同名的 txt 文件,与图片同路径。

  1. from mtcnn import MTCNN
  2. import os
  3. import cv2
  4. import argparse
  5. import tensorflow as tf
  6. os.environ['CUDA_VISIBLE_DEVICES']='0'
  7. # tf 1.13
  8. config = tf.ConfigProto()
  9. config.gpu_options.allow_growth=True
  10. sess = tf.Session(config=config)
  11. sess.run(tf.global_variables_initializer())
  12. parser = argparse.ArgumentParser(description='Get landmarks from images.')
  13. parser.add_argument('--input_dir', type=str, default="./input",
  14. help='directory with the input images')
  15. args = parser.parse_args()
  16. input_dir = args.input_dir
  17. detector = MTCNN()
  18. for filename in os.listdir(input_dir):
  19. print('filename: ', filename)
  20. basename = os.path.splitext(filename)[0]
  21. image_path = f"{input_dir}/{filename}"
  22. text_path = f"{input_dir}/{basename}.txt"
  23. img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
  24. result = detector.detect_faces(img)
  25. keypoints = result[0]['keypoints']
  26. text_file = open(text_path, "a")
  27. for value in keypoints.values():
  28. text_file.write(f"{value[0]}\t{value[1]}\n")
  29. print(f"File successfully written: {text_path}")
  30. text_file.close()

自测结果

输入一个人:
模型加载的时间:1.88
图像处理的时间:0.02
模型预测时间: 7.19 sec
image.png
一次运行处理多个人脸重建:
模型加载的时间:1.85
1 input/001front.png
图像处理的时间:0.09
模型预测时间: 7.19 sec
2 input/002front.png
图像处理的时间:0.07
模型预测时间: 0.02 sec
3 input/front.jpg
图像处理的时间:0.00
模型预测时间: 0.02 sec
image.png
image.png
image.png