基于dlib实现人脸聚类

转载自:https://www.pythonheidong.com/blog/article/182166/

Dlib 是一个现代化的 C ++ 工具包,包含用于创建复杂软件的机器学习算法和工具 ” 。它使您能够直接在 Python 中运行许多任务,其中一个例子就是人脸检测。

一、先安装

以Ubuntu为例子

step 1:

# for Ubuntu

sudo apt-get install build-essential cmake

sudo apt-get install libgtk-3-dev

sudo apt-get install libboost-all-dev

step 2

pip install dlib

 

二、实现过程

1、官方提供的模型文件 http://dlib.net/files/

http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2

http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2

dlib相关文档  http://accu.cc/content/daze/dlib/install/

下载下面的model

 

 

2、程序目录如下

 

 

faces 存放要聚类的人脸图片,model  存放下载下来的model,out是输出目录

 

3、face_clustering.py如下

# coding: utf-8
"""
@author: xhb
"""
 
import sys
import os
import dlib
import glob
import cv2
 
# 指定路径
current_path = os.getcwd()
model_path = current_path + '/model/'
shape_predictor_model = model_path + '/shape_predictor_5_face_landmarks.dat'
face_rec_model = model_path + '/dlib_face_recognition_resnet_model_v1.dat'
face_folder = current_path + '/faces/'
output_folder = current_path + '/output/'
 
# 导入模型
detector = dlib.get_frontal_face_detector()
shape_detector = dlib.shape_predictor(shape_predictor_model)
face_recognizer = dlib.face_recognition_model_v1(face_rec_model)
 
# 为后面操作方便,建了几个列表
descriptors = []
images = []
# 遍历faces文件夹中所有的图片
for f in glob.glob(os.path.join(face_folder, "*.jpg")):
    print('Processing file:{}'.format(f))
    # 读取图片
    img = cv2.imread(f)
    # 转换到rgb颜色空间
    img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
    # 检测人脸
    dets = detector(img2, 1)
    print("Number of faces detected: {}".format(len(dets)))
 
    # 遍历所有的人脸
    for index, face in enumerate(dets):
        # 检测人脸特征点
        shape = shape_detector(img2, face)
        # 投影到128D
        face_descriptor = face_recognizer.compute_face_descriptor(img2, shape)
 
        # 保存相关信息
        descriptors.append(face_descriptor)
        images.append((img2, shape))
 
# 聚类
labels = dlib.chinese_whispers_clustering(descriptors, 0.5)
print("labels: {}".format(labels))
num_classes = len(set(labels))
print("Number of clusters: {}".format(num_classes))
 
# 为了方便操作,用字典类型保存
face_dict = {}
for i in range(num_classes):
    face_dict[i] = []
# print face_dict
for i in range(len(labels)):
    face_dict[labels[i]].append(images[i])
 
# print face_dict.keys()
# 遍历字典,保存结果
for key in face_dict.keys():
    file_dir = os.path.join(output_folder, str(key))
    if not os.path.isdir(file_dir):
        os.makedirs(file_dir)
 
    for index, (image, shape) in enumerate(face_dict[key]):
        file_path = os.path.join(file_dir, 'face_' + str(index))
        dlib.save_face_chip(image, shape, file_path, size=150, padding=0.25)

4、运行  python3 face_clustering.py

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值