python相册中的人脸聚类_基于中文耳语算法的人脸聚类

我以前用过Dlib进行人脸聚类。在

对不起,我没听懂你的问题。

你得到的是错误还是没有得到准确的结果?在

假设您没有得到正确的结果,我建议使用shape_predictor_5_face_landmarks.dat而不是64人脸地标,因为当使用中文耳语算法进行聚类时,它会得到更好的结果。在

你也可以试试DLib自己的中文耳语聚类功能,看看效果是否更好。在#!/usr/bin/python

# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt

#

# This example shows how to use dlib's face recognition tool for clustering using chinese_whispers.

# This is useful when you have a collection of photographs which you know are linked to

# a particular person, but the person may be photographed with multiple other people.

# In this example, we assume the largest cluster will contain photos of the common person in the

# collection of photographs. Then, we save extracted images of the face in the largest cluster in

# a 150x150 px format which is suitable for jittering and loading to perform metric learning (as shown

# in the dnn_metric_learning_on_images_ex.cpp example.

# https://github.com/davisking/dlib/blob/master/examples/dnn_metric_learning_on_images_ex.cpp

#

# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE

# You can install dlib using the command:

# pip install dlib

#

# Alternatively, if you want to compile dlib yourself then go into the dlib

# root folder and run:

# python setup.py install

#

# Compiling dlib should work on any operating system so long as you have

# CMake installed. On Ubuntu, this can be done easily by running the

# command:

# sudo apt-get install cmake

#

# Also note that this example requires Numpy which can be installed

# via the command:

# pip install numpy

import sys

import os

import dlib

import glob

if len(sys.argv) != 5:

print(

"Call this program like this:\n"

" ./face_clustering.py shape_predictor_5_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces output_folder\n"

"You can download a trained facial shape predictor and recognition model from:\n"

" http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2\n"

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

exit()

predictor_path = sys.argv[1]

face_rec_model_path = sys.argv[2]

faces_folder_path = sys.argv[3]

output_folder_path = sys.argv[4]

# Load all the models we need: a detector to find the faces, a shape predictor

# to find face landmarks so we can precisely localize the face, and finally the

# face recognition model.

detector = dlib.get_frontal_face_detector()

sp = dlib.shape_predictor(predictor_path)

facerec = dlib.face_recognition_model_v1(face_rec_model_path)

descriptors = []

images = []

# Now find all the faces and compute 128D face descriptors for each face.

for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):

print("Processing file: {}".format(f))

img = dlib.load_rgb_image(f)

# Ask the detector to find the bounding boxes of each face. The 1 in the

# second argument indicates that we should upsample the image 1 time. This

# will make everything bigger and allow us to detect more faces.

dets = detector(img, 1)

print("Number of faces detected: {}".format(len(dets)))

# Now process each face we found.

for k, d in enumerate(dets):

# Get the landmarks/parts for the face in box d.

shape = sp(img, d)

# Compute the 128D vector that describes the face in img identified by

# shape.

face_descriptor = facerec.compute_face_descriptor(img, shape)

descriptors.append(face_descriptor)

images.append((img, shape))

# Now let's cluster the faces.

labels = dlib.chinese_whispers_clustering(descriptors, 0.5)

num_classes = len(set(labels))

print("Number of clusters: {}".format(num_classes))

# Find biggest class

biggest_class = None

biggest_class_length = 0

for i in range(0, num_classes):

class_length = len([label for label in labels if label == i])

if class_length > biggest_class_length:

biggest_class_length = class_length

biggest_class = i

print("Biggest cluster id number: {}".format(biggest_class))

print("Number of faces in biggest cluster: {}".format(biggest_class_length))

# Find the indices for the biggest class

indices = []

for i, label in enumerate(labels):

if label == biggest_class:

indices.append(i)

print("Indices of images in the biggest cluster: {}".format(str(indices)))

# Ensure output directory exists

if not os.path.isdir(output_folder_path):

os.makedirs(output_folder_path)

# Save the extracted faces

print("Saving faces in largest cluster to output folder...")

for i, index in enumerate(indices):

img, shape = images[index]

file_path = os.path.join(output_folder_path, "face_" + str(i))

# The size and padding arguments are optional with default size=150x150 and padding=0.25

dlib.save_face_chip(img, shape, file_path, size=150, padding=0.25)

您还可以更改阈值和迭代次数,以查看它是否能提供更好的结果。在

希望这有帮助。在

Abstract—Clustering face images according to their latent identity has two important applications: (i) grouping a collection of face images when no external labels are associated with images, and (ii) indexing for efficient large scale face retrieval. The clustering problem is composed of two key parts: representation and similarity metric for face images, and choice of the partition algorithm. We first propose a representation based on ResNet, which has been shown to perform very well in image classification problems. Given this representation, we design a clustering algorithm, Conditional Pairwise Clustering (ConPaC), which directly estimates the adjacency matrix only based on the similarities between face images. This allows a dynamic selection of number of clusters and retains pairwise similarities between faces. ConPaC formulates the clustering problem as a Conditional Random Field (CRF) model and uses Loopy Belief Propagation to find an approximate solution for maximizing the posterior probability of the adjacency matrix. Experimental results on two benchmark face datasets (LFW and IJB-B) show that ConPaC outperforms well known clustering algorithms such as k-means, spectral clustering and approximate Rank-order. Additionally, our algorithm can naturally incorporate pairwise constraints to work in a semi-supervised way that leads to improved clustering performance. We also propose an k-NN variant of ConPaC, which has a linear time complexity given a k-NN graph, suitable for large datasets. Index Terms—face clustering, face representation, Conditional Random Fields, pairwise constraints, semi-supervised clustering.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值