Python - facenet

(1)使用到的python库:   #2.7
tensorflow
scipy
PIL
numpy

(2)下载facenet源码

git clone https://github.com/davidsandberg/facenet

把源码中的align/ 和 facenet.py拷贝到自己的项目中去
align/ :用于人脸检测与人脸对齐的神经网络
facenet :用于人脸映射的神经网络

(3)模型下载
https://drive.google.com/file/d/0B5MzpY9kBtDVZ2RpVDYwWmxoSUk/edit
需翻墙

 

def load_imgs(img_path = base_img_path,use_to_save = True):
    minsize = 20
    threshold = [0.6,0.7,0.7]
    factor = 0.709
    result = {}
    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
    img_paths = os.listdir(img_path)
    for image in img_paths:
        if image == '.DS_Store':
            continue
        aligned = mtcnn(os.path.join(img_path, image),minsize,pnet,rnet,onet,threshold,factor)
        if aligned is None:
            img_paths.remove(image)
            continue
        if use_to_save:
            result[image.split('.')[0]] = aligned
        else:
            prewhitened = facenet.prewhiten(aligned)  # 图片进行白化
            result[image.split('.')[0]] = prewhitened
    return result


def mtcnn(img_path,minsize, pnet, rnet, onet, threshold,factor):
    img = misc.imread(img_path, mode='RGB')  # 读取图片将图片转换成矩阵
    img_size = np.asarray(img.shape)[0:2]
    bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold,
                                                      factor)  # 利用dect_face检测人脸
    # 这里的bounding_boxes实质上是指四个点 四个点连起来构成一个框
    if len(bounding_boxes) < 1:
        print("can't detect face, remove ", img_path)  # 当识别不到脸型的时候,不保留
        return None
        # bounding_boxes = np.array([[0, 0, img_size[0], img_size[1]]])
    det = np.squeeze(bounding_boxes[0, 0:4])
    # 这里是为检测到的人脸框加上边界
    bb = np.zeros(4, dtype=np.int32)
    bb[0] = np.maximum(det[0] - margin / 2, 0)
    bb[1] = np.maximum(det[1] - margin / 2, 0)
    bb[2] = np.minimum(det[2] + margin / 2, img_size[1])
    bb[3] = np.minimum(det[3] + margin / 2, img_size[0])
    # 根据人脸框截取img得到cropped
    cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
    # 这里是resize成适合输入到模型的尺寸
    aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
    return aligned
 
def main(test_path = "imgs/322.jpg",top_n = 100):

    if os.path.exists(database_file) == False:
        print("当前没有数据文件,先进行制作")
        embedDatabaseAndSaveAsNpy()
    preProcess_mtcnn.check_all_in_database(test_path)
    current_emb = np.load(database_file)
    # 将用户的关注整理成列表,现在是这么理解的,数据库不变,用户的关注列表相当于是从数据库中抓取的图片来进行选择对比
    sample_labels = load_labels(test_path) # 读取要测试的图片列表,这里只读取每张图片的名称,因为图片已经在数据库中了没必要在进行一次特征向量映射。
    all_result = []
    mDataDict = changeNpToDict(current_emb) # 把数据库的图片矩阵转换成key-value的字典
    # 这个整理的代码感觉不是很优雅。 暂时先这样把

    for sample in sample_labels:
        if mDataDict.has_key(sample):
            sample_emb = mDataDict[sample].astype('float64')
            result = []
            for compare_index in mDataDict:
                if compare_index == sample:
                    continue
                compare_data = mDataDict[compare_index].astype('float64')
                dist = np.sqrt(np.sum(np.square(np.subtract(sample_emb,compare_data))))
                data = SimpleData(float(compare_index),dist,float(sample)) # 计算两张图片之间的欧氏距离
                result.append(data)
                all_result.append(data)

    all_result.sort(cmp=cmp)  # 对拿到的欧氏距离数组进行排序
    output = []
    count = 0
    for item in all_result:
        for input_label in sample_labels:
            if float(item.key) == float(input_label):  # 排除已存在用户的关注列表中的选项
                continue
        for exist_item in output:   # 排除已存在已选择列表中的选项
            if exist_item.key == item.key:
                continue
        # 这里对key 和 target 进行str处理
        item.key = str(item.key).split('.')[0] + ".jpg"
        item.target = str(item.target).split('.')[0] + ".jpg"
        output.append(item)
        count = count + 1
        if count == int(top_n):
            break
    for item in output:
        print("key:%s,     value:%s,     target:%s"%(item.key,item.value,item.target))

    return output

From: facenet使用记录 https://www.jianshu.com/p/415be6152a3b
 

展开阅读全文

没有更多推荐了,返回首页