最近需要提取人脸landmark,使用的是face-alignment:https://github.com/1adrianb/face-alignment
发现按照作者给出的Binaries
安装方式总会报错
'face_alignment' has no attribute 'FaceAlignment'
因此切换为从source安装。
如果使用的是服务器,且没有root权限,那么最好为创建一个face-alignment的环境再安装。
# 环境搭建
git clone https://github.com/1adrianb/face-alignment
cd face_alignment
conda create -n FaceAlign python=3.7 # 这里版本任意3.5+即可
conda activate FaceAlign
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu116
# 以下包按需安装
conda install matplotlib
conda install -c conda-forge ffmpeg
conda install -c conda-forge opencv
# 安装face_alignment
pip install -r requirements.txt
conda install -c 1adrianb face_alignment
python setup.py install
codes of get landmarks
import face_alignment
from skimage import io
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, flip_input=False)
input = io.imread('/home/wyw/tes_imgs/src_0.jpg')
preds = fa.get_landmarks(input)
print(preds)
运行后会先下载模型,然后输出landmarks
$ CUDA_VISIBLE_DEVICES=9 python3 face_alignment.py
Downloading: "https://www.adrianbulat.com/downloads/python-fan/s3fd-619a316812.pth" to /home/wyw/.cache/torch/hub/checkpoints/s3fd-619a316812.pth
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 85.7M/85.7M [1:24:15<00:00, 17.8kB/s]
Downloading: "https://www.adrianbulat.com/downloads/python-fan/2DFAN4-cd938726ad.zip" to /home/wyw/.cache/torch/hub/checkpoints/2DFAN4-cd938726ad.zip
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 91.9M/91.9M [1:19:52<00:00, 20.1kB/s]
[array([[ 59., 119.],
[ 59., 135.],
[ 64., 153.],
[ 66., 169.],
[ 75., 182.],
[ 84., 194.],
[ 98., 198.],
[111., 203.],
[131., 207.],
[154., 203.],
[167., 200.],
[179., 194.],
[188., 182.],
[194., 167.],
[197., 151.],
[199., 133.],
[201., 117.],
[ 77., 88.],
[ 84., 79.],
[ 95., 74.],
[104., 74.],
[113., 77.],
[147., 77.],
[156., 74.],
[165., 74.],
[176., 79.],
[185., 88.],
[131., 97.],
[131., 108.],
[134., 117.],
[134., 126.],
[120., 137.],
[127., 137.],
[134., 140.],
[138., 137.],
[143., 137.],
[ 91., 101.],
[ 98., 97.],
[104., 97.],
[113., 101.],
[107., 104.],
[ 98., 104.],
[147., 101.],
[156., 97.],
[163., 99.],
[170., 101.],
[165., 104.],
[154., 104.],
[111., 164.],
[118., 158.],
[129., 151.],
[134., 153.],
[138., 151.],
[147., 158.],
[156., 164.],
[149., 171.],
[140., 173.],
[134., 173.],
[125., 173.],
[120., 169.],
[111., 164.],
[127., 160.],
[134., 160.],
[140., 160.],
[154., 164.],
[140., 164.],
[134., 164.],
[127., 164.]], dtype=float32)]