先使用MobileFaceNet的预训练权重文件,暂时先不考虑迁移训练,到业务测试的结果那一步再考虑。先简单实现一个人脸特征向量提取的功能。然后再加上人脸检测。

import os
import cv2
import numpy as np

# tensorflow v1兼容模式
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

#加载模型
def load_model(model):
    # Check if the model is a model directory (containing a metagraph and a checkpoint file)
    #  or if it is a protobuf file with a frozen graph
    model_exp = os.path.expanduser(model)
    if (os.path.isfile(model_exp)):
        print('Model filename: %s' % model_exp)
        with tf.gfile.FastGFile(model_exp, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='')
    else:
        print('Model directory: %s' % model_exp)
        meta_file, ckpt_file = get_model_filenames(model_exp)

        print('Metagraph file: %s' % meta_file)
        print('Checkpoint file: %s' % ckpt_file)

        saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file))
        saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file))


def get_model_filenames(model_dir):
    files = os.listdir(model_dir)
    meta_files = [s for s in files if s.endswith('.meta')]
    if len(meta_files) == 0:
        raise ValueError('No meta file found in the model directory (%s)' % model_dir)
    elif len(meta_files) > 1:
        raise ValueError('There should not be more than one meta file in the model directory (%s)' % model_dir)
    meta_file = meta_files[0]
    ckpt = tf.train.get_checkpoint_state(model_dir)
    if ckpt and ckpt.model_checkpoint_path:
        ckpt_file = os.path.basename(ckpt.model_checkpoint_path)
        return meta_file, ckpt_file

    meta_files = [s for s in files if '.ckpt' in s]
    max_step = -1
    for f in files:
        step_str = re.match(r'(^model-[\w\- ]+.ckpt-(\d+))', f)
        if step_str is not None and len(step_str.groups()) >= 2:
            step = int(step_str.groups()[1])
            if step > max_step:
                max_step = step
                ckpt_file = step_str.groups()[0]
    return meta_file, ckpt_file

load_model('./arch/pretrained_model/MobileFaceNet_9925_9680.pb')

graph = tf.get_default_graph()
inputs = graph.get_tensor_by_name("input:0")
embeddings = graph.get_tensor_by_name("embeddings:0")

# 预处理图片和提取人脸特征向量
def preprocess_face(img_path, image_size):
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img, (image_size, image_size))
    img = (img - 127.5) / 128.0
    return np.expand_dims(img, axis=0)

# 这里我用了一张test.jpg来测试是否能成功提取
with tf.Session() as sess:
    img_batch = preprocess_face('./test.jpg', image_size=112)
    emb_array = sess.run(embeddings, feed_dict={inputs:img_batch})
    face_vector = emb_array[0]  # 这个就是最终的人脸特征向量
    print("Embedding vector:", face_vector, face_vector.shape)

最终输出一个128维度的特征向量
 

[-0.06593809  0.06479637  0.09942999 -0.036153    0.00230558 -0.05349016
  0.04215376 -0.06550748 -0.03037481 -0.05543394  0.06935196  0.19296463
 -0.12160228  0.00561302 -0.01800596  0.00722877 -0.01265899  0.00910822
  0.03842862 -0.00391093  0.02739868 -0.08836475  0.1739467   0.02687719
 -0.03240912 -0.12639524  0.06778994  0.07201114  0.12070469 -0.03436493
  0.00691453  0.007422   -0.09633193 -0.16478366 -0.01714404 -0.02240757
 -0.08910304  0.037149   -0.02115161  0.03711205  0.09625389  0.13209414
 -0.08295677 -0.00716254 -0.07008244  0.18790661 -0.01504046  0.04478389
  0.10191052  0.0664619   0.18579765  0.08475913 -0.0992318  -0.08140002
  0.01950285 -0.03849183 -0.07778776  0.02332162  0.0691159  -0.05457316
  0.14250448 -0.02169115  0.02955511 -0.02207336  0.01580137 -0.03141747
  0.05358745 -0.12323403  0.00311002  0.09521521 -0.01273317 -0.09979598
  0.05440633 -0.04703691 -0.01374748 -0.09823789 -0.09842855  0.12188262
 -0.0350802  -0.0504017   0.16101992  0.18623154  0.03290209 -0.07698766
  0.00842774 -0.01370594  0.03257138 -0.0154144   0.04919001  0.09898655
 -0.06867442  0.16620728 -0.09661655 -0.11887901 -0.0773934  -0.19313788
  0.05105869  0.06043437  0.15586932 -0.08009912 -0.14306138 -0.00878219
  0.08511933  0.00427768  0.05109494  0.19527836  0.07985236 -0.01872196
 -0.08787084 -0.05589655 -0.08776053  0.02165918 -0.00552292  0.01680779
  0.02282833 -0.06601741  0.02094511  0.18413362 -0.01124173 -0.28399333
  0.09627955 -0.00958961 -0.20936546  0.03144606  0.01021841  0.11831793
 -0.02221197  0.08761073] 

下一步考虑怎么将已收集的人脸图片批量的提取向量并保存

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐