首先,致敬作者,他的代码库在:https://github.com/deepinsight/insightface
1.安装项目运行环境。
python=2.7
由于我的服务器上已经安装了cuda8.0,所以环境配置起来很容易,输入如下代码:
pip install -U six scipy scikit-learn opencv-python scikit-image easydict mxnet-cu80
中间遇到了一个问题:
python setup.py egg_info
2.下载pretrained_model。
一共下载两个,一个为人脸识别model,一个为性别年龄识别model,由于代码中必须加载这两个model,所以就算不用都要下载。
3.将model上传到insightface/models中。
如果有服务器就连接服务器。
4.运行人脸特征提取程序。
首先进入路径:
$INSIGHTFACE_HOME/deploy
路径下的test.py就是用来提取人脸特征的,稍微修改下test.py:
vim test.py
import face_model
import argparse
import cv2
import sys
import numpy as np
parser = argparse.ArgumentParser(description='face model test')
# general
parser.add_argument('--image-size', default='112,112', help='')
# 这个比较重要,人脸识别的model,我的经验是要用绝对路径
parser.add_argument('--model', default='/disk1t/insightface/insightface/models/model-r100-ii/model,0', help='path to load model.')
# 这个比较重要,年龄性别的model,我的经验是要用绝对路径
parser.add_argument('--ga-model', default='/disk1t/insightface/insightface/models/gamodel-r50/model,0', help='path to load model.')
parser.add_argument('--gpu', default=0, type=int, help='gpu id')
parser.add_argument('--det', default=0, type=int, help='mtcnn option, 1 means using R+O, 0 means detect from begining')
parser.add_argument('--flip', default=0, type=int, help='whether do lr flip aug')
parser.add_argument('--threshold', default=1.24, type=float, help='ver dist threshold')
args = parser.parse_args()
print(args)
# 加载model
model = face_model.FaceModel(args)
# 读取图片
img = cv2.imread('Tom_Hanks_54745.png')
# 模型加载图片
img = model.get_input(img)
# 获得特征
f1 = model.get_feature(img)
# 输出特征
print(f1)
#print(f1[0:10])
#gender, age = model.get_ga(img)
#print(gender)
#print(age)
#sys.exit(0)
#img = cv2.imread('/raid5data/dplearn/megaface/facescrubr/112x112/Tom_Hanks/Tom_Hanks_54733.png')
#f2 = model.get_feature(img)
#dist = np.sum(np.square(f1-f2))
#print(dist)
#sim = np.dot(f1, f2.T)
#print(sim)
#diff = np.subtract(source_feature, target_feature)
#dist = np.sum(np.square(diff),1)
5.最后获得特征
6.到此,人脸特征提取就完成了
版权声明:本文为xiaotuzigaga原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。