NSDT工具推荐Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

本文将讨论如何使用开源中提供的预训练模型从图像中提取人脸编码或人脸嵌入。我还附上了代码,请参阅此 git仓库

1、人脸嵌入

它分析给定的图像并返回表示图像中检测到的每个人脸的数值向量。向量的大小不同,为 64、128、256、512。这里我们将讨论返回 128 个大小向量的模型。

我们可以使用这种嵌入来执行人脸识别、人脸验证和人脸匹配应用程序。

这是一种基于深度学习的方法来表示个人面部的身份。名为 FaceNet 的架构用于提取人脸嵌入,要了解有关它的更多信息,请参阅这个链接

2、Dlib

我们可以使用 Dlib 来定位图像中的人脸,如上一篇博客中所述。此外,使用它可以提取图像中人脸的人脸编码向量。模型 dlib_face_recognition_resnet_model_v1.dat 用于提取 Dlib 模块中的编码。

在这里,我们需要说出给定图像中人脸的位置。Dlib 的优势在于,它是一个轻量级模型,即使在计算能力较低的 CPU 中也能运行,并且与其他模型相比,推理时间也较短。Dlib 的一个优点是,默认情况下它有一个面部检测模块。

# Load the model using Dlib
dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat')  

获取编码的函数:

def encodings(img,face_locations,pose_predictor,face_encoder):
    predictors = [pose_predictor(img, face_location) for face_location in face_locations]
    return [np.array(face_encoder.compute_face_descriptor(img, predictor, 1)) for predictor in predictors]

源代码可在此处获得。姿势预测模型可在此处下载。

3、TensorFlow 模型

还有一个预先训练的 TensorFlow 模型,可用于提取图像中人脸的编码。两种情况下使用的架构相同,但损失函数和训练数据会发生变化。因此,从中返回的编码向量将与之前的模型不同,但其大小为 128 维。

在这里,对于一个模型,我们只需要传递裁剪后的图像,其中只有一张脸,因为它会检测图像中的脸,它只需读取图像并将图像传递到网络中并返回一个向量,即人脸编码。

为了处理上述情况,我们需要从图像中裁剪人脸并将其传递到模型中。要检测和裁剪图像中的脸,请使用上一篇博客中讨论的任何一种方法。

为了加载模型...

def load_model(modelpath):
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(modelpath, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
    return detection_graph

计算嵌入:

def get_embedding(graph,img):
    input_array = preprocess_input_img(img)
    with graph.as_default():
        with tf.Session() as sess:
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]
            feed_dict = {images_placeholder: input_array, phase_train_placeholder: False}
            embeddings = sess.run(embeddings, feed_dict=feed_dict)
    return embeddings

需要传递模型图和图像(如果需要,可以传递预处理图像)。

我们还可以根据机器容量一次传递多个图像,以设置批处理大小。

它在 CPU 和 GPU 上都运行,而在 GPU 上运行速度更快。

源代码在这里。可以从这里下载模型。如果需要,还可以尝试其他一些 facenet TensorFlow 模型。

比较这些模型,与 TensorFlow 模型相比,Dlib 简单且在提取人脸嵌入的情况下复杂度较低。但在具有高计算能力的情况下,TF 模型表现良好。

我在这里附上了源代码链接,供你参考。


原文链接:Extract Face Embedding from Image

BimAnt翻译整理,转载请标明出处