使用神经网络从单个图像进行 3D 人脸建模的 Python 代码。
conda
. 安装 caffé、dlib 和 libhdf5 需要更多的努力。
在运行代码之前,请确保在以下特定文件夹中包含所有必需的数据:
3dmm_cnn_resnet_101.caffemodel
,deploy_network.prototxt
,mean.binaryproto
)移动到CNN
文件夹01_MorphableModel.mat
到3DMM_model
文件夹Model_Expression.mat
并将此文件移动到3DMM_model
文件夹3DMM_model
文件夹。运行脚本python trimBaselFace.py
。这应该输出 2 个文件BaselFaceModel_mod.mat
和BaselFaceModel_mod.h5
..dat
文件移动到dlib_model
文件夹中。[hidecontent type="logged" desc="隐藏内容:登录后可查看"]
apt-get install cmake
(http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html)
apt-get install libboost-all-dev
sudo apt-get install freeglut3-dev
sudo apt-get install libglew-dev
sudo apt-get install libhdf5-dev
(http://dlib.net/)
DLIB_INCLUDE_DIR
和DLIB_LIB_DIR
)在CMakeLists.txt
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=../bin ..
make
make install
TestVisualization
此代码应在bin
文件夹中生成。
demoCode
文件夹。可以使用以下语法从命令行使用演示脚本:$ Usage: python testBatchModel.py <inputList> <outputDir> <needCrop> <useLM>
其中参数如下:
<inputList>
是一个文本文件,其中包含每个输入图像的路径,每行一个。<outputDir>
是存储 ply 文件的输出目录的路径。<needCrop>
告诉演示图像是否需要裁剪 (1) 或不需要 (0)。默认 1。如果您的输入图像大小相等(正方形)并且具有类似 CASIA 的 [2] 边界框,您可以设置<needCrop>
为 0。否则,您必须将其设置为 1。<useLM>
是使用检测到的地标 (1) 或不使用 (0) 来细化边界框的选项。默认 1。例如<inputList>
:
数据/1.jpg 数据/2.jpg ....
user@system:~/Desktop/3dmm_release$ python testBatchModel.py input.txt out/
> Prepare image data/1.jpg:
> Number of faces detected: 1
> Prepare image data/2.jpg:
> Number of faces detected: 1
> CNN Model loaded to regress 3D Shape and Texture!
> Loaded the Basel Face Model to write the 3D output!
> Processing image: tmp_ims/2.png 2.png 1/2
> Writing 3D file in: out//2.ply
> Processing image: tmp_ims/1.png 1.png 2/2
> Writing 3D file in: out//1.ply
可以使用标准的现成 3D(层文件)可视化软件(例如MeshLab)显示最终的 3D 形状和纹理。使用 MeshLab,输出可能显示如下:
user@system:~/Desktop/3dmm_release$ meshlab out/1.ply
user@system:~/Desktop/3dmm_release$ meshlab out/2.ply
这应该产生类似于:
demoCode
文件夹。可以使用以下语法从命令行使用演示脚本:$ Usage: python testModel_PoseExpr.py <outputDir> <save3D>
其中参数如下:
<outputDir>
是输出目录的路径,其中存储 3DMM(和层)文件。<save3D>
是保存层文件 (1) 或不保存 (0) 的选项。默认 1。例子:
user@system:~/Desktop/3dmm_release$ python testModel_PoseExpr.py out/
(Select `Anders_Fogh_Rasmussen_0004.jpg`)
> Prepare image /home/anh/Downloads/PoseExprFromLM-master/demoCode/data/Anders_Fogh_Rasmussen_0004.jpg:
Number of faces detected: 1
> CNN Model loaded to regress 3D Shape and Texture!
> Loaded the Basel Face Model to write the 3D output!
*****************************************
** Caffe loading : 1.007 s
** Image cropping : 0.069 s
** 3D Modeling : 1.145 s
*****************************************
> Writing 3D file in: out/Anders_Fogh_Rasmussen_0004.ply
> Pose & expression estimation
load ../3DMM_model/BaselFaceModel_mod.h5
** Pose+expr fitting: 0.153 s
** Visualization : 0.052 s
*****************************************
弹出窗口应类似于:
"F0210 10:49:17.604714 24046 net.cpp:797] Check failed:
target_blobs.size() == source_layer.blobs_size() (5 vs. 3) Incompatible
number of blobs for layer bn_conv1"
有关 caffe verson 的更多信息,请参阅https://github.com/BVLC/caffe/releases
从 python 检查你的 caffe 版本:
In [3]: import caffe
In [4]: caffe.__version__
Out[4]: '1.0.0-rc3'
[/hidecontent]